On Ubuntu I needed some preparation: 1. install the php5-cgi package (different from php5-cli, which is what we use when using php from the commandline); 2. enable the actions module.
On Windows I was using xampp, which follows an Everything-But-The-Kitchen-Sink philosophy, and all I needed was already there.
Here is the code I added (to my VirtualHost). First for Ubuntu:
And then for Windows XAMPP:ScriptAlias /usephpcgi /usr/bin Action application/x-httpd-php-cgi /usephpcgi/php5-cgi
(That is copy and paste from working configurations, so I think the trailing slash must be optional!)ScriptAlias /usephpcgi C:/xampp/php/ Action application/x-httpd-php-cgi /usephpcgi/php-cgi.exe
Then for both Apache and Windows:
Now, I have to admit my dirty secret: I cheated. Instead of enabling php-cgi for all *.php in one directory, I left *.php going to the Apache PHP module, and I created the *.phpcgi extension to use the cgi binary. Initially this was simply because I managed to get it working that way; but on reflection I realized I preferred it: I can switch a script between using php module and php-cgi just by changing the extension; also I can use php-cgi anywhere in my VirtualHost. If that does not sound so useful I should explain the script in question is already hidden between an Alias, something like this, so no public-facing URLs need to change:AddType application/x-httpd-php-cgi .phpcgi
AliasMatch ^/admin/(.*?)/(.*)$ /path/to/admin.phpcgi
What about my original plan to configure it for a directory, without changing the file extensions? I had trouble with this, and gave up on this; but Ben kindly left a comment, so now I know how to do it. First, you still need the ScriptAlias and Action directives shown above. Then it is simply this.
As Ben explains (see comment#1 below) the reason we need the FilesMatch inside the Directory is because mod-php is setting a global FilesMatch directive; and that takes priority over our attempts to use AddType or AddHandler for a directory.<Directory /var/www/path/to> <FilesMatch "\.ph(p3?|tml)$"> SetHandler application/x-httpd-php-cgi </FilesMatch> </Directory>