Setup ORACLE 12C / 11G PDO for PHP 7.2 / 7.x

By | July 20, 2018

Bismillahi-r-Rahmani-r-Rahim (In the name of Allah, the most Compassionate, the most Merciful)

Assalamu alaikum wa rahmatullahi wa barakatuh (May the peace, mercy, and blessings of Allah be with you)


This is the ad hoc tutorial on how to enable PHP PDO for ORACLE 12C / 11G. First, we will download and install necessary files and rpm packages. Then we will make the needed system configurations. Next we’ll install the Oracle pdo package, create the php configuration file and test the connectivity with PDO to ORACLE server.

Below content is complementary to the video tutorial above.

Download and install necessary files and packages:
1. Get your Oracle version using SQL query:

select * from product_component_version;

2. Download rpm basic and devel packages corresponding your version from http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html.
3. Copy downloaded basic and devel rpm packages to your server.

yum install -y gcc glibc glibc-common gd gd-devel wget
rpm -ivh oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
rpm -ivh oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm
rm -f  oracle*.rpm

Make the necessary configurations:

ls /usr/include/oracle
oracle_version="12.2"
ln -s /usr/include/oracle/$oracle_version/client64 /usr/include/oracle/$oracle_version/client
ln -s /usr/lib/oracle/$oracle_version/client64 /usr/lib/oracle/$oracle_version/client
echo "export LD_LIBRARY_PATH=/usr/lib/oracle/$oracle_version/client64/lib" > /etc/profile.d/oracle.sh
source /etc/profile.d/oracle.sh

Install the PHP Oracle pdo packages:

php -v
php_version="7.2.6"
wget http://be.php.net/distributions/php-$php_version.tar.gz
tar xfvz php-$php_version.tar.gz
cd php-$php_version/ext/pdo_oci
phpize
./configure --with-pdo-oci=instantclient,/usr/lib/oracle/$oracle_version/client64/lib,$oracle_version
make install
cd ~
rm -Rf php-$php_version*

Create the PHP configuration file:

echo "extension=pdo_oci.so" > /etc/php.d/pdo_oci.ini
php -i | grep oci

Restart the necessary services:

service httpd restart && service php-fpm restart

Test the installed ORACLE PDO (additionally, see the output of phpinfo() for PDO OCI section):

php -r '$con=new PDO("oci:dbname=//192.168.74.139:1521/orcl;charset=utf8","c##test","test");$stmt=$con->prepare("select * from product_component_version"); $stmt->execute();$stmt->setFetchMode(PDO::FETCH_ASSOC);var_dump($stmt->fetch());'

Related resources: