Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jbinfo/d297fcfdb151e25a299c to your computer and use it in GitHub Desktop.
Save jbinfo/d297fcfdb151e25a299c to your computer and use it in GitHub Desktop.

Multiple PHP version under Ubuntu 14.04

Update your machine

apt-get update
apt-get upgrade

Install some dependencies

apt-get install build-essential

This packages contain a lot of necessary tools like a c and a c++ compiler, make and other libs.

Install git

apt-get install git

Install the lamp server (Apache MySQL PHP)

apt-get install lamp-server^

The installation will ask you to choose a password for the mysql root user. Once finished, open a tab in your browser to http://localhost

You should see a page with the heading "It Works!"

Install php-farm

cd /opt
git clone git:// phpfarm

Configure PHP

Before compiling any PHP version, setup the options you need

vim /opt/phpfarm/src/

Mine looks like this:

# You can override config options very easily.
# Just create a custom options file; it may be version specific:
# -
# -
# -
# -
# Don't touch this file here - it would prevent you to just "svn up"
# your phpfarm source code.


--enable-bcmath \
--with-mysqli \
--with-mysql \
--with-curl \
--with-png \
--with-gd \
--enable-gd-native-ttf \
--enable-calendar \
--enable-exif \
--enable-ftp \
--enable-mbstring \
--enable-pcntl \
--enable-soap \
--with-pdo-mysql \
--enable-sockets \
--enable-sqlite-utf8 \
--enable-wddx \
--enable-zip \
--with-openssl \
--with-jpeg-dir=/usr/lib \
--with-zlib \
--with-gettext \
--with-mcrypt \
--enable-intl \

echo $version $vmajor $vminor $vpatch

[ -f $custom ] && source "$custom" $version $vmajor $vminor $vpatch
[ -f $custom ] && source "$custom" $version $vmajor $vminor $vpatch
[ -f $custom ] && source "$custom" $version $vmajor $vminor $vpatch
[ -f $custom ] && source "$custom" $version $vmajor $vminor $vpatch

Compile all the PHP version you need, eg

cd /opt/phpfarm/src
./ 5.5.0

If the compilation fails, just install the libraries that caused the error. In my case I had to install these packages:

apt-get install libxml2 libxml2-dev libssl-dev libcurl4-openssl-dev pkg-config libicu-dev libmcrypt-dev php5-dev libgeoip-dev libmagickwand-dev libjpeg-dev libpng12-dev libmysqlclient-dev
./ 5.5.0

To verify the installation

cd /opt/phpfarm/inst/bin
./php-5.5.0 --version

Install and enable FastCGI

apt-get install libapache2-mod-fastcgi apache2-mpm-worker apache2-suexec 
a2enmod actions fastcgi suexec
service apache2 restart

This will disable mod_php.

Edit the /etc/apache2/apache2.conf, add these lines before the includes

FastCgiServer /var/www/cgi-bin/php-cgi-5.5.0
ScriptAlias /cgi-bin-php/ /var/www/cgi-bin/

Now create the file /var/www/cgi-bin/php-cgi-5.5.0, with this content

export PHPRC



exec /opt/phpfarm/inst/bin/php-cgi-5.5.0

Make the file executable

chmod +x /var/www/cgi-bin/php-cgi-5.5.0

Configure a VirtualHost to use the php version we just configured eg: /etc/apache2/sites-available/example.conf

<VirtualHost *:80>
  DocumentRoot /var/www/example/
  <Directory "/var/www/example">
    AddHandler php-cgi .php
    Action php-cgi /cgi-bin-php/php-cgi-5.5.0
    <FilesMatch "\.php$">
      SetHandler php-cgi

Enable the site

a2ensite example.conf

Add the server to /etc/hosts

Restart apache

service apache2 restart

I got an error at this point. Apache was complaining about mod_fast already defined. To fix the problem opne this file /etc/apache2/mods-available/fastcgi.conf and comment out the last line inside the IfModule block

<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  #FastCgiWrapper /usr/lib/apahce2/suexec
  #FastCgiIpcDir /var/lib/apache2/fastcgi

Then restart apache again

service apache2 restart

If you don't get any error you will find a working installation of PHP5.5.0 at :)

Copy link

nextpic commented Aug 23, 2014

I have this problem:
You don't have permission to access /cgi-bin-php/php-cgi-5.4.32/index.php on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

My VHost conf is:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
DocumentRoot /var/www/

    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            AddHandler php-cgi .php
            Action php-cgi /cgi-bin-php/php-cgi-5.4.32
            <FilesMatch "\.php$">
                    SetHandler php-cgi

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog /var/www/
    LogLevel warn
    CustomLog /var/www/ combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet"

Copy link

got the same error. Running on apache 2.4.7 with same setup here. can't find anything useful online.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment