Purpose of document:

Describe build and installation procedure of PHP5.x on older versions of Linux. In this example, Red Hat Enterprise Linux 2.1 or CentOS 2.1. If you haven't any version of PHP installed, this is probably not the document for you. You probably need to instead learn how to use your Linux distribution's package management system using documentation written for your version. If you have come here just to learn how to install Joomla! or Mambo, you are probably reading the wrong document. If you have an older version of GNU/Linux, and you know the version of PHP is too old for your needs, AND you need to avoid disturbing other programs, this document might help.


Many systems may have dozens, or even hundreds of web-enabled applications running atop Linux, Apache, MySQL, Perl & PHP across perhaps dozens of domain names, These web-enabled applications may be custom content management systems, business logic infrastructure (EG CRM, billing, specialised shops). In fact, any specialised database-driven or code-driven web site. Moving these systems to an updated platform can be very time consuming, requiring not only changes to the web-enabled applications, but extensive testing.

The lowest cost option is to maintain such a system for as long as possible without introducing incompatible changes. Red Hat Enterprise Linux fits the bill with a support cycle of 7 years. RHEL 2.1 maintenance cycle runs until 31st May 2009. For web applications, the chief value of the maintenance cycle is the automated security updates. These keep the server patched against security flaws as they are detected.

From the above, it should be clear why updating the whole system, or major parts of the system is potentially dangerous and uneconomical. When the support cycle for your distribution is nearly over, it may be time to begin the costly and time consuming process of migrating the web applications to a new install. In the meantime, customers may demand enhanced functionality through off-the-shelf content management systems such as Joomla!, Mambo or Mediawiki, or use a modern content management system as a migration path from legacy code running on legacy systems. Opening the option of installing a recent version of PHP on a legacy system, and thereby opening a migration path is therefore of great value. Another point of value is extending the useful life of an installed system and associated hardware, and possibly avoiding the need to run concurrent systems until switch-over is required.

This document describes a process I have used to install PHP5 where for me, it only affects designated areas on the machine. Everything else has remained unaffected. [Please note: This information is given in good faith with the hope it will be useful. There is absolutely no express or implied warranty. Follow these instructions at your own risk!]

This document will concentrate on PHP suitable for running Joomla 1.0.12. Joomla will run with MySQL shipped with RHEL 2.1. If you want to run the latest mediawiki, you will need to run a recent version of MySQL as well as a recent version of PHP. A later document may include instructions on how to do this without affecting your running web applications. As always, make sure you have a working backup in case of disaster. When logged in as root, remember that it only takes a few wrongly chosen characters typed on the console to irretrievably wipe everything. Also remember that it is not the case of IF the hard drive will fail. It is the case of WHEN.


I will assume the reader is comfortable using the command line in Linux, has some knowledge of the Red Hat package management system, knows how to use up2date, and if errors appear in the build process, will know how to download and install necessary development files through the package management system. I will assume the reader has sufficient familiarity with apache configuration syntax to employ the configuration examples. I will also assume the user's apache server uses suexec. suexec enables each virtualhost on a web server to run as a different user. This is standard on many (most?) RHEL virtualhost systems eg those deployed by Rackspace with Plesk. SUexec is is extremely useful in mass-hosting environments, but introduces some security restrictions, so I will show a work-around. I will also assume you have the following development packages installed in addition to the standard advanced server packages (you can install them on rhel using up2date <packagename> or on centos using yum install <packagename>). For libxml2: gcc ; For PHP5: flex zlib-devel libjpeg-devel libpng-devel mysql-devel gcc-c++


Download LibXML2
Download PHP source
Configure, compile and install LibXML2
Configure, compile and install PHP using downloaded & compiled LibXML2
Compile & Install PHP into special location
Compile PHP wrapper and install in web host location
Add configuration directives to Apache to use new PHP

Download LibXML2

Grab a copy of Libxml2 from http://xmlsoft.org/

For example (as a user, not root)

cd /tmp
wget ftp://xmlsoft.org/libxml2/libxml2-2.6.27.tar.gz
tar -zxf libxml2-2.6.27.tar.gz

Download PHP source

Grab a copy of PHP 5.x from http://www.php.net/

For example (as a user, not root)

cd /tmp
wget http://uk3.php.net/get/php-5.2.1.tar.gz/from/uk.php.net/mirror
tar -zxf php-5.2.1.tar.gz

Configure, compile and install LibXML2

We'll put the entire built libxml2 in it's own directory, out of the way, so it doesn't interfere with the LibXML already on the system. (The system's own LibXML is version 1 and is incompatible with PHP5. We'll be telling PHP to use the LibXML in the special location and ignore LibXML on the system. For example (as a user, not root)
cd /tmp
cd libxml2-2.6.27
./configure --without-python --prefix=/usr/local/libxml2
su <enter root password>
make install
Assuming there were no errors, we should now have a compiled libxml2 installed in /usr/local/libxml2

Configure, compile and install PHP using downloaded & compiled LibXML2

cd /tmp/php-5.2.1
./configure --without-python --prefix=/usr/local/php5 --with-zlib --with-mysql --with-libxml-dir=/usr/local/libxml2/ --with-gd --enable-exif
su <enter root password>
make install
We should now have a compiled version of PHP for CGI only in /usr/local/php5

Compile PHP wrapper and install in web host location

If using suexec with apache, you will need to run a program (which we'll call a wrapper) inside the apache documentroot, owned by the user and group apache is running as, with a directory writable only by that user. The wrapper runs PHP at /usr/local/php5/bin/php which cannot be directly run through suexec.

There are 2 programs given here. The bash option is easy, requires no compilation, but introduces an overhead which may be considerable with a lot of traffic. The C program option creates a tiny, fast wrapper which has almost no overhead.

Bash option:

/usr/local/php5/bin/php $@

Simple eh? Bash launches PHP and passes all parameters to PHP. Save the 2 lines into a file called phpwrapper

Alternatively, C programming option

//----Code starts here
#include <stdio.h>
#include <stdlib.h>
#define EXEC_STRING "/usr/local/php5/bin/php"
#define IMAGE_NAME "php"

int main(int argc, char **argv)
    char **arguments;
    int i,k;
    printf("Wrapper program ret val %d, %s probably not found\n",k,EXEC_STRING);
//----Code ends here

Save the above into a file called phpwrapper.c then do:
gcc -O2 -o phpwrapper phpwrapper.c

This will create a binary called phpwrapper. About 14k in size. This program, or the bash script can be used interchangeably. The binary will be faster, and use less system resources.

Add configuration directives to Apache to use new PHP

Copy phpwrapper into the virtualhost's cgi-bin. chown the file to the user and group the virtualhost runs as, and make sure it is executable.

As a security check, point the web browser to your wrapper script in your CGI-bin. eg http://www.host.name/cgi-bin/phpwrapper. If you see the line from the bash script or a line with ELF near the beginning, you are probably safe as Apache will serve, rather than execute the wrapper script for direct requests. (If you get internal server error, or PHP information, it is unsafe as attackers can execute arbitrary code on your PHP as your user).

Add the following lines to the virtualhost stanza. These tell apache to use the CGI version of PHP instead of any PHP apache module.

ScriptAlias /php5-cgi /home/httpd/vhosts/website.com/cgi-bin/phpwrapper
Action php5-cgi /php5-cgi
AddHandler php5-cgi .php

(Change the path to the actual copy of phpwrapper for the virtualhost).

You should now be able to run Joomla! 1.0.12 on RHEL 2.1 or Centos 2.1.

Note: To use search engine friendly URLs with Joomla/PHP run as CGI, you will need to add special apache directives, which are site-specific and beyond the scope of this document.

If you would like someone to perform the procedure remotely for you, email me nick [at] nickhill [dot] co [dot] uk


Sponsored link: Computer shop in Catford, SE6. Discount computers, parts. Linux, Windows, Mac