This is an old revision of the document!


Phpfarm

nolink|

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



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


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.


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

Please note that those files location slightly differs from the original (cweiske) version. Here they reside in /opt/phpfarm/custom/options.sh and /opt/phpfarm/custom/php.ini while in the original, they resided in /opt/phpfarm/src/custom-options.sh and /opt/phpfarm/src/custom-php.ini, also the /opt/phpfarm/custom directory is not present after standard git cloning (as of release dd4a369), so you'll need to create this directory by yourself, we'll also move the default custom options file to this location:

> sudo mkdir /opt/phpfarm/custom
> sudo mv /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

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


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:

  • 5.2.17
  • 5.3.27
  • 5.4.4
  • 5.4.27
  • 5.5.11

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 Apche'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 thibaut thibaut 4096 May  4 07:36 .
drwxr-xr-x 4 thibaut thibaut 4096 May  4 07:25 ..
-rw-r--r-- 1 thibaut thibaut  210 May  4 07:29 php-cgi-5.2.17
-rw-r--r-- 1 thibaut thibaut  211 May  4 07:34 php-cgi-5.3.27
-rw-r--r-- 1 thibaut thibaut  211 May  4 07:35 php-cgi-5.4.27
-rw-r--r-- 1 thibaut thibaut  209 May  4 07:35 php-cgi-5.4.4
-rw-r--r-- 1 thibaut thibaut  211 May  4 07:36 php-cgi-5.5.11


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

  • /opt/phpfarm/inst/php-x.y.z (recursively)
  • /opt/phpfarm/src/php-x.y.z (recursively)
  • /opt/phpfarm/inst/bin/*-x.y.z (symlinks)
  • /opt/phpfarm/inst/main (if it exists and is pointing to a version being removed)


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.



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 define which version of PHP has to be used as the system default PHP executable:

> cd /opt/phpfarm/inst/bin
> ./switch-phpfarm 5.4.4

THIS NEEDS TO BE VERIFIED AS IT DOESN'T SEEM TO WORK…

A possible solution that still needs to be validated:



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.