How to install OCI8 PHP extension in Ubuntu 14.04 to work with Oracle

To work with Oracle DBMS using PHP you need to install oci8 extension.

0. Install Oracle client instant:

http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

The Oracle instant client will be installed at /usr/lib/oracle/12.1/client64

Export the ORACLE_HOME and $LD_LIBRARY_PATH environment variable:

$ export ORACLE_HOME=/usr/lib/oracle/12.1/client64
$ export LD_LIBRARY_PATH=$ORACLE_HOME/lib



1. Install php-pear (needed by pecl) and php5-dev:

$ sudo apt-get install pear
$ sudo apt-get install php5-dev


2. Install oci8 extension using pecl:

$ sudo pecl install oci8

This will ask for Oracle Home Directory – give the path of the instant client:

'instantclient,/usr/lib/oracle/12.1/client64/lib'


3. Enable oci8 in php.ini:

$ sudo echo "extension=oci8.so" >> /etc/php5/fpm/php.ini 
$ sudo echo "extension=oci8.so" >> /etc/php5/cli/php.ini 


4. Restart php5-fpm (or apache if you're using it instead):

$ sudo service php5-fpm restart


5. Check if the oci8 extension has been enabled by loading the phpinfo.php file in your browser:

phpinfo.php

<?php
phpinfo();
?>


6. Try to connect to your Oracle database using the repl interface (the built-in php repl or boris):

$ php -a
Interactive mode enabled

php > $OracleListener = "my.oracleserver.com:1521/MYDBSID";
php > $OracleUsername = "MyOraUser";
php > $OraclePassword = "MyOraPasswd";
php > $conn = oci_connect($OracleUsername, $OraclePassword, $OracleListener);
php > $conn = oci_connect($username, $passwd, $connstr);                                                                     php > if (!$conn) {
php { $e = oci_error();
php { echo $e;
php { }
php > $sql = "select * from mytable where username='genius'";
php > $stmt = oci_parse($conn, $sql);
php > oci_execute($stmt);
php > $row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);
php > foreach ($row as $item) {
php { echo $item;
php { }


Notes: On CentOS, you have to run this to make it work:

setsebool -P httpd_execmem 1

Comments