How To run php4 in paralel with php5 on Ubuntu 11

Recently we've had to upgrade few eZ Publish sites from an older 3.9 version. Since we wanted to use ezupgrade for this then we've got to make php4 available in order to complete the upgrade from 3.9.x to 3.10.1.

To solve the problem of having both php4 and php5 running on the same PC (running Ubuntu 11) I choose the simplest solution: keep the default apache2 + php5 packages and add php4 by cgi (aslo cli will be available). Then I will configure a new virtual host php4-cgi that will parse the php files using php4 by cgi.

So, my goal was: when I want to test a info.php script that is on /var/www/ using PHP4 I will access it as http://php4-cgi/info.php and when I want to test it on PHP5 I access http://localhost/info.php

I assume you already have apache + php5 installed and running so in this post we focus only on adding PHP4 in the picture.

PHP4 is not anymore available as a package so the only way is to get it as source and compile it. So, before everything else we need to create a binary for PHP4 cli & cgi from source.

1. Download source of latest php4 from here

Copy the archive into /tmp and unpack with

# tar xfvz php-4.4.9.tar.gz 

then cd into the new created php-4.4.9 directory.

2. run # ./configure --with-mysql  (I userd minimal configuration so I don't have to install a lot of dev packages, we want to have php4 only good for the upgrade)

For me this failed at first because the required flex binary was not found which was because the flex package was not installed. So, to correct I've run

# sudo apt-get install flex

and then again the step 2. Depending on your system you may have other packages that need to be installed. Look at the error and then use

# apt-cache search xxx

to find clues about what packages you need to install.

3. after you've successfully run the ./configure step then you run

# make

This will compile the binaries of php4 cli and cgi. You find these binaries in sapi/cli and sapi/cgi subfolders.

4. Test the binaries. I've used the following commands to test if both of then are working (being in /tmp/php-4.4.9 folder)

# sapi/cli/php -v

# sapi/cgi/php -v

5. Install the binaries. Since the above commands run succesfully next I've copied the new binaries into the system folders.

# sudo cp sapi/cli/php /usr/bin/php4

#sudo cp sapi/cgi/php /usr/lib/cgi-bin/php4

6. The last step is to create a new virtual host that will run the php files through php4 by cgi.

For this I've choose to create a host name php4-cgi that will point to the IP and a virtual host that will point to the same /var/www on my laptop. As you can see I've used a different IP address than localhost ( In my (brief)tests if I use the same IP address for both host names then apache will not load the correct handler.

# sudo nano /etc/hosts

add this line: php4-cgi 

Then # cd /etc/apache2/sites-available

and create a copy of existing default file.

# sudo cp default php4-cgi

Then edit the file

# sudo nano php4-cgi

Change <VirtualHost *:80>  into <VirtualHost php4-cgi:80>

Then add inside <VirtualHost> at the top

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        AddHandler application/x-httpd-php4 php
        Action application/x-httpd-php4 /cgi-bin/php4

       <Files  *.php>
          SetHandler application/x-httpd-php4

The ScriptAlias line already exists somewhere below in the file, I moved it with this group for clarity. So, locate the duplicated line and delete it (F9 in nano will cut the line).

Save the file and enable the new virtual host with

# sudo a2ensite php4-cgi

Enable the mod_actions module (on my laptop was not enabled so the apache failed to start) with

# sudo a2enmod actions

Then restart apache

# sudo apachectl restart

Now you should be able to access your php files through PHP4 using http://php4-cgi/ and through PHP5 using http://localhost.

For me, the best way to test was to create a simple php file info.php and put inside <?php phpinfo(); ?> the access it using the above hostnames.


In case of troubles check:

 - does the hostname respond to ping with the proper IP?

# ping php4-cgi

must repond as

 - restart the browser (especially Firefox which caches hostnames) and eventually clear its history

 - check the virtual host file to have everything I mention above in place