13 dic. 2009

Install Oracle PDO PHP (php_oci) driver in Ubuntu Hardy (8.04)

Ubuntu Hardy comes with some PHP PDO driver like mysql, postgresql and sqlite. Trying to install Oracle driver with:
sudo pecl install php_oci

fails in my Ubuntu with the following error:
pear/PDO_OCI requires PHP extension "pdo" (version  >= 1.0)

Isn't "pdo" installed already? It is part of php5-common, but PEAR/PECL doesn't know about it. So let's go ahead with ignoring dependency checks:
sudo pecl install -n php_oci

If you haven't installed php5-dev you'll get the following error:
sh: phpize: not found
ERROR: `phpize` failed

So install php5-dev, if you didn't already do it. Now, running the former pecl could yield:
...configure: error:
You need to tell me where to find oracle SDK, or set ORACLE_HOME.

Isn't ORACLE_HOME already set? Running a
echo $ORACLE_HOME
should answer this doubt. In my environment, it's set, but sudo doesn't pass it to the command, let's try again:
sudo -E pecl install -n pdo_oci

configure: error: Cannot find php_pdo_driver.h

The configure script tries to find it in:
/usr/include/php/....

This directory tree is empty or non-existent (in my Ubuntu).
cd /usr/include
sudo rm -rf php
sudo ln -s /usr/include/php5 php


Now the install succeeds. We have to enable it in /etc/php5/conf.d. Create a file 'pdo_oci.ini' with the following content:
#config por PDO OCI Oracle
extension=pdo_oci.so


Let's test is with php interactive mode:

$ php -a
php > $dbh = new PDO('oci:dbname=xe', 'system', '<password>');
php > $sql = 'select * from dual';
php > foreach ($dbh->query($sql) as $row){
php { print_r($row);
php { }
Array
(
[DUMMY] => X
[0] => X
)
php > [Control-D]


If we create now a simple php page which tries to show some Oracle output we get a PHP error message (depending on how error reporting is configured in PHP, it may not be rendered in PHP page, but be present in the log):
PDOException: SQLSTATE[]: pdo_oci_handle_factory: OCI_INVALID_HANDLE (/tmp/pear/cache/PDO_OCI-1.0/oci_driver.c:463) in ... on line ...

The problem is that Apache has no access to the ORACLE_HOME environment variable. Just add it to /etc/apache2/envvars:
export ORACLE_HOME=<path to Oracle, mine is: /usr/lib/oracle/xe/app/oracle/product/10.2.0/server>


Now we should be done.

5 comentarios:

  1. Thank you.
    Information about php_pdo_driver.h very helped me.

    ResponderEliminar
  2. Genius! Thank you so much for publishing this!

    ResponderEliminar
  3. One last thing. I found that in my setup, I had to add the default Apache user to Oracle's "oinstall" group or I got segment faults trying to connect to the database.
    The command to do so is:
    sudo adduser www-data oinstall

    ResponderEliminar
  4. Trying to follow your tips I stumbled on this problem, running Ubuntu 11.04:

    $ sudo -E pecl install -n pdo_oci
    downloading PDO_OCI-1.0.tgz ...
    Starting to download PDO_OCI-1.0.tgz (13,679 bytes)
    .....done: 13,679 bytes
    7 source files, building
    running: phpize
    Cannot find config.m4.
    Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

    ERROR: `phpize' failed

    Note: I DO have php5-dev installed. "which phpize" returns /usr/bin/phpize.

    ResponderEliminar
  5. Igor, do you have "build-essential" installed? May it be that it can't find the M4 macro processor?

    ResponderEliminar