Friday, October 31, 2008

Configure PHP connection to Oracle DB – the RPM way

I have CentOS 4.4 box which I want to connect to Oracle DB 10g via PHP
There are 2 way to configure PHP to connect to Oracle DB using OCI:
A. Use PHP RPMs (which included all php module in RPM package) but nowadays oci8 module no longer provider by CentOS and you might use older version of PHP - easiest and quickest way
B. Use current version of PHP and enable oci8 via PEAR. You will get current PHP version with oci8 enable but still in RPM way

Lets begin

A. Use PHP RPMs packages
1. Install Apache if not installed
CODE:
# yum install httpd


2. Download Oracle Client Software -- if you are using GUI
or download Oracle InstantClient BasicLite (the rpm version) -- I choose this and install it.
to get latest version download it from http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html
CODE:
# rpm –ivh oracle-instantclient-basiclite-*.rpm

my copy of Oracle Instant Client BasicLite is here
http://www.am3n.profusehost.net/phpoci/oracle-instantclient-basic-10.2.0.3-1.i386.rpm

3. Install PHP and makesure you installed php-oci8 module, other modules you can install it if you like
CODE:
# rpm -ivh php-5*.rpm php-oci8-*.rpm php-pear-*.rpm

I am not using CentOS version because it's not provide oci8 module. But I am using PHP from Miracle Linux - Asianux which also look-like Redhat derivatives distro. They provide complete PHP module including oci8.
Download it from my mirror here.
http://www.am3n.profusehost.net/phprh4/

Info about Miracle Linux - Asianux
http://en.wikipedia.org/wiki/Miracle_Linux
Complete PHP modules from Miracle Linux
http://www.miraclelinux.com/update/linux/list.php?errata_id=249

4. Create local variable to connect to Oracle, for NLS_LANG you can set with your locale.
CODE:

# vi /etc/profiles
LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.3/client/lib
export LD_LIBRARY_PATH
NLS_LANG=AMERICAN_AMERICA.UTF8
export NLS_LANG


5. Update linux Library thingy... (locate the oracle library if you use differrent version)
CODE:
# echo /usr/lib/oracle/10.2.0.3/client/bin/ >> /etc/ld.so.conf
# echo /usr/lib/oracle/10.2.0.3/client/lib/ >> /etc/ld.so.conf
# ldconfig
# cd /usr/lib/oracle/10.2.0.3/client/lib/
# ln -s libclntsh.so.10.1 libclntsh.so
# ln -s libocci.so.10.1 libocci.so


6. (Re-)Start the Apache
CODE:
# service httpd restart


I have CentOS 4.4 box which I want to connect to Oracle DB 10g via PHP
There are 2 way to configure PHP to connect to Oracle DB using OCI:
A. Use PHP RPMs (which included all php module in RPM package) but nowadays oci8 module no longer provider by CentOS and you might use older version of PHP - easiest and quickest way
B. Use current version of PHP and enable oci8 via PEAR. You will get current PHP version with oci8 enable but still in RPM way

Lets begin

A. Use PHP RPMs packages
1. Install Apache if not installed
CODE:
# yum install httpd


2. Download Oracle Client Software -- if you are using GUI
or download Oracle InstantClient BasicLite (the rpm version) -- I choose this and install it.
to get latest version download it from http://www.oracle.com/technology/tech/oci/instantclient/instantcl
ient.html
CODE:
# rpm –ivh oracle-instantclient-basiclite-*.rpm

my copy of Oracle Instant Client BasicLite is here
http://www.am3n.profusehost.net/phpoci/oracle-instantclient-basic
-10.2.0.3-1.i386.rpm

3. Install PHP and makesure you installed php-oci8 module, other modules you can install it if you like
CODE:
# rpm -ivh php-5*.rpm php-oci8-*.rpm php-pear-*.rpm

I am not using CentOS version because it's not provide oci8 module. But I am using PHP from Miracle Linux - Asianux which also look-like Redhat derivatives distro. They provide complete PHP module including oci8.
Download it from my mirror here.
http://www.am3n.profusehost.net/phprh4/

Info about Miracle Linux - Asianux
http://en.wikipedia.org/wiki/Miracle_Linux
Complete PHP modules from Miracle Linux
http://www.miraclelinux.com/update/linux/list.php?errata_id=249

4. Create local variable to connect to Oracle, for NLS_LANG you can set with your locale.
CODE:

# vi /etc/profiles
LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.3/client/lib
export LD_LIBRARY_PATH
NLS_LANG=AMERICAN_AMERICA.UTF8
export NLS_LANG


5. Update linux Library thingy... (locate the oracle library if you use differrent version)
CODE:
# echo /usr/lib/oracle/10.2.0.3/client/bin/ >> /etc/ld.so.conf
# echo /usr/lib/oracle/10.2.0.3/client/lib/ >> /etc/ld.so.conf
# ldconfig
# cd /usr/lib/oracle/10.2.0.3/client/lib/
# ln -s libclntsh.so.10.1 libclntsh.so
# ln -s libocci.so.10.1 libocci.so


6. (Re-)Start the Apache
CODE:
# service httpd restart



B. Use your current PHP version from CentOS
If you already install your PHP and don't want to replace it with older version and/or your current version of PHP didn't provide php-oci8 packages you can install it using the following steps
This how to is modification of PHP-OCI8 in Ubuntu Forum
http://www.ubuntuforums.org/archive/index.php/t-92528.html

1. Install Oracle InstantClient
download it from http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html
CODE:
# rpm -ivh oracle-instantclient-basic-10.2.0.3-1.i386.rpm
# rpm -ivh oracle-instantclient-devel-10.2.0.3-1.i386.rpm

My copy is here
http://www.am3n.profusehost.net/phpoci/oracle-instantclient-basiclite-10.2.0.3-1.i386.rpm
http://www.am3n.profusehost.net/phpoci/oracle-instantclient-devel-10.2.0.3-1.i386.rpm

2. Configure linux library thingie...
CODE:
# echo /usr/lib/oracle/10.2.0.3/client/bin/ >> /etc/ld.so.conf
# echo /usr/lib/oracle/10.2.0.3/client/lib/ >> /etc/ld.so.conf

# ldconfig

3. Install oci8 via pear
Make sure you already install php-devel package (and have upgrade to latest version)

CODE:
# cd /usr/local/src
# pear download oci8
# tar xzf oci8-1.2.3.tgz
# cd oci8-1.2.3
# phpize
# ./configure --with-oci8=shared,instantclient,/usr/lib/oracle/10.2.0.3/client/lib/
# make
# make install

My copy of oci8 is here
http://www.am3n.profusehost.net/phpoci/oci8-1.2.3.tgz

4. Enable oci8
CODE:
# vi /etc/php.d/oci8.ini
; Enable oci8 extention module
extension=oci8.so


5. (Re-)Start the Apache
CODE:
# service httpd restart




Just want to make sure if your php is already configure with oci8 module create a php file with phpinfo() function
Create phpinfo file
CODE:
# vi /var/www/html/phpinfo.php


View phpinfo. http://yourserver/phpinfo.php
You should see something like this
CODE:

oci8
OCI8 Support enabled
Revision $Revision: 1.257.2.8 $
Active Persistent Links 0
Active Links 0
Oracle Version 10.1
Compile-time
ORACLE_HOME /opt/oracle/product/10.1.0
Libraries Used -Wl,-rpath,/opt/oracle/product/10.1.0/lib -
L/opt/oracle/product/10.1.0/lib -lclntsh
Temporary Lob support enabled
Collections support enabled

No comments: