Friday, 26 June 2015

PHP: getting PHP 5 and PHP 7 running side by side on the same machine (a better way)

G'day:
When starting to test PHP 7  I decided I did not want to sacrifice PHP 5, so wanted both running together. I horsed around getting PHP 7 running via the Apache module, and PHP 5 running via CGI. This is because Apache cannae run two different PHP modules at the same time. I documented my travails here: "Getting PHP 5.x & 7-dev running on Windows/Apache". As it turns out, there's a much easier option. Just run Apache twice...



I'd never done this before, and didn't even really know if it was possible but I decided to give it a blast. Oh, I'm running on Windows 7, btw.

I looked up the docs for httpd, and indeed one can specify a separate conf file when configuring the Windows service, so I figured I'd give it a go.

Firstly, I have all this crap at the bottom of my httpd.conf file:

LoadModule php7_module "C:/apps/php/7/php7apache2_4.dll"
AddHandler application/x-httpd-php .php
PHPIniDir "C:/apps/php/7/"

LoadModule fcgid_module modules/mod_fcgid.so

(the latter bit is something to do with getting PHP 5 running via CGI. I don't recall what it's about).

Anyway, I basically reverted my httpd.conf file to its default state, and moved the PHP 7 stuff out into its own file (httpd_php7.conf):

Include conf/httpd.conf
Listen 8070
Include conf/extra/httpd-vhosts_php7.conf
LoadModule php7_module "C:/apps/php/7/php7apache2_4.dll"
AddHandler application/x-httpd-php .php
PHPIniDir "C:/apps/php/7/"

(and I commented out the Listen line in httpd.conf)

So this loads the default conf file, plus just the PHP 7 stuff. I also made an equivalent httpd_php5.conf file:

Include conf/httpd.conf
Listen 8056
Include conf/extra/httpd-vhosts_php5.conf
LoadModule php5_module "C:/apps/php/5/php5apache2_4.dll"
AddHandler application/x-httpd-php .php
PHPIniDir "C:/apps/php/5/"

Note also I've configured each version-specific file to load a version-specific vhosts file. These just have the version specific vhost definitions. I've left the generic httpd-vhosts.conf file being included by httpd.conf, as I've got some non-PHP sites set up on this box too.

From there I got rid of the default Apache HTTPD service (one needs to do this from a CLI running with Admin permissions):

httpd.exe -k uninstall

And create two new ones, one for each version of PHP:

httpd.exe -k install -n "Apache for PHP 7" -f "C:\apps\apache\httpd\conf\httpd_php7.conf"

(and just change the 7s for 5s for the PHP 5 one).

That all seemed too easy and I doubted it would work, but I cranked up first the PHP 7 service, then the PHP 5 service... and both started fine.

Putting the proof to the pudding test, I confirmed the versions by browsing to my phpinfo.php file, which runs phpInfo():




Cool!

This could well be the most obvious thing ever, but it took a while for it to occur to my dumb old brain, and I was pleased it worked.

Righto.

--
Adam