PHP5 with Oracle Drivers – No Recompiling!

Oracle LogoI 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.

  1. 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)
  2. Grab a few dependencies for our environment (from apt-get repositories) namely the following:
    1. libaio1 – a dependency introduced by Oracle for the 11g database. Just do it, unless you like segmentation faults.
    2. php5-dev – will be needed for building our new module (don’t worry, PHP isn’t going to be recompiled by hand)
    3. alien – because the world hasn’t figured out that .deb packages are better.
  3. Chest-burst your .rpm packages using alien:
    sudo alien oracle-instantclient-basiclite- ;
    sudo alien oracle-instantclient-devel-
  4. Install your newly created .deb packages:
    sudo dpkg -i oracle-instantclient-basiclite-;
    sudo dpkg -i oracle-instantclient-devel_11.1.0.1-2_i386.deb
  5. With the Oracle environment installed, we are now ready to go get our official Oracle PHP driver – oci8
    sudo pear install pecl/oci8
  6. 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)
  7. 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):
  8. Do the same for the file /etc/php5/cli/php.ini (to be available for the command line interface)
  9. Give ‘er a restart:
    sudo /etc/init.d/apache2 force-reload
  10. Check your progress by creating a php file with phpinfo(), or check in the terminal with:
    php -m

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:

Now what? Read the Underground PHP Oracle Manual


1 Comment

  1. John Wallom says:

    This is awesome. It will save me a ton on development time.


Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.