Cover image for post How to run PHP4 and PHP 5 prallel

How to run PHP4 and PHP 5 prallel

The first 2 versions of PHP 5 had been released and even Beta 3 and the call on pear-dev had to come, until I found time to install a version of PHP 5. One of my most important requirements was to have a copy of PHP 4 and 5 running parallel on 1 maschine. Christian Stocker gave me the important advice to simple run 2 Apache instances with different config files.

This is not a heavy issue to manage, but since i did not find a how-to on it, I would like to provide some information, to make usage of PHP 5 suitable for unexperienced users too.

1. The idea:

The idea is very simple. I wanted to have 2 versions of PHP (4.3.4 and 5.0beta3) running together on 1 maschine, both as Apache 1.3 modules.

2. Compiling PHP:

I had a PHP 4.3.4 running on my devbox and compiled a PHP 5 version in addition. This can be managed in a bloody simple way. Download a copy of PHP 5, unpack the archive to any directory. Copy the './configure' statement from your installed PHP version and exchange/create the prefix option, e.g. '--prefix/usr/php5' and change the '--with-config-file-path' to a different location (e.g. '/etc/php5').

Run the './configure' statement inside the unpack directory using './configure <your options>'. You can get a complete list opf PHP 5 supported option with './configure --help'.

PHP 5 make files had been created successfully (most errors shouls result from version conflicts of libraries and header files, you can easiely solve this by installing or compiling new versions of the required packages/development-packages). Now you should run the 'make' statement and get a coffee, during your first PHP 5 compile...

3. Creating Apache configuration

We want to run 2 different modules with a very similar API definition, so we need 2 different Apache (I used 1.3 branch, but 2.0 should work fine, too) instances. You can achieve that with 2 configuration files.

Easiest way to get started is copiing your current Apache configuration (usually located in '/etc/apache/' or '/etc/httpd/' (some wired distributions even have itt in '/usr/local/httpd/etc/') 'httpd.conf' to 2 new files in the same directory and call them (e.g.) 'httpd.conf.php4' and 'httpd.conf.php5'.

Now start editing the 'httpd.conf.php4' file. You can set up a completely different configuration, if you like, but in this case I will show you only the neccessary changes to divide the 2 Apache instances as clean as possible.

The listed lines should either contain unique or common values (between '.php4' and '.php5' config file), whhat I noted there in comments. Creating the '.php4' file from the '.php5' one or the other way round is more or less a simple replace action.

-- httpd.conf.php4 --

# Unique lock file LockFile /var/lock/apache-php4.lock # Very important for init script # Unique process ID file PidFile /var/run/ # Unique scoreboard file ScoreBoardFile /var/run/apache-php4.scoreboard # Unique port number Listen 80 # Common document root DocumentRoot /var/www # The most important lines in both config files are the following: # For the .php4 file LoadModule php4_module /usr/lib/apache/1.3/ # For the .php5 file # LoadModule php5_module /usr/lib/apache/1.3/

-- httpd.conf.php4 --

You can now try to run those apache instances using the commands

$ apache -f /etc/>apache</httpd.conf.php4 $ apache -f /etc/>apache</httpd.conf.php5

(or the other way around). Maybe your distribution uses another name for the apache binary, such as httpd.

Doing a

$ ps -A

should show you 2 times the number of minimum processes you provided in the httpd.conf.\* files. Open a browser and check the used ports for responses. Here we go, PHP 5 is running. You can easily test your scripts with both PHP versions calling the provided sockets in dirfferent browser-tabs or -windows.

4. Startup (init) for Debian

My GNU/Debian Woody box uses the usual start-up (init) script to run apache during boot process. The process to run 2 Apache instances is the same as for 'httpd.conf' files. Go to your distribution specific init directory (debian uses '/etc/init.d/') and copy the actual startup script (named 'apache' on Debian) into 2 new files (I used 'apache-php4' and 'apache-php5').

The specific configuration changes in this file run parallel to those in 'httpd.conf'. Here is my PHP 5 solution including some comments:

-- apache-php5 --

#! /bin/bash # # apache Start the apache HTTP server. # NAMEapache PATH/bin:/usr/bin:/sbin:/usr/sbin SUEXEC/usr/lib/apache/suexec # Add PHP version specific pid file (set in httpd.conf.\*) PIDFILE/var/run/$ # Your PHP version specififc config file CONF"/etc/apache/httpd.conf.php5" DAEMON/usr/sbin/apache APACHECTL/usr/sbin/apachectl trap "" 1 export LANGC export PATH test -f $DAEMON || exit 0 test -f $APACHECTL || exit 0 # ensure we don't leak environment vars into apachectl APACHECTL"env -i LANG${LANG} PATH${PATH} $APACHECTL" if egrep -q -i "^[[:space:]]\*ServerType[[:space:]]+inet" $CONF then exit 0 fi case "$1" in start) echo -n "Starting web server: $NAME" # Add '-- -f $CONF' to take configuration in respect # This might be a bug in standard Debian ini file here, cause $CONF is defined, # but normaly not used. start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON -- -f $CONF ;; stop) echo -n "Stopping web server: $NAME" # Add '-- -f $CONF' to take configuration in respect start-stop-daemon --stop --pidfile $PIDFILE --oknodo --exec $DAEMON -- -f $CONF ;; reload) echo -n "Reloading $NAME configuration" # Add '-- -f $CONF' to take configuration in respect start-stop-daemon --stop --pidfile $PIDFILE --signal USR1 --exec $DAEMON -- -f $CONF ;; reload-modules) echo -n "Reloading $NAME modules" start-stop-daemon --stop --pidfile $PIDFILE --oknodo --retry 30 # Add '-- -f $CONF' to take configuration in respect start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON -- -f $CONF ;; restart) $0 reload-modules exit $? ;; force-reload) $0 reload-modules exit $? ;; \*) echo "Usage: /etc/init.d/$NAME {start|stop|reload|reload-modules|force-reload|restart}" exit 1 ;; esac if [ $? == 0 ]; then echo . exit 0 else echo failed exit 1 fi

-- apache-php5 --

Done. You can now start / stop / ... those apaches independently using '/etc/init.d/apache-php5 >command<'.


Aaah, yes. the init and config scripts with the lock et al. files have to be adjusted ;) Was that the problem that it didn't work in the first place?

chregu at 2004-01-19

Not really. The problem was an IRCG module I had included within my php.ini. That should not have mattered at all, but it was the reason my PHP4 allways crashed... :(

Toby at 2004-01-19

Will it not be possible to have PHP 4 and 5 running in the same server as different MIME-types? Now that would help in that my hoster could install PHP 5 so that I can just use .htaccess to use 5 instead of 4 in one directory...

Manuzhai at 2004-01-21

I guess, since most of the API between Apache and PHP looks similar in Version 4 and 5 it's not possible to run both as a module in 1 server.

Indeed it should be possible to run 1 version as a module, the other one as CGI interpreter.

Toby at 2004-01-21


Jannis at 2004-03-19

This works well with Apache 2.0, although I didn't need the LockFile, just the PID. I combined it with Coggeshall's mod_proxy setup ( for a pretty slick solution. Thanks for writing this up.

John Gray at 2004-06-27

Hi, with WAMP5 you can now have PHP 5.0.0 and PHP 4.23.8 on the same Windows server...

romain at 2004-07-23

Until now, using php4 and not much want to move to php5 - I was completely satisfied 4

programming help at 2010-04-01

Sorry, that is only for web access. Checkouts can be done like this:

svn co svn://

That is because I run Lighttpd and not Apache. ;)

midi files downloads at 2010-05-21