Multiple versions of PHP on Gentoo

This document is a brief description of how to get two versions of PHP running on the same system, with different web sites configured to use different versions. I’ve only tried it with minor version i.e. 5.3 and 5.2, so I don’t know whether this will work when you want to mix major versions e.g. 4 and 5.

Tell Gentoo you’d like multiple versions

/etc/make.conf:

PHP_TARGETS="php5-3 php5-2"

Because PHP 5.4 is the default and I only want 5.3 and 5.2, I masked it in /etc/package.mask so that portage wouldn’t pull it in by default on emerge php:

>dev-lang/php-5.3

Install PHP

I already had PHP 5.2 installed, so I could just update world (deep with newuse) and specify the addition of PHP 5.3:

emerge -avDuN =dev-lang/php-5.3.23-r1 world

You may want to instead specify the PHP versions only:

emerge -av =dev-lang/php-5.2.17 =dev-lang/php-5.3.23-r1

In order for this to work I had to unmask some packages (I’m on x86_64) and configure some use flags:

/etc/package.unmask:

=dev-lang/php-5.2.17
=dev-lang/php-5.3.23-r1

/etc/package.keywords:

=dev-lang/php-5.3.23-r1
=app-admin/eselect-php-0.7.0

/etc/package.use:

app-admin/eselect-php apache2

Choose your system default

First check which are available. You should see the versions you just installed:

bpc steph # eselect php list cli
  [1]   php5.2 *
  [2]   php5.3

Do the same for apache2 etc as well as cli.

Then set whichever version you want for each:

bpc steph # eselect php set cli 2
bpc steph # eselect php set apache2 2
You have to run `/etc/init.d/apache2 restart' for the changes to take effect
bpc steph # eselect php set cgi 2
bpc steph # eselect php list cli
  [1]   php5.2
  [2]   php5.3 *

Configure your web sites

To run different web site with different versions of PHP you’ll need Apache with mod_fcgid (in the last section we set Apache to use PHP 5.3 by default, but we can point fcgid to different PHPs).

First check the location of our PHP config:

bpc steph # ls -l /etc/php
total 0
drwxr-xr-x 4 root root 160 Sep 30  2012 apache2-php5.2
drwxr-xr-x 4 root root 128 Apr  1 15:05 apache2-php5.3
drwxr-xr-x 4 root root 160 Sep 30  2012 cgi-php5.2
drwxr-xr-x 4 root root 128 Apr  1 15:05 cgi-php5.3
drwxr-xr-x 4 root root 160 Sep 30  2012 cli-php5.2
drwxr-xr-x 4 root root 128 Apr  1 15:05 cli-php5.3

And the location of the binaries:

bpc steph # ls -l /usr/bin/php*
lrwxrwxrwx 1 root root   30 Apr  1 15:28 /usr/bin/php -> ../../usr/lib64/php5.3/bin/php
lrwxrwxrwx 1 root root   34 Apr  1 15:28 /usr/bin/php-cgi -> ../../usr/lib64/php5.3/bin/php-cgi
lrwxrwxrwx 1 root root   37 Apr  1 15:28 /usr/bin/php-config -> ../../usr/lib64/php5.3/bin/php-config
lrwxrwxrwx 1 root root   33 Apr  1 15:28 /usr/bin/phpize -> ../../usr/lib64/php5.3/bin/phpize
-rwxr-xr-x 1 root root 2119 Apr 30  2009 /usr/bin/phpunit

Set the virtual host configuration for your site:

<VirtualHost *:80>
	ServerName mysite
	DocumentRoot /home/steph/websites/mysite/
	<Directory "/home/steph/websites/mysite/">
		Order allow,deny
		Allow from all
		AllowOverride All
	  Options ExecCGI FollowSymLinks
	</Directory>
  SuexecUserGroup steph steph
	AddHandler fcgid-script .php
	 /home/steph/bin/php5.2.fcgi .php
</VirtualHost>

The relevant lines there were AddHandler and FCGIWrapper. Remember to edit /etc/hosts to point mysite to 127.0.0.1 if you’re running it as a local site.

Create a fcgid wrapper (as /home/steph/bin/php5.2.fcgi in my example):

#!/bin/sh
PHPRC=/etc/php/cgi-php5.2/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
# See http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html
export PHP_FCGI_CHILDREN=0
exec /usr/lib64/php5.2/bin/php-cgi

You should be good to go - mysite should be running PHP 5.2. Do a similar setup for your PHP 5.3 sites.

Btw, you can verify that your site is running the correct PHP version by creating a phpinfo.php file and requesting it via your browser (via http://mysite/phpinfo.php in my example):

<?php print phpinfo(); ?>

Remember to delete it afterwards as you don’t want to reveal those details to the public.

References:

Last modified: 01/04/2013 Tags: , ,

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top