Setup Oracle Instant Client with PHP 7 on Ubuntu Server 17.04

Enrique Becerra
Jul 25, 2017 · 4 min read

I built a WebService long time ago on a Windows 2008 Server. Then I installed XAMPP (https://www.apachefriends.org/es/index.html), which is a “developer environment” available for Windows, Linux and Mac OS which comes with Apache, PHP and (that time) MySQL Server all pre-installed. You can setup and configure all these 3 with just some clics.

The WebService should connect to an Oracle Server and I had to research and struggle a lot to make PHP connect to Oracle on Windows. All was based in setting the right Paths in the Environment Variables and getting Oracle Instantclient. All went OK during the years. Server was upgraded to 2012 R2 and was still working without problems.

Nowadays, with all the buzz around Virtualization and such, we got a server with ESXI, got VMWare and VSphere licenses and begun converting all our phisical servers to Virtual Machines. The process took weeks, but we could manage to keep the uptime in a good value.

Later, I was given the great idea to move the Windows 2012 R2 server (and its WebService) to a new Virtual Machine with Ubuntu Server 17.04

I begun researching and decided to go from scratch with everything. I found lots of tutorials, but had to reset and go back several times until I found a way to make all work with the same codebase I had previously on the Windows Server. These are all the summarized steps I followed.

Update: Nov. 20, 2018. I’ve added/changed some lines to reflect latest PHP and Oci8 Library versions on latest Ubuntu. Thanks Adam!

  • Register a Free account, accept the License Agreement and download the RPM (not zip!) files for each package highlighted in the image, Instant Client Package Basic and Instant Client Package SDK
  • Transfer the RPM packages to your Ubuntu (maybe home/yourusername) and then install the alien package converter and build tools to later compile oci8 library.

sudo apt-get install alien build-essential

  • Now we must install all the necessary packages to setup Apache and PHP. We won’t install pear because we will get and compile manually oci8 from the PECL website.

sudo apt-get install apache2 php7.2 php7.2-dev libaio1

  • Now we can install everything. First we have to convert the RPM packages to an Ubuntu compatible format (deb) and that’s a task for alien. Go in your terminal to the path where the rpms are located and fire:

sudo alien oracle-instantclient12.2-basic-12.2.0.1.0–1.x86_64.rpm
sudo alien oracle-instantclient12.2-devel-12.2.0.1.0–1.x86_64.rpm

Once packages were converted, do not forget to install the deb packages! If you don’t do this, all the rest will be void

sudo dpkg -i oracle-instantclient12.2-basic-12.2.0.1.0–1.x86_64.deb
sudo dpkg -i oracle-instantclient12.2-devel-12.2.0.1.0–1.x86_64.deb

  • Go to PECL website and get the latest oci8 package http://pecl.php.net/package/oci8
  • Download the tgz file and again move it to your home/yourusername in your Ubuntu installation
  • Uncompress the oci8 source and execute all these commands

tar -xzvf oci8–2.1.8.tgz
cd oci8–2.1.8
phpize
./configure — with-oci8=shared,instantclient,/usr/lib/oracle/12.2/client64/lib
make
sudo make install

This will build and install the oci8 library which will be located (probably) in

/usr/lib/php/20170718/oci8.so

  • Now we should edit both these files…

/etc/php/7.0/apache2/php.ini
/etc/php/7.0/cli/php.ini

  • Enable the oci8 extension by adding the line to each php.ini

extension=/usr/lib/php/20170718/oci8.so

  • Save each php.ini

IMPORTANT

This is the most important step which made me loose a couple of days until I got a reply in Oracle forums which made things work.
Most tutorials tell you to edit the /etc/environment file and add set path and oracle variables into that file. WRONG!!.
You must edit the
/etc/apache2/envvars
then add the following lines:

export PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/administrador/oracle/oci8–2.1.8/modules:/usr/lib/oracle/12.2/client64:/usr/lib/oracle/12.2/client64/lib”
export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib
export ORACLE_HOME=/usr/lib/oracle/12.2/client64

  • Now restart server (maybe just restarting apache service will work)

sudo shutdown -r now

  • Create a new test.php file in /var/www/html folder with something like this

<?php
$dbstr =”(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ORACLEIP)(PORT=1521))(CONNECT_DATA=(SID=ORACLESID)))”;
if($conn = oci_connect(‘YOURUSERNAME’,’YOURPASSWORD’, $dbstr)):
print “CONNECTED OK!!”;
else:
print “WE HAVE A PROBLEM”;
endif;
?>

  • Open your browser and load the url to test

http://UBUNTUSERVERIP/test.php

Final Notes

I had to install, compile and build oci8 manually because this server is behind a very restricted firewall. You can do it simply by

sudo apt-get install php-pear
sudo pecl install oci8

Thanks for reading and hope to save someone else’s headaches !!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade