Wrong PHP prediction: you don’t need to patch PHP to run multiple versions
I read an article on Michael Kimsals blog about how he is waiting for a patch to run different PHP versions on the same Apache server. I think he is misinformed and I highly doubt than someone will write that patch. The solution is here already.
You simple can’t have PHP4 and PHP5 both run as Apache2 module in the same process, because they use a lot of the same internal symbols (variables, function names, etc). If you would change that, nobody would be able to write any extensions which run both on PHP4 and PHP5. However you can run multiple PHP versions as CGI modules and there is no patch required for that.
If you want to choose the PHP version based on the filename, you can simple add
SetEnv PHPRC "/usr/local/php4/" ScriptAlias /php4/ "/usr/local/php4/" Action application/x-httpd-php4 "/php4/php.exe" AddType application/x-httpd-php4 .php SetEnv PHPRC "/usr/local/php5/" ScriptAlias /php5/ "/usr/local/php5/" Action application/x-httpd-php5 "/php5/php.exe" AddType application/x-httpd-php5 .php5
Further details about this are described in Giunta Gaetanos article. Note: You can also use AddHandler, but the idea stays the same.
However you won’t find any articles describing it in this way, nor many hosting companies implementing it in such a matter. The problem is that you do not want to select based on a filename. Downloaded applications like WordPress and Joomla wouldn’t use PHP5 and asking your customers to please change all their filenames to switch, well…
There are better solutions for this. If you declare each vhost in your apache config, you can just follow Giuntas article. He uses *:port, but you should read that as different vhosts no matter how you decleare these (probably with NameVirtualHost).
If you use mass virtual hosting, which most hosting companies do, you can use a .htaccess file. Just follow the Giuntas article, but don’t put the SetEnv, ScriptAction and Action as well as the AddType for the default PHP version in the main part of httpd.conf/apache2.conf. Than put the following in a .htaccess file:
AddType application/x-httpd-php5 .php
If you have mass virtual hosting and don’t want to use .htaccess files, but want to switch it outside the webroot of your customer, you can use different IP addresses and switch using DNS. This is how it is described in most available articles. Again Giuntas uses different ports, but that’s just the test setup. Instead use different IP addresses and you can switch by changing the IP address in DNS without the need to move any files.
If your currently using PHP as Apache2 module and switch to a CGI based setup, you might notice a 20% drop in performance. If this is your bottleneck, that means you can stack 20% less customers on a server. Beside that fact that you need to buy new servers, moving around accounts isn’t a sweet task. There is a way to support both PHP4 and PHP5, but keep running it as module. You can start Apache twice, listening to different IP addresses, with one loading PHP4 and the other PHP5. I’ve written a howto about this earlier in my article ‘Running multiple instances of Apache’.
All these solutions are fairly easy to set up and in no case is mod_proxy involved. Though I knew I was doing something wrong… charging money for it… and now I’m giving away all my knowledge for free. Guess I’ll always stay a poor bastard
, but at least I’ve made a few souls more happy.
07 Sep 2007 Arnold Daniels




I’ve changed the text of the comment, because Guillaume Rossolini was busting my balls. The content is basically the same.
The way that Zend advises to have php4 and 5 on the same apache is by having 5th as apache module and 4th as CGI.
Yes that is another possibility, and a rather good one. It is basically the same solution as having 2 CGIs. The advantage though is that you don’t loose performance with the PHP5, because it runs as module. Loosing a bit of performance on PHP4 probably isn’t a big issue, since only the few sites will keep running that.