Table of Contents

Phpfarm

nolink|

In order to be able to select the PHP version that will run on each different virtual host, we'll use Phpfarm.


Install git


As long as you satisfy yourself with the latest Git release available from the debian repos you could simply issue the following command to install it on your system:

sudo apt-get update
sudo apt-get install git

Install Phpfarm


We'll use the fpoirotte fork of phpfarm on github as it seems to be maintained more closely than the original cweiske repo. You are invited to review the “README.txt” document on that page.

Download the source

This needs to be done only once, at install time:

$ cd /opt/
$ sudo git clone https://github.com/fpoirotte/phpfarm.git
Cloning into 'phpfarm'...
remote: Counting objects: 584, done.
remote: Compressing objects: 100% (233/233), done.
remote: Total 584 (delta 332), reused 579 (delta 328)
Receiving objects: 100% (584/584), 104.37 KiB, done.
Resolving deltas: 100% (332/332), done.

Compile Selected Versions of PHP


As Phpfarm offers the opportunity to select the PHP version that you'll run along each virtual host, you first need to compile the desired versions.

Configuration files

As described in the README.rst file:
Default configuration options are in src/options.sh. You may create version-specific custom option

src/custom/options.sh
src/custom/options-<major>.sh
src/custom/options-<major>.<minor>.sh
src/custom/options-<major>.<minor>.<patch>.sh
src/custom/options-<major>.<minor>.<patch>-<flags>.sh

Where:

<major> is the version's major number (eg. “5” for PHP 5.3.1).
<minor> is the version's minor number (eg. “3” for PHP 5.3.1).
<patch> is the version's patch number (eg. “1” for PHP 5.3.1).
<flag> matches the specific compilation/installation flags (if any) for that PHP version.

See Special flags in version strings for information on supported flags. The flags should appear in the exact same order as listed in that chapter for this to work.

The shell script needs to define a variable named $configoptions with all ./configure options. Do not try to change prefix and exec-prefix.

> sudo mkdir /opt/phpfarm/custom
> sudo cp /opt/phpfarm/src/default-custom-php.ini /opt/phpfarm/custom/

The global compile options for the PHP versions you'll install can be set in:
/opt/phpfarm/custom/options.sh

See below for a sample content of such file, it should obviously be adapted to your specific php compile options requierments.

It is also possible to create per-version custom options file by specifying the version number in the file name:
/opt/phpfarm/custom/options-5.3.4.sh

PHPfarm will also setup your selected PHP version .ini file with a series of default options. They can be edited in:
/opt/phpfarm/custom/php.ini

Or in a version specific file as:
/opt/phpfarm/custom/php-5.3.4.ini

Sample custom-options.sh file (click to see)

Sample custom-php.ini (click to see)

PHP Compilation

Here also a slight difference from the original (cweiske): we'll use the /opt/phpfarm/src/main.sh script to compile php instead of the original /opt/phpfarm/src/compile.sh script.

$ cd /opt/phpfarm/src
$ sudo ./main.sh 5.2.17
$ sudo ./main.sh 5.3.27
$ sudo ./main.sh 5.4.4
$ sudo ./main.sh 5.4.27
$ sudo ./main.sh 5.5.11
...

For a complete list of existing PHP release versions check http://php.net/releases/.

While compiling PHP, and depending on the configuration options you have chosen, it might happen that you get an error of type: mcrypt.h not found or Unable to detect ICU prefix.

In most cases this means your system is missing some library to allow the required PHP options to execute. Installing the required libraries usually solves the problem. Here you can find an interesting list of configure errors, along with the commands to execute to resolve them.

During this process the missing libraries we had to install were:

> sudo apt-get install libcurl3-dev
> sudo apt-get install libjpeg62 libjpeg62-dev
> sudo apt-get install libpng12-0 libpng12-dev
> sudo apt-get install libicu-dev
> sudo apt-get install libmcrypt4 libmcrypt-dev
> sudo apt-get install libbz2-dev
> sudo apt-get install libxml2-dev
> sudo apt-get install zlib1g-dev

Create PHP Execution (wrapper) Scripts


For what comes next, you should first have suEXEC and the mod_fcgid apache module installed as described here.
Also, if working with a VirtualBox development environment, since we configured a shared web root directory, the following operations should be executed straight from the host's machine and not in the VM.

$ mkdir /home/<username>/Documents/webdev/www/php-fcgid
$ cd /home/<username>/Documents/webdev/www/php-fcgid
$ nano php-cgi-5.2.17

CONTENT:
#!/bin/sh
PHPRC="/opt/phpfarm/inst/php-5.2.17/lib/"
export PHPRC

PHP_FCGI_CHILDREN=3
export PHP_FCGI_CHILDREN

PHP_FCGI_MAX_REQUESTS=500
export PHP_FCGI_MAX_REQUESTS

exec /opt/phpfarm/inst/bin/php-cgi-5.2.17

Create one such file for each installed PHP version you have in Phpfarm (i.e: under /opt/phpfarm/inst/), in our example:

Adapting the file's name and content with the correct version number of course.

It is important to note that for suEXEC to allow execution of a cgi (wrapper)script, the containing directory, as well as the executable files, must all have the same owner as the one executing the suEXEC call (see SuexecUserGroup in the Apache's virtual host's config file).
Also, this directory, as well as the executable files it contains, cannot be writable by anyone but their owner.
Please pay attention to this, as it might result in apache throwing internal error 500 when trying to run a wrongly owned script.

In our example, the scripts are, by default, created with -rw-rw-r– (0644) rights, so we'll correct that:

$ ls -la /home/<username>/Documents/webdev/www
total 28
drwxrwxr-x 2 user group 4096 May  4 07:36 .
drwxr-xr-x 4 user group 4096 May  4 07:25 ..
-rw-rw-r-- 1 user group  210 May  4 07:29 php-cgi-5.2.17
-rw-rw-r-- 1 user group  211 May  4 07:34 php-cgi-5.3.27
-rw-rw-r-- 1 user group  211 May  4 07:35 php-cgi-5.4.27
-rw-rw-r-- 1 user group  209 May  4 07:35 php-cgi-5.4.4
-rw-rw-r-- 1 user group  211 May  4 07:36 php-cgi-5.5.11

$ chmod -R g-w /home/<username>/Documents/webdev/www
$ ls -la /home/<username>/Documents/webdev/www
total 28
drwxr-xr-x 2 user group 4096 May  4 07:36 .
drwxr-xr-x 4 user group 4096 May  4 07:25 ..
-rw-r--r-- 1 user group  210 May  4 07:29 php-cgi-5.2.17
-rw-r--r-- 1 user group  211 May  4 07:34 php-cgi-5.3.27
-rw-r--r-- 1 user group  211 May  4 07:35 php-cgi-5.4.27
-rw-r--r-- 1 user group  209 May  4 07:35 php-cgi-5.4.4
-rw-r--r-- 1 user group  211 May  4 07:36 php-cgi-5.5.11

Remove an Installed PHP Version


In case you'd like to recompile, or simply remove, an already installed PHP version, you'd need to remove the following files:


Setup an Apache Virtual Host


The last thing we need to do to have the chosen PHP version executing on a specified virtual host is to configure it. Please refer to the Apache2 Virtual Host Setup section to figure out how to do this.


Define System Default PHP Version


Using PHPFarm doesn't define a “command line” PHP executable. In some situations you'll want to have one version of PHP as your system's default.

To do this, first add inst/bin, inst/current/bin and inst/current/sbin to your $PATH in ~/.bashrc:

> nano $HOME/.bashrc

ADD THIS AT THE END:
# phpfarm binaries
PATH="$PATH:/opt/phpfarm/inst/bin:/opt/phpfarm/inst/current/bin:/opt/phpfarm/inst/current/sbin"

You could logout and login to a new session to have this modification active, but if you do not want to restart your session for this, issue:

> source $HOME/.bashrc

Using the switch-phpfarm command, you can check or define which version of PHP has to use as the system default PHP executable:

Without a version number it will show you the currently available PHP versions, a star will indicate the current default:

> switch-phpfarm
  5.4.4
* 5.4.6

> switch-phpfarm 5.4.4
Setting active PHP version to 5.4.4
PHP 5.4.4 (cli) (built: Nov  3 2014 13:33:16) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

To make sure you now have the desired php version available from the command line:

> php -v
PHP 5.4.4 (cli) (built: Nov  3 2014 13:33:16) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

> which php
/opt/phpfarm/inst/current/bin/php

Install Xdebug


In a PHP development environment, have a debugger might come handy, follow the instructions of this section to install XDebug on top of your chosen PHP version.