Making PHP4 and PHP5 coexist on a WebServer

New open source software is coming out and wants to use PHP5.

Meanwhile, old open source software has been heavily modded and can’t be updated to the latest release that has been fixed for PHP5.

And you really don’t have the time, resources or willpower to go through a cesspool of community-spewed, fermented, piece of trash code to fix it.

Better let it run under PHP4.

It’s a possible scenario.

There are many articles dealing with that theme:

Below, you will find the quick and dirty, step-by-step guide I used to setup the following:

  • PHP4 using mod_php (because the bulk of my PHP needs runs PHP4)
  • PHP5 using mod_fastcgi
  • Then, you can configure paths (or whole domains) to run with whatever PHP version you like.

**NB** These steps suppose you know a bit about what you’re doing. I mean, configuring apache, emerging packages and editing config files.

1) edit /etc/portage/package.use

– Make sure **cgi** and **force-cgi-redirect** are set for the PHP you want to use as CGI.
– Either add the following line or add the two flags to your existing flag

=dev-lang/php-5* cgi force-cgi-redirect

2) emerge both php packages

emerge -av '=dev-lang/php-4*' '=dev-lang/php-5*'

3) update /etc/conf.d/apache2 and modify APACHE2_OPT to include the following flags:

-D PHP5CGI -D PHP4

4) configure CGI plugin via /etc/apache2/modules.d/php5-cgi.conf

Example:

<IfDefine PHP5CGI>
ScriptAlias /php5-cgi /usr/lib/php5/bin/php-cgi
Action php5-cgi /php5-cgi
AddHandler php5-cgi .php5
</IfDefine>

NB Since I’m not trying to have .php5 run on a domains on my webserver, I don’t actually use this file.

5) configure php4 module via /etc/apache2/modules.d/70_mod_php.conf

I didn’t change anything in there, but you might want to change some file extension matchings.

6) locations, vhosts, etc, can also be fine-tuned using:

Example:

<Location /php5>
AddHandler php5-cgi .php
</Location>

NB Since I’m not trying to have .php5 run on a domains on my webserver, I don’t actually use this setup. See next item to see how I do it.

7) .php could also be handled by mod_fastcgi if so wished.

Here’s how I did it. (If you didn’t install mod_fastcgi, emerge mod_fastcgi and add -D FASTCGI to your /etc/conf.d/apache2 file)

The tricky part is in the apache configuration.
In the domain I wanted to run as php5, I added the following:

Action application/x-httpd-php5 /php5.fcgi
AddType application/x-httpd-php5 .php

Remember you need the ExecCGI option to be set where you want your php scripts to run through CGI

First line tells apache to run any script of type application/x-httpd-php5 through a script called /php5.fcgi

NB Basic mod_fastcgi config runs any .fcgi file through mod_fastcgi.

Second lines tells apapche that all .php files are of type application/x-httpd-php5

Then, all that is left to do is create the php5.fcgi script and put it in your domain’s root.
Here’s my file:

#!/bin/sh
PHP_FCGI_CHILDREN=2
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS
exec /usr/lib/php5/bin/php-cgi

NB The php5.fcgi bin must be executable to Apache, so make sure the +x rights are properly set through chmod

The magic is in the last line which takes the file piped by apache and runs it through /usr/lib/php5/bin/php-cgi, which is a fascgi-compliant version of php5

8) restart apache