Linux Devbox: Running multiple PHP versions en

By drm on Saturday 30 October 2010 14:38 - Comments (10)
Categories: Development, Linux / BSD, Views: 8.300

Ever since PHP5 came out, there is some need to run multiple PHP versions on your local development box, or at least make it easy to switch from one version to another. Derick Rethans has posted a solution based on your PATH environment variable. I have devised an alternative approach which changes the system wide used PHP version.

1. Compiling and installing

First, if you're used to installing PHP from your distro's repositories, stop with that. Remove your current version with your package manager entirely, deleting it's configuration as well (maybe backing it up in your homedir, if it's very special).
Now, download the PHP version of your liking into a local directory, maybe in your homedir, or (what I prefer) in /usr/local/src, and extract it (changing your mirror accordingly):
$ cd /usr/local/src && \
   wget http://nl3.php.net/get/php-5.3.3.tar.bz2/from/this/mirror -O - \
   | tar jx

Change into the extracted source directory
$ cd php.5.3.3

and configure the installation with all the flags you need for your specific version, and make sure you choose a prefix somewhere in /usr/local/, that would not affect any future installed versions. I prefer /usr/local/php/5.x.x/, and have one base php.ini in /usr/local/etc/, and additional php.ini's per version in /usr/local/etc/php/5.x.x/, so the configure command would read something like:
$ ./configure                                              \
   --prefix=/usr/local/php/5.3.3                                \
   --with-config-file-path=/usr/local/etc/php                   \
   --with-config-file-scan-dir=/usr/local/etc/php/5.3.3.d       \
   --with-mysql                                                 \
   --with-mysqli                                                \
   --with-pdo-mysql                                             
   # etcetera

Note that once you configured, the configure script will write out a file called config.nice which will contain all chosen flags and options. You can edit it or run it with additional flags to reconfigure your installation.

If you have established that the configuration is correct and you have all the flags and additional settings you need, you can start compiling and installing.
$ make -j5 # I use 5 for my quadcore, use 3 for dualcore
$ sudo make install


Repeat the above for any other versions of PHP you might want. I currently have 5.2.14 and 5.3.3, but you can use different version names, for example for versions you have running with or without XDebug, GD, suhosin patches, or whatever, with some identifying suffix, e.g. php5.3.3-suhosin

2. Getting the php-version.sh script

The script is published at my github. Download it in a sensible place and chmod it so it can be run:
cd /usr/local/bin && \
   wget http://github.com/drm/snippets/raw/master/shell/php-version.sh && \
   chmod +x ./php-version.sh

3. Using the script

Run the script with the chosen PHP version as an argument (like 5.3.3 in the example above). The first time run, it needs the --force flag to force switching to that version. See further down this post for more information.
$ php-version.sh 5.3.3 --force

Now, the script symlinks all files in the PHP's bin directory to /usr/local/bin/. Validate your status with php -v which should print the current php version as follows.
php -v
PHP 5.3.3 (cli) (built: Oct 30 2010 14:26:40) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies


Whenever you switch a version, it checks to see what the current symlinked version is, removing all symlinks to the current version and symlinking again to the new version specified. So any successive call to the script may lack the --force option, since the current PHP version should be detected. Running the script without arguments prints a usage message, and the currently detected PHP version:
$ php-version.sh
Usage: /usr/local/bin/php-version.sh {version [--force]|--off}
Current version: 5.3.3


Remove the symlinks without choosing a new version is done by passing the --off flag as a first parameter
$ php-version.sh --off
$ php -v
bash: php: command not found

4. Building and using PHP extensions

Whenever you need to build extensions (like XDebug, which is pretty useful), you will need to switch to the version you're building for, before running phpize. After all, the symlinked version of phpize would be used to detect your PHP environment. Installing the extension will need some handywork, but as long as you keep your prefixes separated clearly, you shouldn't get into any trouble. Just install the extensions in the correct place aside the version you compiled for.

Since each version checks for it's own configuration files in the directory specified with the --with-config-file-scan-dir configure option, you can put the extension and zend_extension directives in files placed in that directory.
$ echo "extension=/path/to/extension.so" >> /usr/local/etc/php/5.3.3.d/myextension.ini
$ php -i | grep myextension.ini
additional .ini files parsed => /usr/local/etc/php/5.3.3.d/myextension.ini

5. Web server configuration

I have started switching my development boxes from Apache with mod_php5 to nginx with fastcgi, which makes it a bit easier to switch the php version used by your web server, since the fast cgi is most easily started up by using the php cli binary itself.

The easiest setup for nginx and php fast cgi is described by Tomasz Sterna here.

If you're still keen on using Apache, either consider switching to mod_fastcgi in stead of mod_php5, symlink the installed libphp5.so by hand, or augment the php-version.sh script to do it for you.

Happy php'ing :)

Volgende: The king is dead. Long live the king. 12-'10 The king is dead. Long live the king.
Volgende: ReTwizzle this: #lorizzle 10-'10 ReTwizzle this: #lorizzle

Comments


By Tweakers user Spesh, Saturday 30 October 2010 15:33

De mogelijkheden in Linux zijn eindeloos, kiezen voor Windows server is duidelijk een teken van gebrek aan kennis.

By Tweakers user foobar79, Saturday 30 October 2010 15:52

I would HIGHLY recommend against compiling you're own software.
Especially for machines hosting services on the interwebs.
Unless you have the time to follow (security) maillinglists and recompile every time there is a major bug and/or security fix you´re probably going to have a issue sooner or later.
Running a default distro version will make updates a walk in the park and they will backport security fixes if necessary. (extra bonus: even when you're on vaction you're non-l33t h4x0r colleague's will be able to run yum update / apt get update )

If you really need a recent PHP/whatever version look at e.g. backports for Debian or some other form of a reliable public repo (dotdeb repo e.g.).

When versions do not mix nicely on a single machine simply use virtualisation in some form (vserver is, performance wise, really nice).

Even if it is a development machine you should still stick to packaged versions so you won't have any issues when going to a production machine with packaged version x.

Just my 2ct's

[Comment edited on Saturday 30 October 2010 15:52]


By Tweakers user Ventieldopje, Saturday 30 October 2010 15:54

I can imagine this tool came in handy when PHP5 wasn't mainstream yet and everyone was making the switch from PHP4 to PHP5 (or not) but I think by now everyone should have switched to PHP5 this script isn't necessary anymore ;)

By Tweakers user Punkie, Saturday 30 October 2010 15:58

quote: post
make -j5 # I use 5 for my quadcore, use 3 for dualcore
Waarom de -j5 optie (betekend dat het gestart wordt in max 5 jobs simultaan). Of specifiek, waarom k+1 met k je aantal cores? Ik verwacht eerder k of k*1.5 jobs, afhankelijk van hun gedrag.

By Tweakers user Ramoonus, Saturday 30 October 2010 17:31

het commando etcetera werkt niet ;)

By Tweakers user Blokker_1999, Saturday 30 October 2010 17:49

Punkie wrote on Saturday 30 October 2010 @ 15:58:
[...]

Waarom de -j5 optie (betekend dat het gestart wordt in max 5 jobs simultaan). Of specifiek, waarom k+1 met k je aantal cores? Ik verwacht eerder k of k*1.5 jobs, afhankelijk van hun gedrag.
Het is een aanbeveling die je regelmatig op het net tegenkomt om het aantal cores +1 te nemen.

Ik ben iemand die php vooral voor eigen, kleine projecten gebruik en vraag me dus af wat het nut is om verschillende versies te gebruiken. Zitten er dan zo een grote verschillen tussen die kleine releases?

By Tweakers user drm, Saturday 30 October 2010 17:55

foobar79
I would HIGHLY recommend against compiling you're own software.
This post is mainly targeted at local development usage.
Even if it is a development machine you should still stick to packaged versions so you won't have any issues when going to a production machine with packaged version x.
That's more of a staging than development issue, imo.
Phas0r
I can imagine this tool came in handy when PHP5 wasn't mainstream yet and everyone was making the switch from PHP4 to PHP5 (or not) but I think by now everyone should have switched to PHP5 this script isn't necessary anymore ;)
PHP5.2 to 5.3 is a big step too. E.g. Drupal6 isn't stable on PHP5.3
Ramoonus
het commando etcetera werkt niet ;)
Ingesprongen bedoel ik een comment, geen root prompt ;)

By Tweakers user mindcrash, Saturday 30 October 2010 21:25

Spesh wrote on Saturday 30 October 2010 @ 15:33:
De mogelijkheden in Linux zijn eindeloos, kiezen voor Windows server is duidelijk een teken van gebrek aan kennis.
Ik mis argumenten. Bovendien kan eenzelfde script als hierboven waarschijnlijk net zo goed voor Windows worden geschreven (via Powershell en symlinks)

[Comment edited on Saturday 30 October 2010 21:25]


By David, Sunday 31 October 2010 12:44

Spesh wrote on Saturday 30 October 2010 @ 15:33:
De mogelijkheden in Linux zijn eindeloos, kiezen voor Windows server is duidelijk een teken van gebrek aan kennis.
onderbouw je mening is... :+

By hendra gunawan, Monday 28 November 2011 23:04

Thanks.

Can you update this article to add portability issue? for example, what if i move the directory from /usr/local to /media/flashdisk1? is it still work? I can achieve this in Windows.

please email me if you have done.

Comments are closed