This section explains how to install PHP 4 (versions later than 4.3) or PHP 5 as a shared module in Apache 2:
Once you have installed Apache, you can install the shared PHP module as part of the web server. Login in as the root user.
Get the latest version of PHP from http://www.php.net/downloads.php. Download the "Complete Source Code" version into the /tmp directory. Do not download any version earlier than PHP 4.3.0, as this will not work with the latest version of Apache 2. This book uses PHP 4.3.3 and PHP 5.0.0 beta, and we recommend installing the latest stable version at the time of writing.
Choose an installation directory. If the Apache installation was begun in /usr/local/, the same location can also be used for PHP. Move the PHP source code file to the base directory of the desired installation. Assuming this is /usr/local/src and, assuming the distribution downloaded is PHP 4.3.3 and it was downloaded into the /tmp directory, the command is:
% mv php-4.3.3.tar.gz /usr/local/src
After moving the distribution to the desired location, change directory to that location using:
% cd /usr/local/src
Uncompress the package in the new installation directory by running:
% gzip -d php-version_number.tar.gz
If the version downloaded is PHP 4.3.3, the command is:
% gzip -d php-4.3.3.tar.gz
Un-tar the distribution by running:
% tar xvf php-version_number.tar
A list of files extracted is displayed. If the version downloaded is PHP 4.3.3, the command is:
% tar xvf php-4.3.3.tar
Change directory to the PHP installation:
% cd php-version_number
If the version is PHP 4.3.3, type:
% cd php-4.3.3
Configure the PHP installation by running the configure script. This detects the available tools and the installation environment, adds MySQL support, and prepares for Apache 2 integration. It assumes that MySQL has been installed previously in the directory /usr/local/mysql and that Apache 2 has been installed in /usr/local/apache2.
If you've installed MySQL 4.0, then use the following command:
% ./configure --with-apxs2=/usr/local/apache2/bin/apxs \ --with-mysql=/usr/local/mysql/
If you've installed MySQL 4.1, then the regular PHP MySQL library won't work at the time of writing. Instead, you need to use the improved MySQL library that's discussed in Appendix H (and you need to be aware that the functions described in Chapter 6 are replaced by new functions discussed in Appendix H). To use the improved library, type:
% ./configure --with-apxs2=/usr/local/apache2/bin/apxs \ --without-mysql --with-mysqli=/usr/local/mysql/bin/mysql_config
A common problem that occurs with PHP 5 is that the script complains that you need a newer version of libxml2. If this occurs, visit ftp://xmlsoft.org/ and obtain and install the latest version of libxml2. The installation process is straightforward: choose the .tar.gz version, extract it using the gzip and tar utilities, and then run ./configure, make, and make install.
Compile the PHP scripting engine by running:
Now that the PHP scripting engine is built, install the PHP engine using:
% make install
The PHP installation is almost complete. Now copy across the default PHP configuration file to the default location, This file, php.ini, contains the settings that control the behavior of PHP and includes, for example, how variables are initialized, how sessions are managed, and what scripting tags can be used. The command to copy the file is:
% cp php.ini-dist /usr/local/lib/php.ini
Edit the Apache configuration file and enable PHP script engine support for files that have the suffix .php. To do this, edit the file /usr/local/apache2/conf/httpd.conf and add the following line to the AddType section (at approximately line 850):
AddType application/x-httpd-php .php
For PHP 5 installs, check that the following line has automatically been added to the file:
LoadModule php5_module modules/libphp5.so
If the line is not present, add it.
For PHP 4 installs, check that the following line has automatically been added to the file:
LoadModule php4_module modules/libphp4.so
If the line is not present, add it. Save the file and exit the editor.
Restart the Apache web server by running the command
% /usr/local/apache2/bin/apachectl restart
To test the PHP module, change the directory to the Apache document root:
% cd /usr/local/apache2/htdocs
Create a file with the name phpinfo.php using a text editor. In the file, type the following, save the script, and exit the editor:
<?php phpinfo( ); ?>
Test the newly created PHP script by retrieving with a browser the following URL http://127.0.0.1/phpinfo.php. You should see a page full of information.
Apache is now running and serving both static HTML and PHP scripts. The installation of Apache, PHP, and MySQL is now complete, and you've also successfully installed PEAR, as it's now a default component of PHP.