by Kevin Yank of SitePoint.com
Installing PHP under Linux
As mentioned above, PHP is not really a program in and of itself. Rather, it is a plug-in module for your Web server (probably Apache). There are actually three ways you can install the PHP plug-in for Apache:
The second and third options are pretty much identical in terms of performance, but since you likely already have Apache installed, you'd probably prefer to avoid downloading, recompiling, and reinstalling it from scratch. For this reason, we'll be using the third option.
Start by downloading the PHP Source package from http://www.php.net/ (or one of its mirrors listed at http://www.php.net/mirrors.php). At the time of this writing, PHP 4.0 was available as "Release Candidate 2"-or "almost ready but not quite". Personally I use PHP 4.0-RC2 and don't have any trouble with it. Since the final version will be out "real soon now" (likely before this series of articles is even finished), I'd recommend you install the latest version of 4.0 so you don't have to change anything when the final version is released. In case you do decide to stick with 3.0, however, I'll be sure to point out any spots in the installation procedure that would differ between the two.
The file you downloaded should be called php-version.tar.gz
. We'll start by extracting 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 by RedHat Linux, you'll need to install the Apache development RPM package to get it. You'll find this package on your RedHat CD or you can download it from http://www.redhat.com/. By default, RedHat will install the program as /usr/sbin/apxs
. If you see that file, you know it's installed.
For the rest of this install procedure, you'll need to be logged in as the root user, because it involves making changes to the Apache configuration files.
The next step is to configure the PHP installation program by letting it know what options you want to have enabled and where it should find the programs it needs to know about (like Apache and MySQL). Unless you know what you're doing, you should just type the command like this (all on one line):
% ./configure
--prefix=/usr/local/php
--with-config-file-path=/usr/local/php
--with-apxs=/usr/sbin/apxs
--enable-track-vars
--enable-magic-quotes
--enable-debugger
If you are installing PHP 3.0 (and not 4.0 or later), you'll also need to tell it where to find MySQL on your system with the following additional parameter:
--with-mysql=/usr/local/mysql/
After watching several screens of tests scroll by, you'll be returned to the command prompt. The following two commands will compile and then install PHP:
% make
% make install
PHP is now installed in /usr/local/php
(unless you specified a different directory with the --prefix
option of ./configure
above), and expects to find its configuration file, named php.ini
, in the same directory (unless you specified a different directory with the --with-config-file-path
option of ./configure
above). PHP comes with a sample php.ini
file called php.ini-optimized
(php.ini-dist
for PHP 3.0). Copy this file from your installation work directory to where it belongs:
% cp php.ini-optimized /usr/local/php/php.ini
Or for PHP 3.0:
% cp php.ini-dist /usr/local/php/php.ini
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 it when starting up. Open your Apache httpd.conf
configuration file (/etc/httpd/conf/httpd.conf
on RedHat Linux) in your favorite text editor. Look for a line like the following:
LoadModule php4_module lib/apache/libphp4.so
If you installed PHP 3.0, the line will read php3
instead of php4
. You're looking for a new, uncommented line (no #
at the start of the line), not the old line that we commented out earlier. Chances are it will not appear along with the other LoadModule
lines in the file. Once you find it, you need to change the path so that it matches all the other LoadModule
lines in the file. Under RedHat Linux, this means changing the line so that it looks like this:
LoadModule php4_module modules/libphp4.so
Next, look for the line starting with DirectoryIndex
. This line tells Apache what filenames to use when looking 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
and index.php3
to that list:
DirectoryIndex index.html index.cgi ... index.php index.php3
Finally, go right to the bottom of the file and add the following line to tell Apache what file extensions should be seen as PHP files:
AddType application/x-httpd-php .phtml .php .php3
That should do it! Save your changes and restart your Apache server. All things going to plan, Apache should start up without any error messages. If you run into any trouble, the helpful folks in the SitePoint.com Forums (myself included) will be happy to help.
Post-Installation Setup Tasks |
SitePoint.com is a fast growing Web Developer Community. Kevin Yank is the Editor of the SitePoint TechTimes, a fresh, technically oriented newsletter for the serious Webmaster. |