====== Multiple PHP Versions ====== As for the apache configuration decribed [[packages:amp:apache|here]], we'll try to setup a system that allows an easy selection of the php version that we'll choose to execute on a "per virtual host" base. ===== Remove Existing PHP ===== In case you have a previous install of PHP, you can wipe it off your system using the following commands: Let's check if you have PHP installed: <code> sudo which php5 </code> If you get a response like:// /usr/bin/php5//, your system HAS PHP installed, remove it using: <code> sudo dpkg -l | grep php ii php5-cli 5.5.1+dfsg-2 amd64 command-line interpreter for the php5 scripting language ii php5-common 5.5.1+dfsg-2 amd64 Common files for packages built from the php5 source ii php5-json 1.3.1+dfsg-2 amd64 JSON module for php5 </code> Then for each line reported earlier: <code> sudo dpkg --purge php5-cli php5-common php5-json </code> ===== PHPFarm ===== [[http://cweiske.de/tagebuch/Introducing%20phpfarm.htm|PHPFarm]] was made to simplify the installation of multiple PHP versions on one system. Deployment can seem a little bit complex at first, but once it's rolling it's a real joy! ==== Installation ==== 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.\\ Make sure you have [[packages:git|Git installed]]... Also if you plan on using MySQL with your PHP installation, it is recommended you have it installed before you start the PHP version installation, see this section for [[packages:amp:mysql|more info on installing MySQL]]. === Download the source === This needs to be done only once, at install time: <code> $ 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. </code> ==== Add Selected PHP Versions ==== These steps can be re-executed later, whenever you need to make another PHP version vailable for testing. === compile php sources === <WRAP info center 75%> You can modify the global php compile options in the **/opt/phpfarm/src/custom-options.sh** file. It is also possible to create per-version custom options file by specifying the version number in the file name: 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 the **/opt/phpfarm/src/custom-php.ini** file. Or in a version specific file as: custom-php-5.3.4.ini </WRAP> **Sample custom-options.sh file:** <code> 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 \ --with-mime-magic \ --enable-sockets \ --enable-cgi \ --enable-calendar \ --enable-zip \ --enable-pcntl \ --enable-wddx \ --enable-bz2 \ --enable-mysqlnd \ --enable-intl \ --with-icu-dir=/usr \ --enable-sqlite-utf8 \ " </code> You might need to chmod a+x custom-options.sh **Sample custom-php.ini** <code> date.timezone=Europe/Brussels include_path=".:/opt/phpfarm/inst/php-$version/pear/php/" </code> **Now let's launch the PHP compilation:** <code> cd /opt/phpfarm/src sudo ./compile.sh 5.2.17 sudo ./compile.sh 5.3.25 sudo ./compile.sh 5.4.18 sudo ./compile.sh 5.53 ... sudo mkdir /var/www/php-fcgid </code> The "mkdir" command is only needed on the initial install, it obviously will exist on later versions additions. <WRAP box center 75%> 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 command to execute to resolve them]]. The "Unable to detect ICU prefix" isn't listed there, try:\\ sudo apt-get install libicu-dev </WRAP> My setup required the following additional packages installation: * apt-get install libxml2 libxml2-dev * apt-get install libcurl3-dev * apt-get install libjpeg62 libjpeg62-dev * apt-get install libpng12-0 libpng12-dev * apt-get install libicu-dev * apt-get install libmcrypt4 libmcrypt-dev For what comes next, you should first have suEXEC and the mod_fcgid apache module installed [[packages:amp:apache#Install apache suEXEC|as described here]]. ==== Create php execution script ==== <code> cd /var/www/php-fcgid sudo nano php-cgi-5.2.17 #!/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 (Ctrl-x - Y: to exit nano saving file) </code> Then make it executable: <code> > chmod ug+x /var/www/php-fcgid/php-cgi-5.2.17 </code> Repeat previous code block, replacing "5.2.17" with the targeted version number. <WRAP info> 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 vhost'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. You can read a detailed explanation for all of this in the [[http://httpd.apache.org/docs/2.2/suexec.html|apache page regarding suEXEC]]. <color coral>**TO ACHIEVE THIS WE'LL HAVE TO ADAPT THE SHARED FOLDERS MOUNTING OPTIONS IN /etc/fstab (TBC)**</color> **YOU ALSO HAVE TO USE A NON SYSTEM USER**, (i.e. with a uid/gid above 1000) to run the SuEXEC process, which is defined in the Apache VHost configuration file hereunder. Otherwise you might get an error of type: "cannot run as forbidden uid".\\ Please note that this might imply changing the user and group used to run Apache itself which can be configured in the **/etc/apache2/apache2.conf** file. Also the rights on /var/lib/apache2/fcgid/sock might need to be updated. To monitor SuEXEC errors, check the **/var/log/apache/suexec.log** file </WRAP> === set apache config accordingly === Edit **each** "sites-available" config file: <code> sudo nano /etc/apache2/sites-available/domain.conf <VirtualHost *:80> ServerName domain ServerAlias domain ServerAdmin webmaster@domain DocumentRoot /var/www/sites/domain/web/ <IfModule mod_fcgid.c> SuexecUserGroup suexec suexec #PHP_Fix_Pathinfo_Enable 0 <Directory /var/www/sites/domain/web/> Options +ExecCGI AllowOverride All AddHandler fcgid-script .php FcgidWrapper /var/www/php-fcgid/php-cgi-5.4.4 .php Order allow,deny Allow from all </Directory> </IfModule> ErrorLog /var/www/sites/domain/log/error.log CustomLog /var/www/sites/domain/log/access.log combined ServerSignature Off </VirtualHost> </code> === edit hosts file on your development machine to respond localy === For each "domain" that you define in the config files, edit your **/etc/hosts** file, adding the domain names like this: <code> sudo nano /etc/hosts 127.0.0.1 localhost domain01 domain02 domain03 domain04 domain05 domain06 domain07 domain08 127.0.1.1 machine-name # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts </code> ==== Define System Default PHP Version ==== Using the switch-phpfarm command, you can define which version of PHP has to be used as the system default PHP executable: <code> > cd /opt/phpfarm/inst/bin > ./switch-phpfarm 5.4.4 </code> ==== Adding Xdebug ==== in case you're willing to benefit from a top debugging toll for your php installations, you're invited to read the [[packages:amp:php:xdebug|Xdebug section of this wiki]]. ==== Manage Rights on Web Files ==== In case you haven't done so yet, it is recommended that you have a look at the [[packages:amp:apache#Managing Rights on Web Files|instructions on how to set access rights on web files]]. ==== Restart apache2 ==== When all is done, restart apache with the new configuration: <code> sudo service apache2 restart </code>