Running Apache2 With PHP5 And PHP4 At The Same Time - Page 2

2 Setup For Ubuntu 5.10 (Breezy Badger)

This setup does not differ much from the one for Debian Sarge.

First, you must at least have a working basic Ubuntu 5.10 installation, as described on the first two pages of the "Perfect Setup" tutorial for Ubuntu 5.10 "Breezy Badger" here on HowtoForge: and

The official Ubuntu repositories contain packages for both PHP5 and PHP4 so we don't have to use third-party repositories. However, make sure /etc/apt/sources.list looks like this:

deb breezy main restricted
deb-src breezy main restricted

deb breezy-updates main restricted
deb-src breezy-updates main restricted

deb breezy universe
deb-src breezy universe

deb breezy-security main restricted
deb-src breezy-security main restricted

deb breezy-security universe
deb-src breezy-security universe


apt-get update

after you've changed /etc/apt/sources.list.

Now we can install Apache2 as well as the Apache2-PHP5 module and the PHP4-CGI:

apt-get install apache2 apache2-common apache2-doc apache2-mpm-prefork apache2-utils libapr0 libexpat1 ssl-cert
apt-get install libapache2-mod-php5 php4-cgi

Next, we can install all the PHP5 and PHP4 modules we need on our server, like this:

apt-get install php5-imap php5-syck php-pear php5-curl php5-dev php5-gd php5-ldap php5-mhash php5-mysql php5-odbc php5-recode php5-snmp php5-sqlite php5-xmlrpc php5-xsl php4-auth-pam php4-imagick php4-mcrypt php4-rrdtool php4-sqlite php4-curl php4-domxml php4-gd php4-imap php4-ldap php4-mcal php4-mhash php4-mysql php4-odbc php4-pear php4-xslt (1 line!)

Edit /etc/apache2/apache2.conf. Change

DirectoryIndex index.html index.cgi index.php index.xhtml


DirectoryIndex index.html index.htm index.shtml index.cgi index.php index.php4 index.xhtml

and add these two lines as well (in the section where you can find AddType and AddHandler directives):

AddHandler php-script .php4
Action php-script /cgi-bin/php4

This will make sure that PHP files with the extension .php4 are executed as CGI. PHP files with the extension .php will be run by the Apache2-PHP5 module.

Next, we have to enable a few Apache modules, like for example the actions module (otherwise we cannot run PHP4 as CGI):

a2enmod actions
a2enmod ssl
a2enmod rewrite
a2enmod suexec
a2enmod include

Restart Apache, and you're done!

/etc/init.d/apache2 restart

Now we can test our installation: Put two files, info.php and info.php4, with the same content:


into /var/www (this is the default document root of Ubuntu's Apache2) and try to access them in a browser (if the server's IP address is, for example, you would type and You should see two different PHP versions, one run as Apache module (PHP5), the other one (PHP4) called as CGI:




Share this page:

6 Comment(s)

Add comment


From: Anonymous at: 2006-02-09 19:21:20

I have installed Debian Sarge 3.1 according your "perfect setup" including ISPConfig. If i like to extend this "perfect setup" with your how to "Running Apache2 With PHP5 And PHP4 At The Same Time" everything will work or not?

From: Anonymous at: 2006-02-19 09:01:19

Check this out ->

From: Anonymous at: 2006-02-27 09:02:10

My recomendation would be better to use fastcgi feature of PHP as it would greatly reduce system overhead of loading and running instance of php executable for every request,
and it would reduce overhead of database connection too with this setup off course.

Only what should be known is "Basic" authentication is problematic but that can be overcomed too.

boris [d0t] manojlovic [@t] gmail [d0t] com

From: PAL at: 2012-06-19 21:29:56

The concept is simple. You can run identical software on a computer as long as their name/IDs are unique. In the case of IP products (web servers, etc.) the additional burden is to have a unique IP/Port combination. To run PHP4 and PHP5 on the same computer just compile Apache with the prefix (e.g. --prefix=/usr/local/apache4) and the PHP5 as /usr/local/apache5) (feel free to use your own folder names). Thereafter when compiling PHP do the same. You can manually copy the PHP4 and PHP5 files to the respective modules folder. Rename each PHP CLI to php4 and php5 and copy the php4 or php5 cli to just php for programs that need a default php. You decided which is gonna be your default php. I use php4 myscript.php when I want the php4 cli. Change the listen ports to 80 and 81 (for example), respectively, if you have only one IP. If you have more than one (IP) change the Listen to each respective IPs port e.g. Listen Works for me in CentOS (over a year). No guarantees. I have been developing software since 1980 :)

From: Anonymous at: 2006-07-13 18:29:02

Just wanted to say great tutorial, Falko. Keep up the great work! Also, if anyone wants to *keep* their .php files working with PHP 4, and have .php5 files work with PHP 5, you can use the following in your apache.conf file:

AddType application/x-httpd-php .php5

AddHandler php-script .php
Action php-script /cgi-bin/php4


Jay Pipes (jay at mysql dot com)

From: Anonymous at: 2009-12-14 19:10:25

I tried out the method outlined here, but I get a 404 not found with this error message:

The requested URL /cgi-bin/php4/test.php4 was not found on this server.