Configuring PHP with OCI8
Review the previous Requirements section before configuring OCI8.
Before starting the web server, OCI8 typically requires several Oracle environment variables [see below] to locate libraries, point to configuration files, and set some basic properties such as the character set used by Oracle libraries. The variables must be set before any PHP process starts.
The PHP binary must link with the same, or more recent, major version of Oracle libraries as it was configured with. For example, if you build OCI8 with Oracle 19 libraries, then PHP should also be deployed and run with Oracle 19 libraries. PHP applications can connect to other versions of Oracle Database, since Oracle has client-server cross-version compatibility.
Installing OCI8 from PECL Using the pecl Command
The OCI8 extension can be added to an existing PHP installation by using the » PECL repository.
If you are behind a firewall, set PEAR's proxy, for example:
pear config-set http_proxy //my-proxy.example.com:80/
Run
For PHP 7, use
pecl install oci8-2.2.0
When prompted, enter either the value of
$ORACLE_HOME
, orinstantclient,/path/to/instant/client/lib
.Note: Do not enter variable names like
$ORACLE_HOME
or$HOME
becausepecl
will not expand them. Instead, enter an expanded path, for example/opt/oracle/product/19c/dbhome_1
orinstantclient,/Users/myname/Downloads/instantclient_19_8
If you get an error
oci8_dtrace_gen.h: No such file or directory
, it means PHP was built with DTrace Dynamic Tracing enabled. Install using:$ export PHP_DTRACE=yes $ pecl install oci8
Edit your php.ini file and add the line:
Make sure the php.ini directive extension_dir is set to the directory that oci8.so was installed in.
Installing OCI8 from PECL Using phpize
To install OCI8 on an existing PHP installation when the pecl
command is not available, manually download the » PECL OCI8 package, e.g. oci8-3.0.0.tgz.
Extract the package:
tar -zxf oci8-3.0.0.tgz cd oci8-3.0.0
Prepare the package:
Configure the package, either using
$ORACLE_HOME
or Instant Client./configure -with-oci8=shared,$ORACLE_HOME
or
./configure -with-oci8=shared,instantclient,/path/to/instant/client/lib
Install the package:
If you get an error
oci8_dtrace_gen.h: No such file or directory
, it means PHP was built with DTrace Dynamic Tracing enabled. Re-run theconfigure
andmake
commands after setting this environment variable:Edit your php.ini file and add the line:
Make sure the php.ini directive extension_dir is set to the directory that oci8.so was installed in.
Installing OCI8 as a Shared Extension when Building PHP
If you are building PHP from source code, the configuration shared
option can be used to
build OCI8 as a shared library that can be dynamically loaded into PHP. Building a shared extension allows OCI8 to be upgraded easily without impacting the rest of PHP.
Configure OCI8 using one of the following configure options.
If using the free » Oracle Instant Client libraries, then do:
./configure --with-oci8=shared,instantclient,/path/to/instant/client/lib
If Instant Client 12.2 [or earlier] is installed from ZIP files, make sure to create the library symbolic link first, for example
ln -s libclntsh.so.12.1 libclntsh.so
.If using an RPM-based installation of Oracle Instant Client, the configure line will look like this:
./configure --with-oci8=shared,instantclient,/usr/lib/oracle//client/lib
For example, --with-oci8=shared,instantclient,/usr/lib/oracle/19.9/client/lib
Note that Oracle Instant Client support first appeared in PHP 4.3.11 and 5.0.4 and originally used the option --with-oci8-instant-client to configure PHP.
If using an Oracle database or full Oracle Client installation then do:
./configure --with-oci8=shared,$ORACLE_HOME
Make sure the web server user [
nobody
,www
] has access to the libraries, initialization files and tnsnames.ora [if used] under the$ORACLE_HOME
directory. With Oracle 10gR2, you may need to run the $ORACLE_HOME/install/changePerm.sh utility to give directory access.
After configuration, follow the usual PHP building procedure, e.g. make install. The OCI8 shared extension oci8.so library will be created. It may need to be manually moved to the PHP extension directory, specified by the extension_dir option in your php.ini file.
To complete installation of OCI8, edit php.ini and add the line:
Installing OCI8 as a Statically Compiled Extension when Building PHP
If you are building PHP from source code, you can configure PHP to include OCI8 as a static extension using one of the following configure options.
If using Oracle Instant Client, then do:
./configure --with-oci8=instantclient,/path/to/instant/client/lib
If using an Oracle database or full Oracle Client installation then do:
./configure --with-oci8=$ORACLE_HOME
After configuration, follow the usual PHP building procedure, e.g. make install. After successful compilation, you do not need to add oci8.so to php.ini. No additional build steps are required.
Installing OCI8 on Windows
The OCI8 extension can be added to an existing PHP installation by using the DLLs from » PECL repository or the libraries in your PHP installation's ext
directory.
With Oracle 12c [or later] libraries, uncomment one of the php.ini lines extension=php_oci8_12c.dll
or extension=php_oci8_11g.dll
or
extension=php_oci8.dll
. Only one of these DLLs may be enabled at a time. DLLs with higher versions may contain more functionality. Not all DLLs may be available for all versions of PHP. Make sure extension_dir is set to the directory containing the PHP extension DLLs.
If using Instant Client, set the system PATH environment variable to the Oracle library directory.
Setting the Oracle Environment
Before using this extension, make sure that the Oracle environment variables are properly set for the web daemon user. If your web server is automatically started at boot time then make sure that the boot-time environment is also configured correctly.
Note:
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.
On Red Hat Linux and variants, export variables at the end of
/etc/sysconfig/httpd. Other systems with Apache 2 may use an envvars script in the Apache bin directory. A third option, the Apache SetEnv
directive in httpd.conf, may work in some systems but is known to be insufficient in others.
To check that environment variables are set correctly, use phpinfo[] and check the Environment [not the Apache Environment] section contains the expected variables.
The variables that might be needed are included in the following table. Refer to the Oracle documentation for more information on all the available variables.
Common Oracle Environment VariablesORACLE_HOME | Contains the directory of the full Oracle Database software. Do not set this when using Oracle Instant Client as it is unnecessary and may cause installation problems. |
ORACLE_SID | Contains the name of the database on the local machine to be connected to. There is no need to set this if you using Oracle Instant Client, or always pass the connection parameter to oci_connect[]. |
LD_LIBRARY_PATH | Set this [or its platform equivalent, such as LIBPATH , or SHLIB_PATH ] to the location of the Oracle libraries, for example $ORACLE_HOME/lib or /usr/lib/oracle/18.5/client/lib. Note with Instant Client ZIP files on Linux it is more reliable to use ldconfig instead, see the Instant Client installation instructions. With Instant Client 19 [or later] RPM files, ldconfig is automatically run for you. Some users use LD_PRELOAD instead of LD_LIBRARY_PATH .
|
NLS_LANG | This is the primary variable for setting the character set and globalization information used by the Oracle libraries. |
ORA_SDTZ | Sets the Oracle session timezone. |
TNS_ADMIN | Contains the directory where the Oracle Net Services configuration files such as tnsnames.ora and sqlnet.ora are kept. Not needed if the oci_connect[] connection string uses the Easy Connect naming syntax such as localhost/XE . Not needed if the network configuration files are in one of the default locations such as
/usr/lib/oracle/VERSION/client/lib/network/admin, $ORACLE_HOME/network/admin or /etc.
|
Less frequently used Oracle environment variables include TWO_TASK
, ORA_TZFILE
, and the various Oracle globalization settings like NLS*
and the ORA_NLS_*
variables.
Troubleshooting
The most common problem with installing OCI8 is not having the Oracle environment correctly set. This typically appears as a problem using oci_connect[] or oci_pconnect[]. The error may be a PHP error such as Call to undefined function oci_connect[], an Oracle error such as ORA-12705, or even an Apache crash. Check the Apache log files for startup errors and see the sections above to resolve this problem.
While network errors like ORA-12154 or ORA-12514 indicate an Oracle network naming or configuration issue, the root cause may be because the PHP environment is incorrectly set up and Oracle libraries are unable to locate the tnsnames.ora configuration file.
On Windows, having multiple versions of Oracle on the one machine can easily cause library clashes unless care is taken to make sure PHP only uses the correct version of Oracle.
A utility to examine what libraries are being looked for and loaded can help resolve missing or clashing library issues, particularly on Windows.
Note: If the web server doesn't start or crashes at startup
Check that Apache is linked with the pthread library:
# ldd /www/apache/bin/httpd libpthread.so.0 => /lib/libpthread.so.0 [0x4001c000] libm.so.6 => /lib/libm.so.6 [0x4002f000] libcrypt.so.1 => /lib/libcrypt.so.1 [0x4004c000] libdl.so.2 => /lib/libdl.so.2 [0x4007a000] libc.so.6 => /lib/libc.so.6 [0x4007e000] /lib/ld-linux.so.2 => /lib/ld-linux.so.2 [0x40000000]If the libpthread is not listed, then reinstall Apache:
# cd /usr/src/apache_1.3.xx # make clean # LIBS=-lpthread ./config.status # make # make installPlease note that on some systems like UnixWare, it is libthread instead of libpthread. PHP and Apache have to be configured with EXTRA_LIBS=-lthread.
f dot kheiri at ucl dot ac dot uk ¶
16 years ago
To compile PHP 4.4.2 with OCI8 / Oracle 9i / AIX 5.2 [64-bit] / GCC
PHP will automatically compile against the Oracle 64-bit libraries, which PHP being a 32-bit app, will cause "make" to fail [though it will configure fine]. Here's how to avoid this:
1. replace your PHP's ext/oci8 directory with the latest ext/oci8 directory downloadable from PECL - then remove the old directory entirely
2. rebuild configure by running:
./buildconf --force
...in PHP's root directory. This will rebuild the configure script.
3. using a tool like sed, in your configure file replace all instances of /path/to/oracle/lib with /path/to/oracle/lib32 - note, this may require GNU's autoconf, m4 and gnumake.
4. export LD_LIBRARY_PATH=/path/to/oracle/lib32
5. ./configure --with-oci-8=/path/to/oracle [and any other options]
Then, the usual make and make install.
alvaro at demogracia dot com ¶
14 years ago
If you've followed the instructions and you can't even connect to the DB server, welcome to the Oracle hell. Most of the information you'll find is deprecated, incomplete, not for your platform, unnecessary or just plain wrong.
Typically, you won't need at all those complicate setups you'll read about and they'll probably make things harder. I suggest you get Systernal's "Filemon" utility [for Windows, in Unix you may do with strace] and find out what exact config files and DLLs are being tried by php.exe [or httpd.exe if PHP runs as Apache module or...]. Pretty often, the issue is that [e.g.] TNSNAMES.ORA does not have the correct line ending or Apache is looking for a DLL that does not even exist in your hard disc; learning that prevents you to waste time adding more and more useless environmental variables.
Goog luck.
devolver at iastate dot edu ¶
20 years ago
I spent several hours tracking down error ORA 24374, which would result from only *SOME* of my select statements. This error would be caused if I made a query that would return any non-numerical value. I am running an Apache 1.3.x webserver and PHP 4.2.1.
The fix is to add entries in your httpd.conf file that would export your environment settings. I added these three lines and everything worked like a charm!
SetEnv ORACLE_HOME /path/to/oracle/home
SetEnv ORA_NLS33 /path/to/oracle/home/ocommon/nls/admin/data
SetEnv NLS_LANG AMERICAN
Obviously, if your NLS_LANG is different, you should set it to whatever your NLS_LANG actually is. Ask your friendly DB admin for this information.
Hope this helps someone who treads down the path that I just followed!
Trent
Saxon Leung ¶
10 years ago
Spending long time in researching how to install this on a freshly installed [Red Hat-based [RHEL, CentOS, Fedora]] Linux, and finally I come to this:
1] Download & install
#yum install oracle-instantclient-basic
#yum install oracle-instantclient-devel
or
Download:
oci8-1.4.7.tgz
oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
#rpm -i oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
#rpm -i oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
2] Check PHP version:
#rpm -qa | grep php
It should looks sth like this:
php-devel-5.3.10-1.w5
php-pdo-5.3.10-1.w5
php-ldap-5.3.10-1.w5
php-cli-5.3.10-1.w5
php-mysql-5.3.10-1.w5
php-5.3.10-1.w5
php-common-5.3.10-1.w5
3] Update if needed by:
#yum --enablerepo=webtatic update php
** Make sure you have php-devel installed before updating to a newer version, or you might have problem to add package back. If it happened, you\\'ll have to reinstall all php packages [sadly I did]:
#yum remove php-common
#yum --enablerepo=webtatic install php
4] Remember to install a C compiler for installation use:
#yum install gcc
5] Unzip oci8-1.4.7.tgz:
#tar xvzf oci8-1.4.7
6] Run phpize [php-devel required]:
# phpize
7] Setup Config:
# ./configure --with-oci8=shared,instantclient,/usr/lib/oracle/11.2/client64/lib
8] Add a sym-link to the SDK header files [Comes with the instantclient-devel]
#ln -s /usr/include/oracle/11.2/client64/ /usr/lib/oracle/11.2/client64/lib/include
** The installation defaults to use 1 dir path only, so the missing SDK files need to add back to that directory, by adding a sym-link \\"include\\" under the lib folder.
9] Compile and install:
#make all install
[Please note that I'm installing on a 64-bit platform so links are pointing at client64 instead of client]
semenov dot v at gmail dot com ¶
3 years ago
CentOS 7 PECL with oci8-2
After documented installation stucks
$ yum install systemtap-sdt-devel
$ export ORACLE_HOME=/usr/include/oracle/XX.X/client64
$ LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export $ LD_LIBRARY_PATH
$ export PHP_DTRACE=yes
$ C_INCLUDE_PATH=/usr/include/oracle/XX.X/client64 pecl install oci8
[autodetect]
$ echo extension=oci8.so > /etc/php.d/instantclient.ini
$ systemctl reload https
aliquis at die dot spammers dot die dot link-net dot org ¶
18 years ago
I've had all kinds of errors with RedHat9[yuck], PHP 4.3.4RC1 and Oracle enterprice server 9.2. For some time I thought that --with-oci8 wouldn't work with Oracle9 but that wasn't the case, the solution was many steps.
1] install oracle, might require some tricks, on RedHat9 I had to put this in my .bashrc:
export ORACLE_BASE=/opt/ora9
export ORACLE_HOME=$ORACLE_BASE/product/9.2
export PATH=$ORACLE_HOME/bin:$PATH
export ORACLE_OWNER=oracle
export ORACLE_SID=netadmdb
export ORACLE_TERM=vt100
export LD_ASSUME_KERNEL=2.4.1
export THREADS_FLAG=native
export LD_LIBRARY_PATH=$ORACLE_BASE/product/9.2/lib:$LD_LIBRARY_PATH
2] compile php, use --with-oci8
3] Make an init-script for the oracle9 database server, as default the TNS listener doesn't run so add that on aswell, example:
#!/bin/bash
export ORACLE_HOME=/opt/ora9/product/9.2
export PATH=$ORACLE_HOME/bin:$PATH
export ORACLE_SID=netadmdb
export DISPLAY=:0
oracle_user=oracle
export oracle_user
case $1 in
start]
su - "$oracle_user"