Use PHP4 and PHP5 on the same machine

 A long time ago I had to solve the problem of running PHP4 and PHP5 on the same machine.

After a bit of searching I came across a posting in the german PHP-Mailing-List at http://www.phpbar.de where Norbert Pfeiffer described a possible way.

I choose to adapt my apachectl-file so that not one single instance of the apache-webserver was started but 2 instances. One of which listened on port 80, the other one on port 81.

This of course requires a unique httpd.conf for each of the instances. 

So here is my version of an apachectl-file.

#!/bin/sh
#
# Copyright 1999-2004 The Apache Software Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Apache control script designed to allow an easy command line interface
# to controlling Apache.  Written by Marc Slemko, 1997/08/23
# 
# The exit codes returned are:
#	0 - operation completed successfully
#	1 - 
#	2 - usage error
#	3 - httpd could not be started
#	4 - httpd could not be stopped
#	5 - httpd could not be started during a restart
#	6 - httpd could not be restarted during a restart
#	7 - httpd could not be restarted during a graceful restart
#	8 - configuration syntax error
#
# When multiple arguments are given, only the error from the _last_
# one is reported.  Run "apachectl help" for usage info
#
#
# |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||
# --------------------                              --------------------
# 
# the path to your PID file
PIDFILE=/var/run/httpd.pid
PIDFILE2=/var/run/httpd2.pid
PIDFILE3=/var/run/httpd3.pid
#
# the path to your httpd binary, including options if necessary
HTTPD="/usr/sbin/httpd -f /etc/httpd/httpd.80.conf"
HTTPD2="/usr/sbin/httpd -f /etc/httpd/httpd.81.conf"
HTTPD3="/usr/sbin/httpd -f /etc/httpd/httpd.8888.conf"
#.
# a command that outputs a formatted text version of the HTML at the
# url given on the command line.  Designed for lynx, however other
# programs may work.  
LYNXBIN="curl"
LYNX="$LYNXBIN -dump"
#
# the URL to your server's mod_status status page.  If you do not
# have one, then status and fullstatus will not work.
STATUSURL="http://localhost/server-status"
#
# --------------------                              --------------------
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||

ERROR=0
ARGV="$@"
if [ "x$ARGV" = "x" ] ; then 
    ARGS="help"
fi

for ARG in $@ $ARGS
do
    # check for pidfile
    if [ -f $PIDFILE ] ; then
	PID=`cat $PIDFILE`
	if [ "x$PID" != "x" ] && kill -0 $PID 2>/dev/null ; then
	    STATUS="httpd (pid $PID) running"
	    RUNNING=1
	else
	    STATUS="httpd (pid $PID?) not running"
	    RUNNING=0
	fi
    else
	STATUS="httpd (no pid file) not running"
	RUNNING=0
    fi
#
# Check for second apache
    if [ -f $PIDFILE2 ] ; then
        PID2=`cat $PIDFILE2`
        if [ "x$PID2" != "x" ] && kill -0 $PID2 2>/dev/null ; then
            STATUS="httpd (pid $PID2) running"
            RUNNING2=1
        else
            STATUS="httpd (pid $PID2?) not running"
            RUNNING2=0
        fi
    else
        STATUS="httpd (no pid file) not running"
        RUNNING2=0
    fi
#
# Check for third apache
    if [ -f $PIDFILE3 ]; then
	PID3=`cat $PIDFILE3`
        if [ "x$PID3" != "x" ] && kill -0 $PID3 2>/dev/null ; then
            STATUS="httpd (pid $PID3) running"
            RUNNING3=1
        else
            STATUS="httpd (pid $PID3?) not running"
            RUNNING3=0
        fi
    else
        STATUS="httpd (no pid file) not running"
        RUNNING3=0
    fi

    case $ARG in
    start)
	if [ $RUNNING -eq 1 ]; then
	    echo "$0 $ARG: httpd (pid $PID) already running"
	    continue
	fi
	if $HTTPD ; then
	    echo "$0 $ARG: httpd started"
	else
	    echo "$0 $ARG: httpd could not be started"
	    ERROR=3
	fi
        if [ $RUNNING2 -eq 1 ]; then
            echo "$0 $ARG: httpd (pid $PID2) already running"
            continue
        fi
        if $HTTPD2 ; then
            echo "$0 $ARG: httpd2 started"
        else
            echo "$0 $ARG: httpd2 could not be started"
            ERROR2=3
        fi
        if [ $RUNNING3 -eq 1 ]; then
            echo "$0 $ARG: httpd (pid $PID3) already running"
            continue
        fi
        if $HTTPD3 ; then
            echo "$0 $ARG: httpd3 started"
        else
            echo "$0 $ARG: httpd3 could not be started"
            ERROR3=3
        fi
;;
    stop)
	if [ $RUNNING -eq 0 ]; then
	    echo "$0 $ARG: $STATUS"
	    continue
	fi
	if kill $PID ; then
	    echo "$0 $ARG: httpd stopped"
	else
	    echo "$0 $ARG: httpd could not be stopped"
	    ERROR=4
	fi
        if [ $RUNNING2 -eq 0 ]; then
            echo "$0 $ARG: $STATUS"
            continue
        fi
        if kill $PID2 ; then
            echo "$0 $ARG: httpd2 stopped"
        else
            echo "$0 $ARG: httpd2 could not be stopped"
            ERROR2=4
        fi
	if [ $RUNNING3 -eq 0 ]; then
            echo "$0 $ARG: $STATUS"
            continue
        fi
        if kill $PID3; then
            echo "$0 $ARG: httpd3 stopped"
        else
            echo "$0 $ARG: httpd3 could not be stopped"
            ERROR3=4
        fi

	;;
    restart)
	if [ $RUNNING -eq 0 ]; then
	    echo "$0 $ARG: httpd not running, trying to start"
	    if $HTTPD ; then
		echo "$0 $ARG: httpd started"
	    else
		echo "$0 $ARG: httpd could not be started"
		ERROR=5
	    fi
	else
	    if $HTTPD -t >/dev/null 2>&1; then
		if kill -HUP $PID ; then
		    echo "$0 $ARG: httpd restarted"
		else
		    echo "$0 $ARG: httpd could not be restarted"
		    ERROR=6
		fi
	    else
		echo "$0 $ARG: configuration broken, ignoring restart"
		echo "$0 $ARG: (run 'apachectl configtest' for details)"
		ERROR=6
	    fi
	fi
        if [ $RUNNING2 -eq 0 ]; then
            echo "$0 $ARG: httpd2 not running, trying to start"
            if $HTTPD2 ; then
                echo "$0 $ARG: httpd2 started"
            else
                echo "$0 $ARG: httpd2 could not be started"
                ERROR=5
            fi
        else
            if $HTTPD2 -t >/dev/null 2>&1; then
                if kill -HUP $PID2 ; then
                    echo "$0 $ARG: httpd2 restarted"
                else
                    echo "$0 $ARG: httpd2 could not be restarted"
                    ERROR2=6
                fi
            else
                echo "$0 $ARG: configuration broken, ignoring restart"
                echo "$0 $ARG: (run 'apachectl configtest' for details)"
                ERROR2=6
            fi
        fi
        if [ $RUNNING3 -eq 0 ]; then
            echo "$0 $ARG: httpd3 not running, trying to start"
            if $HTTPD3 ; then
                echo "$0 $ARG: httpd3 started"
            else
                echo "$0 $ARG: httpd3 could not be started"
                ERROR3=5
            fi
        else
            if $HTTPD3 -t >/dev/null 2>&1; then
                if kill -HUP $PID3 ; then
                    echo "$0 $ARG: httpd3 restarted"
                else
                    echo "$0 $ARG: httpd3 could not be restarted"
                    ERROR3=6
                fi
            else
                echo "$0 $ARG: configuration broken, ignoring restart"
                echo "$0 $ARG: (run 'apachectl configtest' for details)"
                ERROR3=6
            fi
        fi
	;;
    graceful)
	if [ $RUNNING -eq 0 ]; then
	    echo "$0 $ARG: httpd not running, trying to start"
	    if $HTTPD ; then
		echo "$0 $ARG: httpd started"
	    else
		echo "$0 $ARG: httpd could not be started"
		ERROR=5
	    fi
	else
	    if $HTTPD -t >/dev/null 2>&1; then
		if kill -USR1 $PID ; then
		    echo "$0 $ARG: httpd gracefully restarted"
		else
		    echo "$0 $ARG: httpd could not be restarted"
		    ERROR=7
		fi
	    else
		echo "$0 $ARG: configuration broken, ignoring restart"
		echo "$0 $ARG: (run 'apachectl configtest' for details)"
		ERROR=7
	    fi
	fi
        if [ $RUNNING2 -eq 0 ]; then
            echo "$0 $ARG: httpd2 not running, trying to start"
            if $HTTPD2 ; then
                echo "$0 $ARG: httpd2 started"
            else
                echo "$0 $ARG: httpd2 could not be started"
                ERROR2=5
            fi
        else
            if $HTTPD2 -t >/dev/null 2>&1; then
                if kill -USR1 $PID2 ; then
                    echo "$0 $ARG: httpd2 gracefully restarted"
                else
                    echo "$0 $ARG: httpd2 could not be restarted"
                    ERROR2=7
                fi
            else
                echo "$0 $ARG: configuration broken, ignoring restart"
                echo "$0 $ARG: (run 'apachectl configtest' for details)"
                ERROR2=7
            fi
        fi
        if [ $RUNNING3 -eq 0 ]; then
            echo "$0 $ARG: httpd3 not running, trying to start"
            if $HTTPD3 ; then
                echo "$0 $ARG: httpd3 started"
            else
                echo "$0 $ARG: httpd3 could not be started"
                ERROR3=5
            fi
        else
            if $HTTPD3 -t >/dev/null 2>&1; then
                if kill -USR1 $PID3 ; then
                    echo "$0 $ARG: httpd3 gracefully restarted"
                else
                    echo "$0 $ARG: httpd3 could not be restarted"
                    ERROR3=7
                fi
            else
                echo "$0 $ARG: configuration broken, ignoring restart"
                echo "$0 $ARG: (run 'apachectl configtest' for details)"
                ERROR3=7
            fi
        fi
	;;
    status)
	case "`which $LYNXBIN`" in
		*"no "*) echo "Status doesn't work without lynx, try fullstatus" ;;
		*)  $LYNX $STATUSURL | awk ' /process$/ { print; exit } { print } ';;
	esac
        case "`which $LYNXBIN`" in
                *"no "*) echo "Status doesn't work without lynx, try fullstatus" ;;
                *)  $LYNX $STATUSURL | awk ' /process$/ { print; exit } { print } ';;
        esac
	;;
    fullstatus)
        case "`which $LYNXBIN`" in
                *"no "*) open $STATUSURL ;;
                *)  $LYNX $STATUSURL ;;
        esac	
	;;
    configtest)
	if $HTTPD -t; then
	    :
	else
	    ERROR=8
	fi
        if $HTTPD2 -t; then
            :
        else
            ERROR2=8
        fi
        if $HTTPD3 -t; then
            :
        else
            ERROR3=8
        fi
	;;
    *)
	echo "usage: $0 (start|stop|restart|fullstatus|status|graceful|configtest|help)"
	

So you noted that this file starts three Server-insances? Yepp. Currently I need PHP4 for some legacy-stuff (runs on port 8888), PHP5 for the current development (still runs on port 81) and for RT I needed another server instance as the PHP4-Module and the Perl-Module somehow did not want to work together. So that was the easiest way to fix that for me.