====== Phpfarm ====== {{:vms:webdev:php_logo.png?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 [[https://github.com/fpoirotte/phpfarm|fpoirotte fork of phpfarm]] on github as it seems to be maintained more closely than the original [[https://github.com/cweiske/phpfarm|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-.sh src/custom/options-..sh src/custom/options-...sh src/custom/options-..-.sh Where: is the version's major number (eg. "5" for PHP 5.3.1).\\ is the version's minor number (eg. "3" for PHP 5.3.1).\\ is the version's patch number (eg. "1" for PHP 5.3.1).\\ 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)| EXTRA_LIBS="$EXTRA_LIBS -lstdc++ " export EXTRA_LIBS configoptions="\ --with-config-file-scan-dir=/opt/phpfarm/inst/php-${version}/conf.d \ --enable-cli \ --with-pear \ --with-iconv \ --with-mysql=/usr/bin/mysql_config \ --with-mysql \ --with-mysqli \ --with-pdo-mysql \ --with-libdir=/lib/x86_64-linux-gnu \ --enable-ftp \ --with-gd \ --enable-gd-native-ttf \ --with-mcrypt \ --with-mhash \ --enable-soap \ --with-curl \ --with-zlib \ --with-zlib-dir \ --enable-mbstring \ --with-jpeg-dir=/usr/lib/x86_64-linux-gnu \ --with-png-dir=/usr/lib/x68_64-linux-gnu \ --with-gettext \ --with-mhash \ --enable-bcmath \ --enable-sockets \ --enable-cgi \ --enable-calendar \ --enable-zip \ --enable-pcntl \ --enable-wddx \ --with-bz2 \ --enable-mysqlnd \ --enable-intl \ --with-icu-dir=/usr \ --with-openssl \ " You might have to replace all **x68_64-linux-gnu** reference with the one corresponding to your architecture (like **i386-linux-gnu**) ++++ ++++Sample custom-php.ini (click to see)| date.timezone=Europe/Brussels extension_dir="$ext_dir" include_path=".:/opt/phpfarm/inst/php-$version/pear/php/" ++++ ==== 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 [[http://www.robo47.net/text/6-PHP-Configure-und-Compile-Fehler|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 [[vms:webdev:apache#Install apache suEXEC|as described here]].\\ Also, if working with a VirtualBox development environment, since we [[vms:webdev:apache#Create the Shared www Folder|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//Documents/webdev/www/php-fcgid $ cd /home//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 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//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//Documents/webdev/www $ ls -la /home//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: * /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) ----- ===== 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 [[vms:webdev:apache#Virtual Host Setup|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 [[vms:webdev:phpfarm:xdebug|the instructions of this section]] to install XDebug on top of your chosen PHP version.