Martins Blog

Trying to explain complex things in simple terms

Compiling PHP 5.3.2 for Oracle 11.2 and Apache 2.2.3

Posted by Martin Bach on May 11, 2010

Today I wanted to experiment with the database resident connection pool. It seems to have been specifically written with PHP in mind so I thought this might be a good idea to get going with this new technology. I didn’t want to start completely from scratch so I searched the docs for PHP and Oracle related documentation. It turns out that you can be an Oracle aware PHP developer in 2 days so I took this as a starting point.

Unfortanately Oracle use Zend Server for PHP. Sure a great product, but I didn’t want to give my details to yet another company to spam me with information I don’t want/need. So I decided to use the source-how difficult can configure && make && make install be?.

Install apache2 and apache 2 devel

I already had httpd installed on my box but I needed the development packages as well. Here’s what I needed to install for my setup:

[root@tomcat ~]# rpm -ihv httpd-devel-2.2.3-43.0.1.el5.x86_64.rpm \
  apr-devel-1.2.7-11.el5_3.1.x86_64.rpm \
  apr-util-devel-1.2.7-11.el5.x86_64.rpm \
  db4-devel-4.3.29-10.el5.x86_64.rpm \
  expat-devel-1.95.8-8.3.el5_4.2.x86_64.rpm \
  openldap-devel-2.3.43-12.el5.x86_64.rpm \
  cyrus-sasl-devel-2.1.22-5.el5_4.3.x86_64.rpm

After a few moments this completed.

PHP 5.3.2

Download the php source from http://www.php.net-it will direct you to a mirror close by. Unzip the code to a convenient location and change into it. A minimal build command sequence is depicted below:

cd php-5.3.2

export ORACLE_HOME=/opt/oracle/client/11.2.0/

./configure \
--with-apxs2=/usr/sbin/apxs \
--enable-sigchild

Yes, it’s intention NOT to configure –with-oci8. The OCI library which is distributed with PHP is not the latest one, see below.

The command failed with this error:

checking whether to enable LIBXML support... yes
checking libxml2 install dir... no
checking for xml2-config path... 
configure: error: xml2-config not found. Please check your libxml2 installation.

Turned out I didn’t have a libxml2 development package installed. Taking care of the problem with RPM:

[root@tomcat ~]# rpm -ihv /mnt/oracleEnterpriseLinux/source/libxml2-devel-2.6.26-2.1.2.8.0.1.x86_64.rpm \
  /mnt/oracleEnterpriseLinux/source/zlib-devel-1.2.3-3.x86_64.rpm 

With the new packages the configure ran through. Perform the magic make && make install. Make test failed for almost all OCI tests by the way so I was a bit weary-it turned out that the LD_LIBRARY_PATH wasn’t set causing the trouble. Copy the development php.ini to your install location, change if needed (I didn’t):

[root@tomcat: ~]# cp php.ini-development /usr/local/lib/php.ini -iv

Apache Configuration

Apache won’t know about “*.php” file straight away-it needs to be told what to do with them. In the PHP case it has to hand them off to the PHP engine for rendering. This works in 2 steps:

  1. You load the shared library for PHP, called libphp5.so. This is the library we just created and linked
  2. You set handlers for PHP files

There is not need to edit the httpd configuration file to add support for PHP5’s dynamic library. The make install command will take care of setting the loadModule directive. The Oracle Enterprise Linux httpd.conf automatically includes files in /etc/httpd/conf.d/ which comes in handy. I created a file php5.conf in said directory with the following content:

[root@tomcat: ~]# vi /etc/httpd/conf.d/php5.conf

<FilesMatch \.php$>
 SetHandler application/x-httpd-php
</FilesMatch>

<FilesMatch "\.phps$">
 SetHandler application/x-httpd-php-source
</FilesMatch>

This should prevent evil people from injecting code masked as pictures. Older settings used the AddHandler directive which wasn’t safe enough.

OCI 8

The OCI8 libraries are the communication layer PHP uses to talk to Oracle. OCI is short for “Oracle Call Interface”. The PECL tool allows you to automatically download and install extension such as OCI8 for Oracle access. It seems to be for PHP what the CPAN module is for perl. My system didn’t have direct Internet access to I needed to create this extension manually. Download the latest version of the OCI extension (1.4.1 when I wrote this) from http://pecl.php.net/package/oci8

Building the extension is simple once you understand how it works. PHP even has an equivalent for the “perl Makefile.pl” step :). Ensure ORACLE_HOME is set to your client, the syntax is slightly different for the instant client.

[root@tomcat ~]# tar --gzip -xvf oci8-1.4.1.tgz && cd oci8-1.4.1
[root@tomcat oci8-1.4.1]# phpize
[root@tomcat oci8-1.4.1]# ./configure -with-oci8=shared,$ORACLE_HOME
[root@tomcat oci8-1.4.1]# make
[root@tomcat oci8-1.4.1]# make install
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20090626/

I edited /usr/local/lib/php.ini and added the following line:

extension=oci8.so

There was no need to set extension_dir – phpinfo() returned OCI the next time I ran it:

output from phpinfo for OCI

Did it work?

I ran a quick test to ensure it works:

[oracle@tomcat admin]$ tnsping reporting

TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on 11-MAY-2010 15:53:43

Copyright (c) 1997, 2009, Oracle.  All rights reserved.

Used parameter files:
/opt/oracle/client/11.2.0/network/admin/sqlnet.ora


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION= (ADDRESS=(PROTOCOL=tcp)(HOST=scanrac11gr2.localdomain)(PORT=1521)) (CONNECT_DATA= (SERVICE_NAME=reporting)))
OK (50 msec)

The PHP script basically logs in and out again.

<html><head><title>Oracle Test</title></head>
<body>

<?php
 if ( $conn=OCILogon("martin", "secret", "reporting")) {
 echo "Successfully connected to Oracle using OCI extension.\n";
 OCILogoff($conn);
 } else {
 $err = OCIError();
 echo "Error in connecting to the Oracle." . $err[text];
 }
?>

</body>
</html>

It actually didn’t work first time-OCI8 was complaining about ORACLE_HOME and LD_LIBRARY_PATH. Adding them into the /etc/init.d/httpd script in the header solved the problem after a restart of httpd.

[root@tomcat ~]# diff -h /tmp/httpd /etc/init.d/httpd 
19a20,23
> # set ORACLE_HOME and LD_LIBRARY_PATH for OCI8
> export ORACLE_HOME=/opt/oracle/client/11.2.0
> export LD_LIBRARY_PATH=$ORACLE_HOME/lib
>

Enjoy your PHP installation!

2 Responses to “Compiling PHP 5.3.2 for Oracle 11.2 and Apache 2.2.3”

  1. Hi Martin,

    I have compiled PHP 5.3.2 with following config

    ./configure –with-oci8=shared,/u01/app/oracle/product/11.2.0/dbhome_1 –with-apxs2=/usr/local/apache2/bin/apxs

    and latest OCI8 has been included.


    oci8

    OCI8 Support enabled
    Version 1.4.1
    Revision $Revision: 294447 $

    Any reason that you did it in two steps ?

    regards,
    Marcin

    • Martin said

      I followed documentation from the Oracle website which suggested that OCI for this particular version wasn’t the current one. So I decided to do it in 2 steps. But I have to agree my PHP skills are a bit “rusty”. Used it at university when MySQL 3.23 and PHP 3 were current :)

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

 
%d bloggers like this: