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.