In this tutorial I’d like to show you how to install and switch between multiple PHP version on Ubuntu. You should also be able to use this on other distribution, but you may have to adjust some of the commands and paths (apt-get, apache2).
So let’s do this.
To get set-up we first need to update our packet list if you haven’t done that already today. :D
sudo apt-get update
Next we need to install some programs on our machine so we can build php ourself.
sudo apt-get install git build-essential lamp-server^
When the installation is finished you should be asked for a password for the root mysql server. After you’ve specified your password you should be able to open http://localhost/ in you browser and see the “It Works!” message. (On Ubuntu 14.04 it’s a quiet longer page with the title “Apache2 Ubuntu Default Page”)
Now everything should be setup to install phpfarm which is a collection of tools to download and compile various PHP version and use the with the Apache webserver.
We install it into the /opt directory which is for 3rd party software in the linux file system hierarchy.
cd /opt sudo git clone git://git.code.sf.net/p/phpfarm/code phpfarm
If we now want to compile PHP 5.6.0 we have to create a options file for this specific version. In the repository of phpfarm we already have a template so we don’t have to start from scratch.
If you’ve installed it with my guide in the /opt directory the template should be /opt/phpfarm/src/options.sh
We now have to copy this file and create a new one with the version number in the file name.
sudo cp /opt/phpfarm/src/options.sh /opt/phpfarm/src/custom-options-5.6.0.sh
Because the default options are pretty basic, we will add a few more compile flags. Also we have to remove the part at the end of the template, because else we will get a infinite loop where the same script gets called.
Here is my custom-options-5.6.0.sh file:
#!/bin/bash #gcov='--enable-gcov' configoptions="\ --enable-bcmath \ --with-mysqli \ --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 \ $gcov"
To now finally compile the version we just have to run the compile.sh file as root and wait for the compile process to finish.
sudo ./compile.sh 5.6.0
The script will remind you if you have to install any libraries to compile the version, for example I had to install the mcrypt-dev packet.
To verify we’ve correctly compile our version we can now run the php binary in /opt/phpfarm/inst/php-5.6.0/bin
/opt/phpfarm/inst/php-5.6.0/bin/php --version
The next step is to configure our Apache to use our compiled version of PHP. To do this we have to install the fastcgi module, so we can start PHP as a FastCGI daemon.
sudo apt-get install libapache2-mod-fastcgi apache2-mpm-worker apache2-suexec sudo a2enmod actions fastcgi suexec
To create our FastCGI daemon I’ve created a little script with which we can generate our script in approximately 3 seconds.
#!/bin/bash mkdir -p /var/www/cgi-bin VERSION=$1 cat > /var/www/cgi-bin/php-cgi-$1 <<EOF #!/bin/sh PHPRC="/etc/php5/cgi/$1/" export PHPRC PHP_FCGI_CHILDREN=3 export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=5000 export PHP_FCGI_MAX_REQUESTS exec /opt/phpfarm/inst/bin/php-cgi-$1 EOF chmod +x /var/www/cgi-bin/php-cgi-$1
Save this script onto your drive and make it executable by running:
chmod +x scriptname
You can use the script like this:
sudo ./scriptname 5.6.0
This will generate a File /var/www/cgi-bin/php-cgi-5.6.0 (Or whatever PHP Version you want.) which will be used by Apache.
Now we have to configure Apache to use this as a FastCGI Handler. To do this we add those lines before the “IncludeOptional mods-enabled/*.load” line.
FastCgiServer /var/www/cgi-bin/php-cgi-5.6.0 ScriptAlias /cgi-bin-php/ /var/www/cgi-bin/
Next we have to comment the second last line of the configuration of the FastCGI module in /etc/apache2/mods-available/fastcgi.conf
#FastCgiIpcDir /var/lib/apache2/fastcgi
Almost finished. Now we have to add this snippet to our virtual host configuration to allow our host to use a .htaccess file to add additional rules.
<Directory /var/www/html> AllowOverride All </Directory>
Finally we can add the piece of code which will change the PHP version into the .htaccess file of our host. (The file should be /var/www/html/.htaccess)
AddHandler php-cgi .php Action php-cgi /cgi-bin-php/php-cgi-5.6.0 <FilesMatch "\.php$"> SetHandler php-cgi </FilesMatch>
Now we have to restart the Apache webserver by running:
sudo service apache2 restart
And here we go, a virtual host with a customized PHP version. If you want to install additional versions, just follow the guide again, but replace 5.6.0 with your version.
If you run into any problems or if you have problems following this guide, just comment below and I’ll get back to you.
This guide was heavily inspired by this gist (https://gist.github.com/gmodarelli/5887778). I just added the script and polished it up into a blog post.
Thank you for reading my article and we’ll see us next time with “Supercharge your LAMP Stack Part 2: Create dynamic virtual hosts”
Hi Robin!
Thanks for the very fast reply!
Newb mistake. I installed the Digital Ocean LAMP server according to their procedure.
There were clearly some missing packages there versus the GIT install.
I was able to track down the last few missing 1 or 2 from that & then the d/l and compile worked!
Thanks Again!,
-Will
Great to hear that it worked out for you :)
Hi Robin,
I don’t think this worked.
At the end I get Quote, ”
make: *** [ext/openssl/openssl.lo] Error 1
make failed.
“endquote.
Not sure which extra packages to install like gmodarelli talks about over here: https://gist.github.com/gmodarelli/5887778
-Or should I just install the last PHP 5.6 which was 5.6.33?
Hey Will, I haven’t updated this article since it came out 4 years ago. So by now some of it is probably outdated. :/
The issue may be related to php 5.6 as it’s no longer supported, so you could try doing it with php 7
As I don’t work as much with php as in the past I probably won’t update the article with the instructions for php 7.
If you figure out a easy solution to fix your problem let me know. :)
Cheers,
Robin