Installing PHP (Under Linux) - Page 7

January 3, 2002

As mentioned above, PHP is not really a program in and of itself. Instead, it's a plug-in module for your Web server (probably Apache). There are actually three ways to install the PHP plug-in for Apache:

  • As a CGI program that Apache runs every time it needs to process a PHP-enhanced Web page.
  • As an Apache module compiled right into the Apache program.
  • As an Apache module loaded by Apache each time it starts up.

The first option is the easiest to install and set up, but it requires Apache to launch PHP as a program on your computer every time a PHP page is requested. This can really slow down the response time of your Web server, especially if more than one request needs to be processed at a time.

The second and third options are almost identical in terms of performance, but since you‘re likely to have Apache installed already, you'd probably prefer to avoid having to download, recompile, and reinstall it from scratch. For this reason, we'll use the third option.

To start, download the PHP Source Code package from (or one of its mirrors listed at At the time of this writing, PHP 4.x has become well-established as the version of choice; however, some old servers still use PHP 3.x (usually because nobody has bothered to update it). I'll be covering the installation of PHP4 here, so be aware that if you still work with PHP3 there may be some minor differences.

The file you downloaded should be called php- version.tar.gz. To begin, we'll extract the files it contains:

% tar xfz php-version.tar.gz
% cd php-version

To install PHP as a loadable Apache module, you'll need the Apache apxs program. This comes with most versions of Apache, but if you're using the copy that was installed with your distribution of Linux, you may need to install the Apache development RPM package to access Apache apxs. You should be able to install this package by whatever means your software distribution provides. By default, RedHat and Mandrake will install the program as /usr/sbin/apxs, so if you see this file, you know it's installed.

For the rest of the install procedure, you'll need to be logged in as the root user so you can make changes to the Apache configuration files.

The next step is to configure the PHP installation program by telling it which options you want to enable, and where it should find the programs it needs to know about (like Apache and MySQL). Unless you know exactly what you're doing, simply type the command like this (all on one line):

% ./configure

Again, check for any error messages and install any files it identifies as missing. On Mandrake 8.0, for example, it complained that the 'lex' command wasn't found. I searched for 'lex' in the Mandrake package list and it came up with 'flex', which it described as a program for matching patterns of text used in many programs' build processes. Once that was installed, the configuration process went without a hitch. After you watch several screens of tests scroll by, you'll be returned to the command prompt. The following two commands will compile and then install PHP. Take a coffee break: this will take some time.

% make
% make install

PHP is now installed in /usr/local/php (unless you specified a different directory with the --prefix option of the configure script above) and it'll expect to find its configuration file, named php.ini, in the lib subdirectory (unless you specified a different directory with the -- with-config-file-path option of the configure script above). PHP comes with two sample php.ini files called php.ini-dist and php.ini- optimized. Copy these files from your installation work directory to the directory in which PHP expects to find its php.ini file, then make a copy of the php.ini- dist file and call it php.ini:

% cp php.ini* /usr/local/php/lib/
% cd /usr/local/php/lib
% cp php.ini-dist php.ini

You may now delete the directory from which you compiled PHP - it's no longer needed.

We'll worry about fine-tuning php.ini shortly. For now, we need to make sure Apache knows where to find PHP, so that it can load the program when it starts up. Open your Apache httpd.conf configuration file (usually /etc/httpd/conf/httpd.conf if you're using your Linux distribution's copy of Apache) in your favorite text editor. Look for a line that looks like this:

LoadModule php4_module lib/apache/

Find the new, uncommented line (no # at the start of the line), not the old line that you may have commented out earlier. It may not appear along with the other LoadModule lines in the file. Once you find it, you might need to change the path to match all the other LoadModule lines in the file. Under RedHat Linux, this means you'll have to change the line to make it look like this:

LoadModule php4_module modules/

PHP will probably run correctly without this change, but on older versions of RedHat, Apache won't be able to find the file until you make this change. If you prefer, leave the line alone for now -- you can come back and change it if you run into trouble.

Next, look for the line that begins with DirectoryIndex. In recent distributions, this may be in a separate file called commonhttpd.conf. This line tells Apache what filenames to use when it looks for the default page for a given directory. You'll see the usual index.html and so forth, but you need to add index.php, index.php3, and index.phtml to that list if they're not there already:

DirectoryIndex index.html ... index.php index.phtml index.php3

Finally, go right to the bottom of the file (again, this should go in commonhttpd.conf if you have such a file) and add these lines, to tell Apache which file extensions should be seen as PHP files:

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

That should do it! Save your changes and restart your Apache server. If all things go to plan, Apache should start up without any error messages. If you run into any trouble, the helpful folks in the Forums (myself included) will be happy to help.

The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers