Sunday, August 10, 2008

Install Oracle Instant Client and PHP OCI8 module

If you want to connect to an Oracle database with PHP, you can use Oracle's Instant Client and the oci8 module from pear.

Download the Basic and the SDK packages from oracle.com. At the time of this writing, the filenames are instantclient-basic.zip and instantclient-sdk.zip.

Unzip these files in a new directory, e.g. /opt/oracle/instantclient.

Code:
[bash]
sudo su
mkdir -p /opt/oracle/instantclient
cd /opt/oracle/instantclient
unzip instantclient-basic.zip
unzip instantclient-sdk.zip
echo /opt/oracle/instantclient >> /etc/ld.so.conf
ldconfig
[/bash]

The previous two lines are supposed to create symlinks named libclntsh.so and libocci.so which we will need later. In my case these symlinks were not created by ldconfig, so I created them manually.

Code:
[bash]
ln -s libclntsh.so.11.1 libclntsh.so
ln -s libocci.so.11.1 libocci.so
[/bash]

In the next step we will download the oci8 module with pear. Pear is in the php-pear package.

Code:
[bash]
apt-get install php-pear
[/bash]

Also, you need php5-dev and build-essential packages for compiling oci8 module.

Code:
[bash]
apt-get install php-pear php5-dev build-essential
[/bash]

"Normally" we should be able to just use pecl install oci8 now, but apparently pear is not able to figure out where the instantclient libraries are. So we will just download the oci8 module and build it on our own.

Code:
[bash]
mkdir -p /usr/local/src
cd /usr/local/src
pecl download oci8
tar xzf oci8-1.3.4.tgz
cd oci8-1.3.4
phpize
./configure --with-oci8=shared,instantclient,/opt/oracle/instantclient
make
make install
[/bash]

The oci8-1.3.4.tgz filename will of course change for newer releases.

To enable the oci8 module in the php.ini (/etc/php5/apache2/php.ini and /etc/php5/cli/php.ini), add a line
Code:
[text]
extension=oci8.so
[/text]

(put this line after the examples starting with ;extension).

Now stop and start Apache. You should see the oci8 module in the output of phpinfo().

Good luck