[ Team LiB ] Previous Section Next Section

Installing PHP for Linux and Apache

In this section, we will look at one way of installing PHP with Apache on Linux. The process is more or less the same for any Unix operating system. You might be able to find prebuilt versions of PHP for your system, which are simple to install.

Compiling PHP, though, gives you greater control over the features built in to your binary.

Before you install, you should ensure that you are logged in to your system as the root user. If you are not allowed access to your system's root account, you might need to ask your system administrator to install PHP for you.

There are two ways of compiling an Apache PHP module. You can either recompile Apache, statically linking PHP into it, or you can compile PHP as a dynamic shared object (DSO). If your version of Apache was compiled with DSO support, it is capable of supporting new modules without the need for recompiling the server. This method is the easiest way to get PHP up and running, and it is the one we cover in this section.

To test that Apache supports DSOs, you should launch the Apache binary (httpd) with the -1 argument, like so:

/usr/local/apache/bin/httpd -1

Where Is Apache?


httpd, the Apache application, can be installed in different places on a system. One standard location is /usr/local/apache/bin/httpd, but you may find that it is somewhere else on your server. If it has been placed in your path, you may not even have to use the full path in order to invoke the application. You would then be able call apache like this:

httpd -1

You should see a list of modules. If you see


among them, you should be able to proceed; otherwise, you might need to recompile Apache. The Apache distribution contains full instructions for this.

Compile Apache with DSO Support


If you do install Apache, remember to ensure that you compile DSO support in. You can do this by passing --enable-module=so to the configure script, like this:

./configure --enable-module=so

If you have not already done so, you need to download the latest distribution of PHP (PHP 5.0.0b1 at the time of writing). Your distribution will be archived as a tar file and compressed with gzip, so you will need to unpack it:

tar -xvzf php-5.0.0b1.tar.gz

After your distribution is unpacked, you should move to the PHP distribution directory:

cd php-5.0.0b1

Within your distribution directory you will find a script called configure. This accepts additional information that should be provided when the configure script is run from the command line. These command-line arguments control the features that PHP supports. For this example, we will include some useful command-line arguments, although you might want to specify arguments of your own. We will discuss some of the configure options available to you later in the hour:

./configure \
  --prefix=/home/usr/local/php5/ \
  --with-mysql \
  --with-apxs=/usr/local/apache/bin/apxs \
  --with-xsl \
  --with-gdbm \
  --with-gd \
  --with-freetype=/usr/include/freetype/ \
  --with-zlib-dir=/usr/include \
  --with-ttf \

Installing PHP with Apache 2


It is currently recommended that you run PHP with Apache 1.3 rather than Apache 2. However, you can find full instructions for installing PHP with Apache 2 at http://www.php.net/manual/en/install.apache2.php. The main installation difference lies in a flag to the configure script. You should use --with-apxs2 rather than --with-apxs.

The directives chosen in this example are designed to support the features discussed in this book. Most of them require that your system has certain libraries installed before you can compile PHP. The configure script will complain if the relevant libraries cannot be located.

Of these configure options, the one that is absolutely essential is --with-apxs because it associates PHP with your server. The argument you use depends on the location of Apache on your server. If you are running Linux and are not sure where to find Apache, try running the locate command at the command line, like so:

locate apxs

It lists all the paths on your system that contain the string apxs.

After the configure script has run, you can run the make program. You need a C compiler on your system to run this command successfully:

make install

These commands should end the process of PHP compilation and installation. You should now be able to configure and run Apache.

    [ Team LiB ] Previous Section Next Section