Installation and Configuration - PHP Pocket Reference, 2nd Edition (2009)

PHP Pocket Reference, 2nd Edition (2009)

Installation and Configuration

PHP works with many different web servers in many different ways, but by far the most popular way to run PHP is as an Apache module with Apache 1.3.x. Full installation instructions for all the different ways to install PHP can be found in the PHP documentation. Here, I cover the Apache module installation.

If you are compiling from the PHP source tarball, follow the instructions in the INSTALL file found inside the PHP distribution file. A tarball is a compressed tar file. tar stands for tape archive, but these days it has little to do with tapes. It is simply a way to lump multiple files and directories into a single file for distribution. Normally tarballs have the .tar.gz extension to indicate a tar file compressed with gzip. To untar a tarball, use:

tar zxvf foo.tar.gz

On Windows, many utilities (including WinZip) understand tarballs.

If you are installing from a precompiled binary package such as an rpm file, most of the work should be done for you. But doublecheck that the Apache configuration described below is correct.

When you are using PHP as an Apache module, PHP processing is triggered by a special MIME type. This is defined in the Apache configuration file with a line similar to:

AddType application/x-httpd-php .php

This line tells Apache to treat all files that end with the .php extension as PHP files, which means that any file with that extension is parsed for PHP tags. The actual extension is completely arbitrary and you are free to change it to whatever you wish to use.

If you are running PHP as a dynamic shared object (DSO) module, you also need this line in your Apache configuration file:

LoadModule php4_module modules/libphp4.so

Note that in many default httpd.conf files you will find AddModule lines. These really aren't necessary. They are only needed if you have a ClearModuleList directive somewhere in your httpd.conf file. I would suggest simply deleting the ClearModuleList directive and deleting all your AddModulelines. The idea behind ClearModuleList/AddModule is to make it possible to reorder already loaded modules in case module order is an issue. With most modules, the order that they are loaded -- which governs the order they are called -- is not important. And further, most binary distributions of Apache ship with most modules compiled as dynamically loadable modules, which means that if order is an issue for some reason, you can simply change the order of the LoadModule calls to fix it.

Don't forget to restart your server after making changes to your httpd.conf file. Once the server is restarted, you can check to see if PHP is working by creating a file in your document root named info.php containing the single line:

<?php phpinfo( )?>

Load this up in your browser using http://your.domain.com/info.php. You should see all sorts of information about PHP. If you don't see anything, try selecting "View Source" in your browser. If you see the phpinfo( ) line, you probably forgot (or mistyped) the AddType line in your httpd.conffile. If the browser tries to download the file instead, it means that the AddType is there, but the PHP module is not being triggered -- perhaps because you forgot the LoadModule line.

Once you have verified that PHP is working, have a look at the PHP initialization file called php.ini. The phpinfo( ) page will tell you where PHP is expecting to find it. PHP functions fine without this file, but with all the default settings. If you want to change the defaults, or perhaps more importantly, you want to be immune from any changes to the defaults when you upgrade, you should create a php.ini file. The source distribution of PHP comes with a php.ini-dist file that you can rename and copy into the location specified in the phpinfo( ) output. The php.ini file itself is well-commented and self-explanatory for the most part.

You can also put configuration directives inside the Apache httpd.conf file, and, in certain cases, in individual .htaccess files. This is very useful for setting things per-directory or per-virtual host. If you have this line in the php.ini file:

include_path = ".:/usr/local/lib/php:.."

you can set this in your httpd.conf file with:

php_value include_path .:/usr/local/lib/php:..

There are four httpd.conf directives used for setting PHP directives:

php_value

For setting normal strings and values

php_flag

For setting boolean values

php_admin_value

For setting administrative values

php_admin_flag

For setting boolean administrative values

In addition, the normal values and booleans can be set in your .htaccess files, but only if the Apache AllowOverride setting (which sets what is allowed in a .htaccess file) includes "Options".

More information can be found at http://www.php.net/configuration.