erdfisch Blog

Multiple versions of PHP with Apache under Linux

Body: 

PHP has done significant changes to the language specification during the last relase cicles. Those led to several incompatabilities, which effect the different versions of Drupal: While you can not use Drupal 8 with PHP under version 5.4, Drupal 6 on the other side wouldn't (and won't) run properly with all modules using PHP version 5.4. Most GNU/Linux distributions however maintain newer versions of PHP, which makes sense for security and so on. Never the less in order to perform migrations, or still some bigger websites run on Drupal 6, it is needed to run multiple versions of PHP in one single computer.

One good solution, for sure, is virtualization and installing an older version of PHP in the virtual machine. But this fits not always in the regular workflow of the programmer, so this tutorial is about installing a different PHP version in parallel and it can be defined in Apache's virtual host configuration, which version of PHP to use.

This tutorial is based on Debian (Jessie) Testing and should be basically the same in other distributions. Although some package names and commands may vary a little bit. We are going to use the newest version of PHP version 5.3.x.

Obtain super cow powers

  • su -

or

  • sudo -s

Download newest php source code

  • mkdir /usr/local/src/php5-build
  • cd /usr/local/src/php5-build
  • wget -O php-5.3.28.tar.bz2 http://de1.php.net/get/php-5.3.28.tar.bz2/from/this/mirror
  • tar xvjpf php-5.3.28.tar.bz2
  • cd php-5.3.28

Install all necessary dependencies
General webserver. You propably have most of all these packages already installed (on Debian make sure the non-free repositories are enabled)

  • apt-get install apache2 php5 php5-common php5-cli php5-mysql php5-gd php5-mcrypt php5-curl libapache2-mod-php5 php5-xmlrpc mysql-server mysql-client libapache2-mod-fastcgi

Special dependencies needed only for compiling

  • apt-get install build-essential php5-dev libbz2-dev libmysqlclient-dev libxpm-dev libmcrypt-dev libcurl4-gnutls-dev libxml2-dev libjpeg-dev libpng12-dev

Compile PHP

  • ./configure --prefix=/usr/share/php53 --datadir=/usr/share/php53 --mandir=/usr/share/man --bindir=/usr/bin/php53 --includedir=/usr/include/php53 --sysconfdir=/etc/php53/apache2 --with-config-file-path=/etc/php53/apache2 --with-config-file-scan-dir=/etc/php53/conf.d --enable-bcmath --with-curl=shared,/usr --with-mcrypt=shared,/usr --enable-cli --with-gd --with-mysql --with-mysqli --enable-libxml --enable-session --enable-xml --enable-simplexml --enable-filter --enable-inline-optimization --with-jpeg-dir --with-png-dir --with-zlib --with-bz2 --with-curl --enable-exif --enable-soap --with-pic --disable-rpath --disable-static --enable-shared --with-gnu-ld --enable-mbstring
  • make && make install

Remove packages used for compiling only

  • apt-get remove build-essential php5-dev libbz2-dev libmysqlclient-dev libxpm-dev libmcrypt-dev libcurl4-gnutls-dev libxml2-dev libjpeg-dev libpng12-dev

Activate apache module

  • a2enmod cgi fastcgi actions
  • service apache2 restart

Create corresponding configuration file

  • editor /etc/apache2/php53.conf

#Include file for virtual hosts that need to run PHP 5.3

SetHandler application/x-httpd-php5

ScriptAlias /php53-cgi /usr/lib/cgi-bin/php53-cgi
Action application/x-httpd-php5 /php53-cgi
AddHandler application/x-httpd-php5 .php

 

Create environment script to start the additional PHP version

  • editor /usr/lib/cgi-bin/php53-cgi

#!/bin/sh
PHPRC="/etc/php53/apache2/"
export PHPRC
PHP_FCGI_CHILDREN=4
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS
exec /usr/bin/php53/php-cgi

  • chmod +x /usr/lib/cgi-bin/php53-cgi

Configure Apache2's virtual hosts
In order to run the virtual host based on the different PHP version, just include the created configuration file into the VirtualHost.
For example:


Include php53.conf
ServerName example.org
DocumentRoot /var/www/sites/example.org

Options Indexes FollowSymLinks MultiViews
AllowOverride all
Require all granted

 

Configure a D6 drush command
In order to use drush properly with Drupal 6, the drush command needs to be run through the alternative version of Drupal. Therefore it is best to create an alias in some place of your .bashrc. You can locate the file with the command "find / -name 'drush.php'"
Here are two examples, depending if you used the composer to install drush or the debian package:

alias drush_d6="/usr/bin/php53/php $HOME/.composer/vendor/bin/drush.php"

alias drush_d6='/usr/bin/php53/php /usr/share/php/drush/drush.php'

Kommentare

Comments

I wonder whether you ever …

I wonder whether you ever tried out phpenv? This could reduce most of the pain of this process.