Building ORACLE support into PHP
Some people can be frightened off by the thought of building a Linux web server from source, let alone adding PHP support. There are, of course, other options available such as packaged binary distributions of Apache, PHP, and Zend Core for ORACLE. But if building your own Apache installation from scratch is non-negotiable, then it's time to roll those sleeves up again.
In this article, we throw caution to the wind and go one step further and add ORACLE support into the mix. In for a penny, in for a pound as the saying goes!
For the benefit of Linux users who wish to build Apache and PHP from source, what follows is a cookbook approach to building and configuring Apache 2.2.3 and PHP 5.2.0 for use with a locally installed ORACLE database.
Assumptions
We will assume that there is a local instance of ORACLE running and that the user account we shall be using to build Apache and PHP has no problem connecting to this instance using SQL*Plus. In particular, the environment variable ORACLE_HOME must be available during the build of PHP's OCI8 extension. The other assumption is that external sources of information (such as installation instructions) will be consulted for additional help (e.g., how to configure and start up Apache, set up PHP.ini and so on).
If there is no local ORACLE installation present, the host will, at the very least, require the ORACLE client libraries. According to PHP.net, a basic installation of Oracle Instant Client will suffice.
In this particular walkthrough, the Linux system happens to be a full (all packages) install of Fedora Core 4 Linux dating back to 02-Feb-2006.
Ingredients
- Apache 2.2.3 source, available from Apache.org
- PHP 5.2.0 source, available from PHP.net
Method
The following files were downloaded from their respective sites and placed in /tmp:
- Apache: httpd-2.2.3.tar.bz2
- PHP: php-5.2.0.tar.bz2
Preparatory work
We set up a couple of environment variables for use later. Here, we will be aiming to install Apache and PHP under the /usr/local directory. Customise these locations accordingly (figure 1.0).
Figure 1.0
$ mkdir ~/buildarea $ export WORKDIR=~/buildarea $ export LIBS=-lpthread $ export TARGET_APACHE=/usr/local/apache-2.2.3 $ export TARGET_PHP=/usr/local/php-5.2.0
Gathering the files
Transfer the source code archives to our working directory in preparation for building (figure 1.1).
Figure 1.1
$ cd ${WORKDIR}
$ cp /tmp/httpd-2.2.3.tar.bz2 .
$ cp /tmp/php-5.2.0.tar.bz2 .
$ #
$ # Do not forget to generate the MD5 digests of these tarballs
$ # using md5sum and compare them with the values
$ # published on Apache.org and PHP.net.
$ #
Build Apache 2.2.3
Figure 1.2 shows the steps taken to construct and install the web server software.
Figure 1.2
$ tar -jxvf httpd-2.2.3.tar.bz2
$ cd httpd-2.2.3
$ ./configure --prefix=${TARGET_APACHE} --enable-mods-shared=all
$ make
$ sudo make install
An alternative to using the sudo command, above, is to issue the following commands instead (figure 1.3):
Figure 1.3
$ su # to take on the root user's privileges. # make install # to install the software on the system. # exit # to return to your regular user account. $
Post-build checks
Here are some checks to perform to ensure that everything went as expected (figure 1.4). Of particular importance is the presence of the pthread library in the Apache executable. If it is absent, refer to PHP.net.
Figure 1.4
$ ${TARGET_APACHE}/bin/httpd -l
$ # Expect the output to be as follows:
$ # core.c
$ # prefork.c
$ # http_core.c
$ # mod_so.c
$ ldd ${TARGET_APACHE}/bin/httpd
$ # Check for the presence of libpthread.so in the output.
Prepare to build PHP 5.2.0
With Apache built and installed (but not tested) we move on to PHP 5.2.0. First, we'll unpack the tarball (figure 1.5) and then create a simple script to make things easier (figure 1.6).
Figure 1.5
$ cd ${WORKDIR}
$ tar -jxvf php-5.2.0.tar.bz2
$ cd php-5.2.0
Create a script called build-php5 with the following contents (figure 1.6). This is given by way of example. Note the value of --prefix and adjust accordingly if TARGET_PHP has been customised in the preparatory step above.
Figure 1.6
#!/bin/bash
./configure \
--prefix=/usr/local \
--with-config-file-path=${TARGET_APACHE} \
--disable-debug \
--enable-pic \
--enable-shared \
--enable-inline-optimization \
--enable-sigchild \
--with-apxs2=${TARGET_APACHE}/bin/apxs \
--with-exec-dir=${TARGET_PHP}/bin \
--with-regex=php \
--with-gettext \
--with-gd \
--with-jpeg-dir=/usr \
--with-png \
--with-zlib \
--with-gdbm \
--enable-debugger \
--enable-magic-quotes \
--enable-safe-mode \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-track-vars \
--enable-yp \
--enable-ftp \
--enable-wddx \
--with-mysql=/usr/lib/mysql \
--with-mysqli \
--with-oci8=shared \
--with-xml \
--with-curl
Build PHP 5.2.0
Figure 1.7 shows how to go about building and installing PHP.
Figure 1.7
$ chmod +x build-php5 $ ./build-php5 $ make $ sudo make install
That's it! Apache 2.2.3 and PHP 5.2.0 have been built. The rest is down to configuration.
Configure Apache and PHP
- Inspect the contents of your freshly-installed Apache 2.2.3's
httpd.conffile and configure accordingly. For the moment, disable any PHP directives (e.g., the LoadModule directive) you come across; - Ensure that Apache starts up and shuts down with no error messages being written to its error log and that it can serve simple HTML files without any problems. Shut down Apache once again;
- Enable support for PHP as per its installation instructions (reinstate the LoadModule directive, add a DirectoryIndex directive, set up the PHP MIME type handler;
- Create a
php.inifile as per the PHP installation instructions and place it in${TARGET_APACHE}. Enable the OCI8 extension within the file and ensure that theextensions_dirdirective is correctly set. The directory ought to be somewhere beneath${TARGET_PHP}; - Start up Apache and check the error logs for any problems. Check the output from phpinfo() to verify that OCI8 support is enabled.
Finishing off, it remains to create a bootup script for Apache and to ensure that it sets up the web server process with access to ORACLE-related environment variables. If these are absent, there will be difficulties with successfully connecting and working with the database within PHP scripts. This includes variables such as LD_LIBRARY_PATH, TNS_ADMIN and NLS_LANG.
If all has gone well, it should now be possible to try out some of the sample PHP scripts available on PHP.net to test connectivity and get them talking to the database.
