6

I want to share some information about my OCI8 installation to access Oracle databases.

If you got errors like "PHP Startup: Unable to load dynamic library 'oci8.so'"

or "checking Oracle Instant Client library version compatibility... configure: error: Link from libclntsh.so to /opt/oracle/instantclient/libclntsh.so.*.1 not found"

or just want to install Quick&Easy OCI8 to get a connection between your php and an Oracle database, you are at the right place!

FixFaier
  • 301
  • 1
  • 2
  • 8

1 Answers1

13

First prerequirements are a working apache2 and php7.2 (Ubunti 18.04) environement.

  1. Download the basic (like instantclient-basic-linux.x64-12.2.0.1.0.zip) and the sdk (instantclient-sdk-linux.x64-12.2.0.1.0.zip) package from the Oracle Website http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index.html

  2. Upload both files to your webserver, you can use WinSCP for it

  3. Unzip both files on server, in my case, you will get a new folder named "instantclient_12_2"

4a. Create destination folder

mkdir /opt/oracle

4b. Move and rename the instantclient folder

mv instantclient_12_2 /opt/oracle/instantclient

4c. Change rights on folder

chown -R root:www-data /opt/oracle

  1. Check if you have the required packages for installing OCI8

apt install php7.2-dev php-pear build-essential libaio1

  1. Create necessary soft links

ln -s /opt/oracle/instantclient/libclntsh.so.12.1 /opt/oracle/instantclient/libclntsh.so

ln -s /opt/oracle/instantclient/libocci.so.12.1 /opt/oracle/instantclient/libocci.so

7a. Add instant client to ld config files

echo /opt/oracle/instantclient > /etc/ld.so.conf.d/oracle-instantclient.conf

7b. Update Dynamic Linker Run-Time Bindings

ldconfig

8a. Now install OCI8 by pecl

pecl install oci8

8b. The OCI8 installation is asking you for the right folder

instantclient,/opt/oracle/instantclient

9a. Add oci lib to the cli php config (console php)

echo "extension = oci8.so" >> /etc/php/7.2/cli/php.ini

9b. Add oci lib to the apache php config

echo "extension = oci8.so" >> /etc/php/7.2/apache2/php.ini

10a. Set environement variables for the cli version (you will need to reboot the server after)

echo "LD_LIBRARY_PATH=\"/opt/oracle/instantclient\"" >> /etc/environment

echo "ORACLE_HOME=\"/opt/oracle/instantclient\"" >> /etc/environment

10b. Set environement variables for the apache version

echo "export LD_LIBRARY_PATH=\"/opt/oracle/instantclient\"" >> /etc/apache2/envvars

echo "export ORACLE_HOME=\"/opt/oracle/instantclient\"" >> /etc/apache2/envvars

  1. Restart Apache

service apache2 restart

  1. You're done, now you can test your connection to the Oracle database

=PHP CONNECTION EXAMPLE=

<?php
// Create connection to Oracle, change HOST IP and SID string!
$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 000.000.000.000)(PORT = 1521)))(CONNECT_DATA=(SID=XXX)))";
// Enter here your username (DBUSER) and password!
$conn = oci_connect("DBUSER", "PASSWORD",$db);
if (!$conn) {
   $m = oci_error();
   echo $m['message']. PHP_EOL;
   exit;
}
else {
   print "Oracle database connection online". PHP_EOL;
}

?>
Javi Stolz
  • 103
  • 2
FixFaier
  • 301
  • 1
  • 2
  • 8
  • Work fine. This help me a lot. But you missed one command: `echo "extension = oci8.so" >> /etc/php/7.2/fpm/php.ini` – mikia Dec 17 '18 at 13:03
  • I tried your instruction with **Ubuntu 18.04**, **ISPConfig 3.1** and **Oracle 11**. Everything works perfectly!!! – mikia Dec 18 '18 at 10:07
  • Perfect.. everything worked. – hybrid May 01 '19 at 02:51
  • Wow. thats a ton of work just to get a PHP extension to work but it works like a charm. Got v11 running in homestead thanks to that tutorial. – Daniel Steiner Oct 30 '19 at 20:28
  • Thank you! Also works with `instantclient_19_6` on php7.4. But, in this case, specify `sudo pecl install oci8-2.2.0`. If you've messed up a bit, and you're receiving `pecl/oci8 is already installed and is the same as the released version 2.2.0`, try to issue a `sudo pecl uninstall oci8-2.2.0`, immediately followed by the `install` verb. – Fabien Haddadi Jan 12 '21 at 18:13
  • Also, if you are in charge of an upgrade on an existing system, you want to make sure that /etc/profile is not used in lieu of /etc/environment, for php cli env vars. Check with `cd /etc; grep -l instantclient * 2>>/dev/null` for existing declarations. – Fabien Haddadi Jan 12 '21 at 18:52
  • This is how to confirm actual linking of OCI8 API to php (cli): `$ php -r "echo (int)function_exists('oci_set_call_timeout'), PHP_EOL;"` Should return 1 for a working set up with php 7.214+ and oci8 2.2.0+ – Fabien Haddadi Jan 12 '21 at 19:03