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-like operating system. Although you might be able to find prebuilt 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, http://www.php.net/, and follow the link to the Downloads section. Grab the latest version of the source codefor this example, we are using 5.1.4. 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:
# gunzip < php-VERSION.tar.gz | tar xvf
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
In 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 control the features that PHP will support. In this example, we will include the basic options you need to install PHP with Apache and MySQL support. We will discuss some of the available configure options later in the chapter, and throughout the book as they become relevant.
# ./configure --prefix=/usr/local/php \ --with-mysqli=/usr/local/mysql/bin/mysql_config \ --with-apxs2=/usr/local/apache2/bin/apxs
After the configure script has run, you will be returned to the prompt, such as
... Generating files updating cache ./config.cache creating ./config.status creating php5.spec creating main/build-defs.h creating scripts/phpize creating scripts/man1/phpize.1 creating scripts/php-config creating scripts/man1/php-config.1 creating sapi/cli/php.1 creating main/php_config.h creating main/internal_functions.c creating main/internal_functions_cli.c +--------------------------------------------------------------------+ | 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. | +--------------------------------------------------------------------+ Thank you for using PHP. #
From the prompt, 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:
... chmod 755 /usr/local/apache2/modules/libphp5.so [activating module `php5' in /usr/local/apache2/conf/httpd.conf] Installing PHP CLI binary: /usr/local/bin/ Installing PHP CLI man page: /usr/local/man/man1/ Installing build environment: /usr/local/lib/php/build/ Installing header files: /usr/local/include/php/ Installing helper programs: /usr/local/bin/ program: phpize program: php-config Installing man pages: /usr/local/man/man1/ page: phpize.1 page: php-config.1 #
You will need to ensure that two very important files are copied to their correct locations. First, issue the following command to copy the recommended version of php.ini to its default location. You will learn more about php.ini later in this chapter.
# cp php.ini-recommended /usr/local/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 (it usually will be, as you can see in the make install output, just shown):
# cp libs/libphp5.so /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 Linux/UNIX Configuration Options
In the previous section, when we ran the PHP 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 command 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 want 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 libphp5.so and place it in the Apache directory structure. All you have to do is restart Apache to load the new file.
Integrating PHP with Apache on Linux/UNIX
To ensure that PHP and Apache get along with one another, you need to check forand potentially adda few items to the httpd.conf configuration file. First, look for a line like the following:
LoadModule php5_module modules/libphp5.so
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 (libphp5.so).
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 line to that section:
AddType application/x-httpd-php .php .html
This statement ensures that the PHP engine will parse files that end with the .php and .html extensions. Your selection of filenames might differ.
Save this file and then restart Apache. When you look in your error_log, you should see something like the following line:
[Thu May 12 10:42:47 2006] [notice] Apache/2.0.58 (Unix) PHP/5.1.4 configured
PHP is now part of the Apache web server. If you want to learn how to install PHP on a Mac OS X platform, keep reading. Otherwise, you can skip ahead to the "Testing Your Installation" section.