Multiple PHP versions with Apache 2, FastCGI, PHPFarm on Ubuntu

Posted December 2nd, 2010 in Tehnikalije by Metod

This tutorial is a part of the “Setting up your local development environment” series. It is based on this article while keeping in mind that in previous apache tutorial we compiled apache from source. So I’m definitely not taking all the credit for it. But there are some modifications.

Starting with PHPFarm, we need Git installed in order to check it out. If you don’t have it installed:

  1. sudo apt-get install git-core

Now create a directory in which PHP versions will reside. The directory of my liking is /usr/local/php, so I will use that. You can of course change that.

  1. cd /usr/local

Clone the phpfarm repository.

  1. git clone git://git.code.sf.net/p/phpfarm/code php

Now we have to edit the configuration options which phpfarm will use to compile php.

  1. cd php/src

We can use more general configuration options or very strict ones. Default configuration options.sh can be overridden by creating a file called custom-options.sh or a bit more specific like custom-options-5.2.sh or very strict like custom-options-5.3.18.sh. Since the configurations for 5.2 and 5.3 are different and I like to be a strict person, I will create two configurations:

  • custom-options-5.2.17.sh
  • custom-options-5.3.18.sh

custom-options-5.2.17.sh contents (these config values were also mentioned in previous tutorial):

  1. configoptions="
  2.         –enable-cli \
  3.         –with-pear \
  4.         –with-openssl=/usr \
  5.         –with-iconv \
  6.         –with-curl \
  7.         –with-mysqli \
  8.         –enable-mbstring \
  9.         –enable-exif \
  10.         –with-jpeg-dir=/usr \
  11.         –with-zlib \
  12.         –with-zlib-dir \
  13.         –with-png-dir=/usr \
  14.         –with-gd \
  15.         –with-gettext \
  16.         –enable-gd-native-ttf \
  17.         –with-mhash \
  18.         –enable-ftp \
  19.         –with-pspell \
  20.         –with-mcrypt \
  21.         –enable-bcmath \
  22.         –with-mime-magic \
  23.         –with-pdo-mysql \
  24.         –enable-sockets \
  25.         –enable-soap \
  26.        –enable-calendar \
  27.         –enable-fastcgi \
  28.         –enable-force-cgi-redirect \
  29. "

custom-options-5.3.18.sh contents:

  1. configoptions="
  2.         –with-mysqli=mysqlnd \
  3.         –with-pdo-mysql=mysqlnd \
  4.         –enable-cli \
  5.         –with-pear \
  6.         –with-openssl=/usr \
  7.         –with-iconv \
  8.         –with-curl \
  9.         –enable-mbstring \
  10.         –enable-exif \
  11.         –with-zlib \
  12.         –with-zlib-dir \
  13.         –with-gd \
  14.         –with-gettext \
  15.         –enable-gd-native-ttf \
  16.         –with-mhash \
  17.         –enable-ftp \
  18.         –with-pspell \
  19.         –with-mcrypt \
  20.         –enable-bcmath \
  21.         –enable-sockets \
  22.         –enable-soap \
  23.         –enable-calendar \
  24.         –with-png-dir=/usr \
  25.         –with-jpeg-dir=/usr
  26. "

For PHP 5.3 we don’t need the cgi parameters, since these are already enabled by default.

On Ubuntu 11.04, the configuration might fail with error messages about libjpeg and/or libpng. What you can do is the following:

  1. sudo apt-get install libjpeg8-dev

Install the libjpeg8-dev package. This will solve the libjpeg error message.

  1. sudo ln -s /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/libpng.so

Create a symbolic link to the libpng.so in the /usr/lib directory. This will solve the libpng error message.

Next step – compiling PHP.

  1. sudo ./compile.sh 5.2.17
  2. sudo ./compile.sh 5.3.18

Where the number after the ./compile.sh is the PHP version you wish to compile.

Now that we have compiled PHP, we can move on to set up apache. If you followed the apache tutorial, first of we need to recompile it.

  1. ./configure \
  2.         –prefix=/usr/local/apache2.2.22 \
  3.         –with-included-apr \
  4.         –enable-mods-shared=all \
  5.         –enable-so \
  6.         –enable-mod-rewrite \
  7.         –enable-suexec \
  8.         –with-suexec-caller=daemon

These are just my configuration options. The options you have to pay attention to for this topic are:

  • –enable-so
  • –enable-suexec
  • –enable-suexec-caller=daemon – You can change this according to which user your apache runs under.
  1. make
  2. sudo make install

Apache should be up and running. Next stop – FastCGI module.

Download and extract the latest (current) source from http://www.fastcgi.com/dist/.

  1. tar xzf mod_fastcgi-current.tar.gz
  2. cd mod_fastcgi-2.4.6

Next thing to do is rename Makefile.AP2 to Makefile, so we can compile it for apache 2.

  1. cp Makefile.AP2 Makefile

Compile and install the module.

  1. make top_dir=/usr/local/apache2.2.22
  2. sudo make install top_dir=/usr/local/apache2.2.22

The top_dir parameter is optional. If you compiled apache to the more usual location like /usr/local/apache2, you do not need to set this option.

Now that we have mod_fastcgi module compiled, we can load it as a shared object. Add the following line to your httpd.conf:

  1. LoadModule fastcgi_module modules/mod_fastcgi.so

If you had previously ran PHP as an apache module, you must disable it. You can only run PHP either as CGI or an apache module. So in httpd.conf, comment out this line:

  1. # LoadModule php5_module modules/libphp5.so

Create a .conf file (you can call it what you want) – i.e. php-fcgi.conf and put the following content inside:

  1. <IfModule mod_fastcgi>
  2.     FastCgiWrapper /usr/lib/apache2.2.22/bin/suexec
  3.     FastCgiConfig -idle-timeout 110 -killInterval 120 -pass-header
  4.         HTTP_AUTHORIZATION -autoUpdate
  5.  
  6.     ScriptAlias /php-fcgi/ /var/www/cgi-bin/
  7. </IfModule>

And include it in your main httpd.conf:

  1. # Include FastCGI configuration
  2. Include conf/php-fcgi.conf

Now create a directory for CGI scripts.

  1. sudo mkdir /var/www/cgi-bin

And create scripts.

  1. sudo nano /var/www/cgi-bin/php-cgi-5.2.17
  2. sudo nano /var/www/cgi-bin/php-cgi-5.3.18

And put something like the following in. Just change the version of PHP.

  1. #!/bin/sh
  2. version="5.3.18"
  3.  
  4. PHPRC=/usr/local/php/phpfarm/inst/php-${version}/lib/php.ini
  5. export PHPRC
  6.  
  7. PHP_FCGI_CHILDREN=3
  8. export PHP_FCGI_CHILDREN
  9.  
  10. PHP_FCGI_MAX_REQUESTS=5000
  11. export PHP_FCGI_MAX_REQUESTS
  12.  
  13. # which php-cgi binary to execute
  14. exec /usr/local/php/phpfarm/inst/php-${version}/bin/php-cgi

These scripts should be executable. So we set permissions to 0755.

  1. cd /var/www/cgi-bin
  2. sudo chmod +x *

Now we configure apache virtualhosts. Each virtualhost will have it’s own subdomain and PHP version.

  1. NameVirtualHost localhost.53:80
  2. <VirtualHost localhost.53:80>
  3.   ServerName localhost.53
  4.   DocumentRoot /var/www
  5.   <Directory "/var/www">
  6.     AddHandler php-cgi .php
  7.     Action php-cgi /php-fcgi/php-cgi-5.3.18
  8.   </Directory>
  9. </VirtualHost>

Do the same for all versions of PHP.

We are almost there. Since these subdomains (asumingly) do not exist yet, we must point them to your localhost IP.

  1. sudo nano /etc/hosts

Just add your subdomains after the localhost definition. It should look something like this:

  1. 127.0.0.1 localhost localhost.52 localhost.53

Everything is set, so just restart the apache.

  1. sudo /usr/local/apache2.2.22/bin/apachectl -k restart

Put a file phpinfo.php in /var/www directory with the following line:

And test it. If everything went well, you should see different PHP versions on different subdomains.

  • http://localhost.52/phpinfo.php – PHP 5.2.17
  • http://localhost.53/phpinfo.php – PHP 5.3.18

Enjoy!

Post updated on November 12th, 2012th to reflect latest updates.