Running PHP4 alongside PHP5 in Apache non-intrusively

Here's a non-intrusive method of installing PHP4 so it works with (read: doesn't get in the way of) the LAMP stack provided by your favourite package manager (e.g. apt-get in Debian/Ubuntu and yum in RedHat derivatives), but gives access to both PHP4 and PHP5 in the same Apache configuration.

We won't touch PHP5, but we'll install PHP4 as CGI/FastCGI so it's available at the flip of a switch on a per-VirtualHost basis.

Fetch, build, and install PHP4
$ wget -O php-4.4.9.tar.bz2 http://php.net/get/php-4.4.9.tar.bz2/from/www.php.net/mirror
$ tar xf php-4.4.9.tar.bz2
$ cd php-4.4.9

Here's a basic configure line that should get you going (it'll install PHP4 to /usr/php4). You can either add the additional switches you want to the ./configure line now, or compile the modules in later. You may need to install a few libraries using your favourite package manager (e.g. yum install flex bzip2-devel zlib-devel curl-devel).

$ ./configure \
      --prefix=/usr/php4 \
      --enable-force-cgi-redirect \
      --enable-fastcgi \
      --with-pear \
      --with-png-dir=/usr/local/lib \
      --with-xml \
      --with-zlib \
      --with-zlib-dir=/usr/include \
      --enable-bcmath \
      --enable-calendar \
      --enable-ftp \
      --enable-magic-quotes \
      --enable-sockets \
      --enable-track-vars \
      --enable-mbstring \
      --enable-memory-limit \
      --with-curl

Now the standard make process (replace 4 with the number of concurrent jobs you'd like to run):

$ make -j 4
$ sudo make install
Configure Apache to support PHP4

There are 3 lines that tie everything together, but they need to go in different places:

  1. httpd.conf:

    #
    # Configuration for PHP4 to be used in CGI/FastCGI mode.
    #
    ScriptAlias /php4-script /usr/php4/bin/php
    Action php4-script /php4-script
    
  2. The top of each VirtualHost that is going to run PHP4:

    # Have this VirtualHost use PHP4 instead of the default PHP.
    AddHandler php4-script .php