OCI functions allows access to Oracle databases, but, at least in Ubuntu, aren’t included in the default PHP installation. This article will show you how to install php-oci8 on Ubuntu LTS. From the PHP manual:
«They support SQL and PL/SQL statements. Basic features include transaction control, binding of PHP variables to Oracle placeholders, and support for large object (LOB) types and collections. Oracle’s scalability features such as Database Resident Connection Pooling (DRCP) and result caching are also supported.»
Downloads
First we need to download some libraries from here:
https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html
Choose the zip versions of:
- Basic package
- SDK package
- SQL*Plus package. This last one is optional but will be very useful if you want to test your connection with an Oracle database from the command line
Install instant client
This “installation” is just unzip the files you’ve downloaded earlier in a location that you can find later.
While I did this I was using midnight commander: step on the file, press F2, run command (or press @) and write ‘unzip‘. You, as me, could be tempted on press enter to enter the zip and copy the contents, but doing that way breaks some symbolic links.
The alternative is to run on the command line:
unzip <file.zip>
As you can see in my above screenshot I’ve unzipped on a temporary folder on root’s home. I will put everything on /opt/oracle/instantclient. This path doesn’t exist yet, so create with
mkdir -p /opt/oracle/instantclient
Now you can navigate to the destination directory, select all the files with the * key, and pressing F5 to copy them:
Make sure that the following two files exists and are symbolic links to the real library file:
- libocci.so must point to libocci.so.21.1
- libclntsh.so must point to liblntsh.so.21.1
You can check this by running ls -l *so
Configure the dynamic loader to search for this libraries in this path creating the file /etc/ld.so.conf.d/oracle.conf with the following content:
/opt/oracle/instantclient
And then run:
ldconfig
Install the php-oci8 extension
Im assuming that you already have the LAMP (for Apache) or LEMP (for nginx) stack installed on your system, if not you can check this tutorial for LAMP. Or, if you choose Nginx, this is the tutorial for the LEMP stack. When you are ready run:
pecl install oci8-2.2.0
On my first try I’ve got this error: phpsize not found. You can easily fix this by installing the -dev packages with:
apt install php-dev
After installing php-dev you will be able to install oci8-2.2.0 with pecl
You will be asked for the path to the ORACLE_HOME directory, answer this:
instantclient,/opt/oracle/instantclient
The penultimate step is to instruct php to load this extension. I’m using php-fpm so I created a new file /etc/php/7.4/fpm.conf.d/20-oci8.ini with this content:
extension=oci8.so
To me, the last step is to restart php (if you are using lamp, restart apache) in order to PHP load the oci8 extension:
systemctl restart php7.4-fpm
You can easily check if everything went ok with a phpinfo() script:
The hardest step
To finish this task I need to go with our local Oracle guru (a.k.a ‘the dba‘) and ask him permission on the database.