Why do this?
For running legacy Drupal applications it can be necessary to have different version of PHP on your system. Drupal5 runs on PHP-5.2, Drupal6 and later versions run on PHP-5.3, more about this here: http://drupal.org/requirements.
In a situation like this you might want the PHP-5.2 application (e.g., Drupal5) and the PHP-5.3 application (e.g., Drupal6) to be served from the same Apache. One way of doing it is using mod_php5 to serve PHP-5.3 applications and run PHP-5.2 applications over fast-cgi. Under Ubuntu 10.04 this can be achieved by installing PHP-5.3 from the repository and manually compiling and installing PHP-5.2.
I was facing this problem and this post is a write-up what I did to get it running in a step-by-step tutorial with all necessary resources and configurations.
There are two excellent blog posts, about how this can be done. The earlier is from Dave Parrish, a bit later came Brett with a nice tutorial. However, in spite of these excellent posts I encountered some pitfalls and discovered some caveats.
Setup Apache with mod_php5 (PHP-5.3)
Under Ubuntu it is really easy to set up apache with php. You simply install the necessary packages from the repository. The php version shipped with Ubuntu 10.04 is PHP-5.3. In my test environment I have the following installed:
sudo apt-get install apache2
sudo apt-get install php5 php5-dev libapache2-mod-php5
Checking localhost by directing the browser to http://localhost shows apache’s index.html saying It works!
To test if php works create a php file with f.x.
echo "<?php phpinfo(); ?>" | sudo tee /var/www/testphp.php
Testing php by directing the browser to http://localhost/testphp.php shows the php version information for PHP-5.3 from mod_php5.
To set up Drupal6 you can also install from the repositories by running
sudo apt-get install mysql-server
sudo apt-get install drupal6
With this setup Drupal6 applications can painlessly be run. However for production you might want to set up Drupal differently, here is some guidance:Â http://drupal.org/node/251019.
Install and test PHP-5.2
To install PHP-5.2 download the latest version from http://php.net/downloads.php to some local directory. Change into the directory and unpack php and change into the resulting directory
gunzip php-5.2.17.tar.gz
tar -xf php-5.2.17.tar
cd php-5.2.17/
Install the build tools to manually compile PHP-5.2.
sudo apt-get install libxml2-dev libmysqlclient-dev libcurl4-gnutls-dev libpng12-dev libjpeg62-dev
Then configure and install PHP-5-2 to /opt/php5.2
./configure --prefix=/opt/php5.2 --with-config-file-path=/opt/php5.2 --with-mysqli --with-mysql --with-curl --with-gd --with-jpeg-dir --enable-cli --enable-fastcgi --enable-discard-path --enable-force-cgi-redirect
make
sudo make install
Now you have PHP-5.2 installed in /opt/php5.2
You can test if PHP-5.2 works by having the testphp.php interpreted by the cgi binary
/opt/php5.2/bin/php /var/www/testphp.php
Set up Apache for FastCGI
Next thing to do is to set up apache to run PHP-5.2 over FastCGI
sudo apt-get install libapache2-mod-fastcgi
Make sure all neccesary modules are enabled in apache
sudo a2enmod cgi
sudo a2enmod fastcgi
sudo a2enmod actions
sudo /etc/init.d/apache2 restart
It is smart  to use a shell script that executes PHP-5.2 for you, in order to configure FastCGI and make a php.ini available. To do this create a file name php52-cgi and place it in Apaches cgi-bin directory /usr/lib/cgi-bin/ with the following content
#!/bin/sh
PHPRC="/opt/php5.2/"
export PHPRC
PHP_FCGI_CHILDREN=4
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS
exec /opt/php5.2/bin/php-cgi
The first parameter tells PHP-5.2 where to look for a php.ini, the second pre-fork threads with the FastCGI process manager, the third limits the requests. The last line executes php. More about this can be found at http://www.fastcgi.com/drupal/node/5?q=node/10.
Make sure the file is executable
sudo chmod +x /usr/lib/cgi-bin/php52-cgi
Please note that there is no php.ini in /opt/php5.2/. You have to manually copy and rename the php.ini that was shipped with PHP-5.2. It is located at in the php folder you unzipped at php-5.2.17/php.ini-recommended.
You can test if the script works by executing
/usr/lib/cgi-bin/php52-cgi < /var/www/testphp.php
To set up Apache create a Include file named php52.conf at /etc/apache2/ that can be used in any virtual host, which needs to use PHP-5.2. Write the following content in the /etc/apache2/php52.conf
#include for virtual hosts that need to run php-5.2
<FilesMatch "\.php">
SetHandler application/x-httpd-php5
</FilesMatch>
ScriptAlias /php52-cgi /usr/lib/cgi-bin/php52-cgi
Action application/x-httpd-php5 /php52-cgi
AddHandler application/x-httpd-php5 .php
The FilesMatch forces the handler to execute files with the suffix .php. This must be set, because if mod_php with PHP-5.3 is installed, the module interprets the php file and not the cgi script. The ScriptAlias sets the path to the cgi script. The AddHandler registers a new handler. The Action activates the cgi script for the files passed from the handler. You can read up on this under http://httpd.apache.org/docs/2.2/mod/mod_actions.html, http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler, and http://httpd.apache.org/docs/2.2/mod/core.html#sethandler.
See if it works
For testing you can runa PHP-5.3 application and a PHP-5.2 application in two different virtual hosts. Create a new entry in /etc/hosts with the line
127.0.0.1 php52.localhost php53.localhost
Now when you ping php52.localhost or php53.localhost you should receive packages from your local apache on the ip-address 127.0.0.1.
Now we create two virtual host files in /etc/apache2/sites-available, one named php52 and php53. In the php52 we add the Include file created earlier.
php52:
<VirtualHost *:80>ServerAdmin webmaster@localhostServerName php52.localhostDocumentRoot /var/www#Include php5.2Include php52.conf
ErrorLog /var/log/apache2/error.log# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.LogLevel debugCustomLog /var/log/apache2/access.log combined </VirtualHost>
php53:
<VirtualHost *:80>ServerAdmin webmaster@localhostServerName php53.localhostDocumentRoot /var/wwwErrorLog /var/log/apache2/error.log# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.LogLevel debugCustomLog /var/log/apache2/access.log combined </VirtualHost>
Enable the new virtual hosts and restart apache
cd /etc/apache2/sites-enabled
sudo ln -s ../sites-available/php52 php52
sudo ln -s ../sites-available/php52 php52
sudo /etc/init.d/apache2 restart
When you now direct your browser to http://php52.localhost/testphp.php you should see the output of PHP-5.2, when you direct your browser to http://php53.localhost/testphp.php you see the output of PHP-5.3.
So for any virtual host that requires PHP-5.2 you simply need to add the include /etc/apache2/php52.conf.
Have fun.