Building PHP on Linux/Unix with Apache

In this section, we will look at one way of installing PHP with Apache on Linux/Unix. The process is more or less the same for any Unix operating system. While you might be able to find pre-built versions of PHP for your system, compiling PHP from the source gives you greater control over the features built into your binary.

To download the PHP distribution files, go to the home of PHP,, and follow the link to the Downloads section. Grab the latest version of the source code?for this example, we are using 4.2.3. Your distribution will be named something similar to php-version.tar.gz, where version is the most recent release number. This archive will be a compressed tar file, so you will need to unpack it:

#> tar -xvzf php- version.tar.gz

Keep the downloaded file in a directory reserved for source files, such as /usr/src/ or /usr/local/src/. After your distribution is unpacked, you should move to the PHP distribution directory:

#> cd php-version

Within your distribution directory you will find a script called configure. This script accepts additional information that is provided when the configure script is run from the command line. These "command-line arguments" will control the features that PHP will support. For this example, we will include the basic options you need to use to install PHP with Apache and MySQL support. We will discuss some of the available configure options later in the hour.

#> ./configure  --prefix=/usr/local/php \
--with-mysql=/usr/local/bin/mysql \

Once the configure script has run, you will be returned to the prompt after receiving several informational notes from the PHP Group:

|                        > WARNING >                             |
|                                                                    |
| Apache 2 Support is EXPERIMENTAL and should NOT be used in         |
| production environment. Before submitting bug reports, try the     |
| latest CVS snapshot from                      |
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
|                          > NOTE >                              |
|            The default for register_globals is now OFF!            |
|                                                                    |
| If your application relies on register_globals being ON, you       |
| should explicitly set it to on in your php.ini file.               |
| Note that you are strongly encouraged to read                      |
|          |
| about the implications of having register_globals set to on, and   |
| avoid using it if possible.                                        |


Depending on the PHP version you install, you may or may not receive the warning regarding the status of Apache 2 support. As of this writing, Apache 2 and PHP work splendidly together for all functionality in this book. However, you should run your own tests to determine whether you wish to use these versions in production.

Next, issue the make command, followed by the make install command. These commands should end the process of PHP compilation and installation and return you to your prompt.

You will need to ensure that two very important files are copied to their correct locations. First, issue the following command to copy the distributed version of php.ini to its default location. You will learn more about php.ini later in this hour.

#> cp php.ini-dist /usr/local/php/lib/php.ini

Next, copy the PHP shared object file to its proper place in the Apache installation directory, if it has not already been placed there by the installation process:

#> cp libs/ /usr/local/apache2/modules/

You should now be able to configure and run Apache, but let's cover some additional configuration options before heading on to the "Integrating PHP with Apache on Linux/Unix" section.

Additional Configuration Options

When we ran the configure script, we included some command-line arguments that determined some features that the PHP engine will include. The configure script itself gives you a list of available options, including the ones we used. From the PHP distribution directory, type the following:

#> ./configure --help

This produces a long list, so you might want to add it to a file and read it at your leisure:

#> ./configure --help > configoptions.txt

If you discover additional functionality you wish to add to PHP after it has been installed, simply run the configuration and build process again. Doing so will create a new version of and place it in the Apache directory structure. All you have to do is restart Apache in order for the new file to be loaded.

Integrating PHP with Apache on Linux/Unix

To ensure that PHP and Apache get along with one another, you need to check for?and potentially add?a few items to the httpd.conf configuration file. First, look for a line like the following:

LoadModule php4_module        modules/

If this line is not present, or only appears with a pound sign (#) at the beginning of the line, you must add the line or remove the #. This line tells Apache to use the PHP shared object file that was created by the PHP build process (

Next, look for this section:

# AddType allows you to add to or override the MIME configuration
# file mime.types for specific file types.

Add the following lines:

AddType application/x-httpd-php .php .phtml .html
AddType application/x-httpd-php-source .phps

This ensures that the PHP engine will parse files that end with the .php, .phtml, and .html extensions. Your selection of filenames may differ, and you may wish to add .php3 as an extension, for backwards compatibility with any very old scripts you may have.

Any files with the .phps extension will be output as PHP source (that is, the source code will be converted to HTML and color-coded). This can be useful for debugging your scripts.

Save this file, and then restart Apache. When you look in your error_log, you should see something like the following line:

[Sun Sep 29 10:42:47 2002] [notice] Apache/2.0.43 (Unix) PHP/4.2.3 configured

PHP is now part of the Apache Web server. If you want to learn how to install PHP on a Windows platform, keep reading. Otherwise, you can skip ahead to the "Testing Your Installation" section.

    Part III: Getting Involved with the Code