Apache get PHP version from environment

August 19, 2014

Using phpfarm to compile custom PHP versions.

With the following structure:

/home/site
β”œβ”€β”€ cgi-bin
β”‚Β Β  └── php-cgi
└── phpfarm
    └── inst
        β”œβ”€β”€ bin
        β”‚Β Β  β”œβ”€β”€ php-cgi-5.5
        β”‚Β Β  └── php-cgi-5.6
        └── current-bin
            └── php-cgi

The cgi-bin/php-cgi script:

#!/bin/sh -e

cd "$(dirname "$0")"

inst=../phpfarm/inst

if [ -z "$PHP_VERSION" ]; then
    file="$inst/current-bin/php-cgi"
else
    file="$inst/bin/php-cgi-$PHP_VERSION"
fi

exec "$file"

And the Apache configuration:

DocumentRoot /home/site/public_html
ScriptAlias /cgi-bin-php/ /home/site/cgi-bin/

AddHandler php-cgi .php
Action php-cgi /cgi-bin-php/php-cgi

The PHP version will be executed according to the PHP_VERSION environment variable, that you can set for example from a .htaccess:

SetEnv PHP_VERSION 5.6

Variant: PHP version at project level

If you don’t need this to be configurable at environment level, you can simply configure the PHP version in the virtual host:

DocumentRoot /home/site/public_html
ScriptAlias /cgi-bin-php/ /home/site/cgi-bin/

<Location /project-5.5>
  AddHandler php-cgi .php
  Action php-cgi /cgi-bin-php/php-cgi-5.5
</Location>

<Location /project-5.6>
  AddHandler php-cgi .php
  Action php-cgi /cgi-bin-php/php-cgi-5.6
</Location>

Where php-cgi-5.5 contains the following (and similar for php-cgi-5.6):

#!/bin/sh
exec /home/php/phpfarm/inst/bin/php-cgi-5.5

Want to leave a comment?

Start a conversation on Twitter or send me an email! πŸ’Œ
This post helped you? Buy me a coffee! 🍻