I assume you have a working Apache 2 installation with PHP4 modules installed, if not run the following and you will have:
sudo apt-get install apache2 apache2-mod-php4
Note: you can also have PHP5 installed as module and PHP4 as CGI, everything works the same way with swapping the versions in commands.
First of all you will need the php5-cgi package, and enable the Actions module:
sudo apt-get install php5-cgi
Add a ScriptAlias directive to your apache2.conf – if you have not done this already – like this:
ScriptAlias /cgi-bin /cgi-bin
Now create the /cgi-bin directory and symlink the php5-cgi binary to /cgi-bin/php5-cgi:
ln -s /usr/bin/php5-cgi /cgi-bin/php5-cgi
Finally in every VirtualHost where you need PHP5 support put these lines:
AddHandler php5-script .php
Action php5-script /cgi-bin/php5-cgi
All you have to do is restart the apache to take changes effect and you’re done.