How to run PHP4 and PHP 5 prallel - Blog - Open Source - schlitt.info

schlitt.info - php, photography and private stuff

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/apache-php4.pid # 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/libphp4.so # For the .php5 file # LoadModule php5_module /usr/lib/apache/1.3/libphp5.so

-- 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/$NAME-php5.pid # 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<'.

If you liked this blog post or learned something, please consider using flattr to contribute back: .

Trackbacks

Comments

Add new comment

Fields with bold names are mandatory.