There are several reasons you might need to run multiple versions of PHP on the same server. Maybe you have a PHP 5.2 application running on your server and you need to start working on another application based on a new framework like Symfony2 or Lithium? Perhaps you haver a client with a legacy site that runs PHP 5.2, or maybe you simply want to test some of the new functionality? This post is going to explain how to setup a server to run multiple versions of PHP.

Apache doesn’t allow multiple versions of PHP using mod_php, so we need to compile it using fastcgi support. This will allow us to configure multiple sites, each one with a different version of PHP. I also allows us to specify the owner of the PHP process so we don’t have to use the default Apache user. Assigning specific users for each site has a number of advantages, including eliminating those pesky permissions problems when you have files created both by users and the webserver.

Pre-requisites

Before start we need to make sure all the required packages and modules for Apache are installed on the server.

# CentOS
$ yum install apr-devel apr

# Ubuntu
$ apt-get install apache2-suexec
$ a2enmod actions
$ a2enmod suexec

PHP Compilation

You will need to compile your PHP with no Apache apxs. This will enable cgi and allow you to choose the version of PHP you want to use when you setup your site. It will also avoid the multiple sapi error. Using your terminal, go to the directory where your PHP source files are located and type:

$ ./configure ...

In case of PHP 5.2, you must provide the required param to enable fastcgi.

$ ./configure --enable-fastcgi ...

If this won’t be the default PHP version on the server, you should add the param “prefix”. Otherwise you will replace this default version.

$ ./configure --prefix=/path/to/php/install ...

Finally

$ make && make install

Store the php.ini file for this version of PHP. Although it is not recommended, you can also use a generic php.ini, but this must be set in the configure options.

$ cp php.ini-recommended /usr/local/lib/php.ini

Fastcgi

Get the mod_fastcgi sources into the server

$ wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz
$ tar -xzf mod_fastcgi-current.tar.gz

Compile the module using httpd libs

$ cd mod_fastcgi-2.4.6/
$ cp Makefile.AP2 Makefile
$ make top_dir=/usr/lib64/httpd
$ make install top_dir=/usr/lib64/httpd

Load module for fastcgi on the apache configuration

// CentOS /etc/httpd/conf.d/fastcgi.conf
// Ubuntu /etc/apache2/mods-available/fastcgi.load
LoadModule fastcgi_module /path/to/mod_fastcgi.so

If Ubuntu is your distro then you must enable the module with a2enmod

Users

We need to set specific owners for PHP processes.

$ groupadd web
$ useradd -g web -d /var/www/vhosts/example.com testuser1

VirtualHost configuration
Create a script to run php using mod_fastcgi

// /var/www/example.com/cgi-bin/php-fcgi
#!/bin/sh
PHPRC=/usr/local/lib/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_CHILDREN=8
exec /usr/local/bin/php-cgi

We must set this script as executable and the owner with the previously added user

$ chmod +x /var/www/example.com/cgi-bin/php-fcgi
$ chown testuser1:web /var/www/example.com/cgi-bin/php-fcgi

Finally we need to configure the virtual host for our site specifying the cgi execution

// /etc/apache2/sites-enabled/example.com
ScriptAlias /cgi-bin "/var/www/example.com/cgi-bin"
<IfModule mod_fastcgi.c>
    SuexecUserGroup testuser1 web
    <Directory "/var/www/vhosts/example.com/httpdocs">
        Options +ExecCGI
        AllowOverride All
        AddHandler php5-fastcgi .php
        Action php5-fastcgi /cgi-bin/php-fcgi
        DirectoryIndex index.php index.html
        Order allow,deny
        Allow from all
    </Directory>
    ServerSignature Off
</IfModule>

Some settings may differ according to the Linux distro or the architecture of your server.

Hope this tutorial be helpful for you and comments are always welcome.