Installing PDO_OCI and OCI8 in CentOS

When we want to connect applications to the database, the drivers needed. If the database that you want to connect is Oracle, then we need PDO_OCI or OCI8 driver. Currently the most recommended to be installed is OCI8 as it gets support from Oracle. While PDO_OCI less recommended because it is a product of experiments and Oracle also does not contribute there. Nevertheless we will try to install both.

Oracle

Installing Oracle is easy. You can follow this tutorial by David Ghedini in installing Oracle 11g Express.

Development packages

$ sudo yum install php-pear php-devel zlib zlib-devel bc libaio glibc
$ sudo yum groupinstall "Development Tools"

InstantClient

Download Oracle InstantClient RPM files here. Put these files in your server. Download the basic and devel packages.
  • Basic: oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
  • Devel: oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
Install the downloaded rpm files:
$ sudo rpm -ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
$ sudo rpm -ivh oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm

$ sudo ln -s /usr/include/oracle/11.2/client64 /usr/include/oracle/11.2/client
$ sudo ln -s /usr/lib/oracle/11.2/client64 /usr/lib/oracle/11.2/client
Create a file inside /etc/profile.d named oracle.sh and put this as the content:
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
And run it so we’ll have LD_LIBRARY_PATH as an environment variable.
source /etc/profile.d/oracle.sh

PDO_OCI

Download the PDO_OCI source using pecl.
$ pecl download PDO_OCI
$ tar -xvf PDO_OCI-1.0.tgz
$ cd PDO_OCI-1.0
Inside the PDO_OCI-1.0 folder, edit the file named config.m4.
Find a pattern like this near line 10 and add these 2 lines:
elif test -f $PDO_OCI_DIR/lib/libclntsh.$SHLIB_SUFFIX_NAME.11.2; then
  PDO_OCI_VERSION=11.2
Find a pattern like this near line 101 and add these lines:
11.2)
  PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)
  ;;
Build and install the extension.
$ phpize
$ ./configure --with-pdo-oci=instantclient,/usr,11.2
$ make
$ sudo make install
To enable the extension, add a file named pdo_oci.ini under /etc/php.d and put this as the content:
extension=pdo_oci.so
Validate that it was successfully installed.
$ php -i | grep oci
You should see something like this in the output:
/etc/php.d/pdo_oci.ini,
PDO drivers => oci, odbc, sqlite

OCI8

Download the OCI8 source using pear
$ pear download pecl/oci8
$ tar -xvf oci8-1.4.9.tgz
$ cd oci8-1.4.9
Build and install the extension.
$ phpize
$ ./configure --with-oci8=shared,instantclient,/usr/lib/oracle/11.2/client64/lib
$ make
$ sudo make install
To enable the extension, add a file named oci8.ini in /etc/php.d with this content:
extension=oci8.so
Validate that it was successfully installed.
$ php -i | grep oci8
You should see something like this:
/etc/php.d/oci8.ini,
oci8
oci8.connection_class => no value => no value
oci8.default_prefetch => 100 => 100
oci8.events => Off => Off
oci8.max_persistent => -1 => -1
oci8.old_oci_close_semantics => Off => Off
oci8.persistent_timeout => -1 => -1
oci8.ping_interval => 60 => 60
oci8.privileged_connect => Off => Off
oci8.statement_cache_size => 20 => 20

Finishing up

Do not forget to restart your web server (e.g. Apache). You can double check with phpinfo() if the extensions were successfully installed.