This is a blog about running two PHP versions on one webserver and using multiple php.ini files, this combination can be a tricky one to tackle. But luckily one we can tackle quite easily as long as one of the PHP versions is >= 5.2.7. For this example I'll be using Apache, but the webserver flavor doesn't really matter. The most important part is the "PHP_INI_SCAN_DIR" environment variable.
There could be a number of reasons to want what I'm about to talk about. In my case I have a project where I have a legacy code-base, running on a specific PHP version, and a new code-base which will be run on 5.3. Because the new code-base will be a ongoing progress of replacing the old, it first has to run side by side with the legacy code-base. So I wanted my development image to run two PHP versions.
The old code-base used php.ini settings such as a include-path, error reporting, etc. Which will be different from the new code-bas, and those can no-longer be set with the 'php_value' feature of Apaches since the PHP version we'll be using for that runs as (f)CGI rather then as module.
Installing two PHP versions in one Apache
I'm not going into depth here, I'm assuming you know how to install PHP both via a package manager and manually.
Install
the first PHP version you want to use, I suggest to pick the most
recent version or the version you expect to be updating more frequently
via your package manager (or manually if your distribution/OS doesn't have one). And to install the (legacy) version
'manually', this will also be the version that will run in fcgi in this
example. (So in my case, I installed PHP 5.3 via a package manager and
PHP 5.2 manually.)
If you have for example a PHP 5.1.6 and PHP 5.2.11 then you need to install the PHP 5.1.6 as module and PHP 5.2.11 as (f)CGI since the PHP_INI_SCAN_DIR variable only works on 5.2.7 and up.
Secondly install the second
version. The important factor is that you specify the --prefix= and let
it be different from the the first version, I used:
--prefix=/usr/local/php52.
Adding your PHP-CGI handler:
This is probably common ground also, but I just added it for easy copy and pasting.
<IfModule alias_module>
ScriptAlias /cgi-bin-php/ "/usr/local/php52/bin/"
</IfModule>
<Directory "/usr/local/php52/bin/">
AllowOverride All
Options None
Order allow,deny
Allow from all
</Directory>
Vhost config:
The virtual host config I'm using has the
"interesting" part of this blog article, and that is the
'PHP_INI_SCAN_DIR'. (Currently PHP 5.3 doesn't support multiple paths to
scan for php.ini files. But hopefully that is added soon, it is
available in PHP 6.) By issuing 'SetEnv PHP_INI_SCAN_DIR
"/path/to/ini/directory/"' you let PHP 'know' to look there for the
php.ini, rather then the default location. (For those of you who are
curious, check line 606 of:
http://svn.php.net/viewvc/php/php-src/trunk/main/php_ini.c?revision=289667&view=markup)
<virtualhost *:80>
ServerAdmin me@myhost
ServerName myapplication.myhost
# Path to website/project's document root
DocumentRoot /mnt/hgfs/projects/myapplication/www
# Set the environment var for PHP to look for php.ini's
SetEnv PHP_INI_SCAN_DIR "/mnt/hgfs/projects/myapplication/ini"
AddHandler php-script .php
Action php-script /cgi-bin-php/php-cgi
..
</virtualhost>
All you need to do now is make sure you have project specifc php.ini files in their "/mnt/hgfs/projects/$x/ini" directories.
Do
note that you need to copy the *entire* php.ini there, not just one
with replacement settings because the original php.ini is no longer
used.