The Server

I've got a development server that I am using to host my current projects. Here are a few stats:

root@myserver:/usr/bin $ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.10
DISTRIB_CODENAME=maverick
DISTRIB_DESCRIPTION="Ubuntu 10.10"
root@myserver:/usr/bin $ apache2 -v
Server version: Apache/2.2.16 (Ubuntu)
Server built:   Nov 18 2010 21:17:43
root@myserver:/usr/bin $ php --version
PHP 5.3.3-1ubuntu9.1 with Suhosin-Patch (cli) (built: Oct 15 2010 14:00:18)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
root@myserver:/usr/bin $ uname -r
2.6.35-22-server

The Issue

I am running PHP 5.3.3 using mod_php5 and it is working great. But I have to run PHP 5.2.11 for starters VH around the server, and so i used phpfarm to compile PHP 5.2.11. I wish to configure Apache to make use of mod_php5 for everything around the server aside from this VH. I'll run PHP 5.2.11 for that one VH over FastCGI.

My Test

Like a test, I am using Apache's default site at /var/www. I setup the next directory files to report PHP versions in my experience:

  • /var/world wide web/phpinfo.php
  • /var/world wide web/php-5.2.11/phpinfo.php

My objective would be to have /var/www/phpinfo.php show me version 5.3.3 (mod_php5) and also have /var/www/php-5.2.11/phpinfo.php show me 5.2.11 (CGI). This is not working yet.

I placed the next code /etc/apache2/httpd.conf:

FastCgiServer /var/www/cgi-bin/php-cgi-5.2.11
ScriptAlias /cgi-bin-php/ /var/www/cgi-bin/

I placed the next code in to the default site's VH definition:

<Directory /var/www/php-5.2.11/>
    AddHandler php-cgi .php
    Action php-cgi /cgi-bin-php/php-cgi-5.2.11
</Directory>

Results

With mod_php5 enabled:

  • /var/world wide web/phpinfo.php ---> 5.3.3 (mod_php5)
  • /var/world wide web/php-5.2.11/phpinfo.php ---> 5.3.3 (mod_php5)

With mod_php5 disabled:

  • /var/world wide web/phpinfo.php ---> no handler Opera attempts to download the PHP file
  • /var/world wide web/php-5.2.11/phpinfo.php ---> 5.2.11 (CGI)

The Files Involved

http://files.mattalexander.me/apacheconfig.tgz

A great alternative is suPHP.

If setup correctly, you could have as numerous handlers, as you want for different sites inside a virtual host.

For additional security, there's suPHP's "paranoid" mode that you assign a Unix user and group to some virtual host and also the scripts is going to be run as that user.

My suPHP config appears like that:

...

[handlers]
;Handler for php-scripts
x-httpd-php=php:/usr/bin/php4-cgi
x-httpd-php5=php:/usr/bin/php5-cgi

;Handler for CGI-scripts
x-suphp-cgi=execute:!self

...

Inside a simple .htaccess file you'll be able to have scripts operated with different versions of PHP:

<FilesMatch \.php$>
   SetHandler x-httpd-php
</FilesMatch>

<FilesMatch \.php5$>
   SetHandler x-httpd-php5
</FilesMatch>

# etc...

Hope that can help.

The issue is that mod_php still handles .php files, even if the CGI handler is placed for any vhost.

To repair that, the <Directory> within the vhost will need a SetHandler directive:

  Action php-cgi /cgi-bin-php/php-cgi-5.2.11
  <FilesMatch \.php$>
    SetHandler php-cgi
  </FilesMatch>
</Directory>

I have up-to-date the tutorial.