简体   繁体   中英

Oracle 11g PHP oci_connect fails OCIEnvNlsCreate()

Problem: Linux CentOS/Oracle 11g/InstantClient/PHP 5.3.3

Error: oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with your system - please check that ORACLE_HOME and LD_LIBRARY_PATH are set and point to the right directories

phpinfo() relevant sections:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

As you see, the environment variables are correctly set, and I can echo them on the page with with getenv() . I also checked the read permissions and I can read all the files in these directories from the webpage. The client library files are there (I installed and reinstalled twice) full install. The libraries are readable and loadable.

ldd did not show any errors.

Here is the call from php file:

$test = getenv('LD_LIBRARY_PATH')."  ".getenv('ORACLE_HOME');
$this->dbconn = oci_connect("myuser","mypassword", "localhost/orcl") or die("<b>Connection Failed: $test </b>").oci_error();

I tried all the tricks I found from googling, nothing worked.
Any help is appreciated.

Got OCIEnvNlsCreate() failed? Welcome to hell...

I know that the thread is old, but the problem is still present. And usually the answers to this problem redirect to third party sites, usually 404s .

Let's start with the problem. The error itself is pretty generic. Its documentation sums up to "lib crashed...dunno why". There is a wide range of solutions including, but not limited to configuring, reinstalling and praying to "insert random god name here".

Here is a short list of the most common solutions:

1. Put the variable in your .php file.

    putenv("LD_LIBRARY_PATH=$newld");

where $newld is the link to your library.

2. Remove the variable LD_LIBRARY_PATH , if exists(yeah, the way it was shown in 1.), from your php file and place it into your apache2.conf or httpd.conf or /etc/environment or whatever file your distribution/version has for the environment variables. A simple guide can be found here or everywhere on the internet. Why?

Do not set Oracle environment variables using putenv() in a PHP script because Oracle libraries may be loaded and initialized before your script runs. Variables set with putenv() may then cause conflicts, crashes, or unpredictable behavior. Some functions may work but others might give subtle errors. The variables should be set up before the web server is started.

as stated in the installation docs. Also restart apache afterwards. It's distribution/version based, but most probably the command is service httpd restart .

  1. Reinstall oci8 . As many times as it take. Don't force it. Forcing only makes it worse. Also, try to compile it (aka download the package use ./configure -'all the fancy commands you'll need to use' and then make install ).

  2. Make sure u got the right LD_LIBRARY_PATH . Funny thing to say, right? You probably didn't notice that oracle created the folder client/lib/ in N different locations which contains more or less the same files, and the only difference, between them, is that choosing all but one of those folders will result in the mentioned error . Sanity check?

  3. Reinstall php/apache . It's probable that you had some old installation that is causing issues. So use apt-get purge php* or yum remove php* or whatever does the same trick for your distribution, will be a good start.

EDIT After PHP upgrade the problem reoccurred, this time with a different solution.

  1. EDIT SYSCONFIG Edit the file /etc/sysconfig/httpd. Add those 2 lines at the end:

    export LD_LIBRARY_PATH=/path/to.lib export ORACLE_HOME=/path/to/home

  2. GIVE UP AND READ THE MANUAL it can be found here .

If none of those things help, feel free to browse the wall of sorrow , for a solution...

I've also dealt with these same issues myself and I found a solution.

The solution in my case was:

  1. Error: Error: oci_connect(): OCIEnvNlsCreate() failed

Grant permissions to client directory and files:

sudo chmod -R 777 /usr/lib/oracle/11.2/client64

and

sudo chmod -R 777 /usr/include/oracle/11.2/client64

2.Error which I got later: ORA-12546: TNS:permission denied

setsebool -P httpd_can_network_connect 1 

Apache would be authorized to connect the db.

You need to copy all content of the instant client to apache/bin

im using xampp and working for me.

copy all files of the instant client enter image description here to apache/bin

I've dealt with these same issues myself except on an Ubuntu system. We got around it by setting the environment variables directly in /etc/environment and leaving them out of the apache config. Just add the variables at the bottom of the environment file and they'll apply system wide.

add your custom environment variable on apache init script. I'm using centos, so, init script is located on /etc/init.d/httpd

simply, copy paste your oracle user's .bash_profile onto /etc/init.d/httpd

this is mine.

ORACLE_HOSTNAME=ora11g.home.com; export ORACLE_HOSTNAME
ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1; export ORACLE_HOME
ORACLE_SID=ora11g; export ORACLE_SID
ORACLE_TERM=xterm; export ORACLE_TERM
PATH=$ORACLE_HOME/bin:/usr/sbin:$PATH; export PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib;
export LD_LIBRARY_PATH
CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;
export CLASSPATH

hope this help

I just solved this same issue in :

  • Apache
  • CentOS 7
  • PHP 5
  • Oracle 12cR1

The message says :

please check that ORACLE_HOME and LD_LIBRARY_PATH are set and point to the right directories

In apache, there is a file where you can set the environment variable : /etc/sysconfig/httpd . There has to be a config file for nginx.

I added the required environment variables. Assuming that

  • ORACLE_HOME is /oracle/product/12.1.0/dbhome_1
  • Oracle client was installed under /oracle/product/12.1.0/client

Add :

ORACLE_HOME=/oracle/product/12.1.0/dbhome_1
LD_LIBRARY_PATH=/oracle/product/12.1.0/dbhome_1/lib:/oracle/product/12.1.0/dbhome_1/network/lib:/oracle/product/12.1.0/client/lib

I also added :

TNS_ADMIN=/oracle/product/12.1.0/dbhome_1/network/admin
ORACLE_SID=<your sid>

After updating /etc/sysconfig/httpd I just restarted Apache's service httpd and that was it:

$ sudo systemctl restart httpd

I hope it helps !

My solution for this php_warning was below:

Added below lines to the httpd config and the warning dissapeared.

# cat /etc/sysconfig/httpd  |grep -v "#"
LANG=C
ORACLE_HOME=/oracle/product/19
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib

The error was: [Wed Dec 02 10:06:27.291278 2020] [:error] [pid 1744] [client 127.0.0.1:39610] PHP Warning: oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with your system - please check that ORACLE_HOME and LD_LIBRARY_PATH are set and point to the right directories in /var/www/html/websrv/ws_reg.php on line 57 [Wed Dec 02 10:06:27.291401 2020] [:error] [pid 1744] [client 127.0.0.1:39610] PHP Warning: oci_connect(): Error while trying to retrieve text for error ORA-01804\\n in /var/www/html/websrv/ws_reg.php on line 57 [Wed Dec 02 10:06:27.291531 2020] [:error] [pid 1744] [client 127.0.0.1:39610] PHP Warning: oci_close() expects parameter 1 to be resource, boolean given in /var/www/html/websrv/ws_reg.php on line 588

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM