I was recently tasked with getting PHP talking to an Oracle database. Not an easy task one might say – even with the support that Oracle supposedly throws behind its Linux initiative. So what is the problem? Well, no one compiles packages by hand anymore because it is a total waste of time. When is the last time you compiled a C package on Windows instead of using an .exe? Do you even have a C compiler installed? Didn’t think so… (C’mon Oracle, think .debs)
So what else is a problem? Well, there seems to be no unanimous voice guiding your shaky fingers as you type commands into your terminal as root. Scary. Also, the installable packages of PHP that ship with most distributions seems to have left out the connections for the the database with the world’s largest market share. Not smart.
So this post will attempt to painlessly rectify the situation at hand – getting Oracle and PHP to play nice together.
- Oracle has a client and networking tools package for virtually every system that needs to be installed to provide the Oracle database drivers. For Linux, the options are the same as on Windows – the full client package, or a newer “Instant Client” package. The latter is a much smaller download with less fuss. Go get it from Oracle’s download section of their website. (and get the SDK package too)
- Grab a few dependencies for our environment (from apt-get repositories) namely the following:
- libaio1 – a dependency introduced by Oracle for the 11g database. Just do it, unless you like segmentation faults.
- php5-dev – will be needed for building our new module (don’t worry, PHP isn’t going to be recompiled by hand)
- alien – because the world hasn’t figured out that .deb packages are better.
- Chest-burst your .rpm packages using alien:
sudo alien oracle-instantclient-basiclite-22.214.171.124-1.i386.rpm ; sudo alien oracle-instantclient-devel-126.96.36.199-1.i386.rpm
- Install your newly created .deb packages:
sudo dpkg -i oracle-instantclient-basiclite-188.8.131.52-1.i386.deb; sudo dpkg -i oracle-instantclient-devel_184.108.40.206-2_i386.deb
- With the Oracle environment installed, we are now ready to go get our official Oracle PHP driver – oci8
sudo pear install pecl/oci8
- When it asks you to enter in your “Instant Client” destination, enter in the following path, prefixed with the word “instantclient” and a comma (adjusting for your version number)
- Almost done – add your shiny new driver to your php.ini file so that it is loaded on startup:
sudo vim /etc/php5/apache2/php.ini
Append this to the bottom of the file (for it to be available to the web server):
- Do the same for the file /etc/php5/cli/php.ini (to be available for the command line interface)
- Give ‘er a restart:
sudo /etc/init.d/apache2 force-reload
- Check your progress by creating a php file with phpinfo(), or check in the terminal with:
Troubleshooting: If you receive an error from pear about ‘phpize’ failing without any indication of why, try downloading the file (pear download pecl/oci8) and then extracting, and cd into the newly created folder. Run step 5 again.
Ok, so I snapped at Oracle earlier for not backing Linux. All in all, this wasn’t too bad, but it certainly leaves room for improvements. Personally, I would push the popular distributions to just include this database driver with PHP by default. Or make it an optional package. Again, the market share certainly could justify this.
PHP’s page on oci8: http://www.php.net/oci8
Now what? Read the Underground PHP Oracle Manual