Install Two Versions of PHP on Ubuntu Lucid 10.04 – PHP 5.3 and PHP 5.2

OK – so after my last long post on how to downgrade Ubuntu Lucid 10.04 back to PHP 5.2 (which happened to be version 5.2.10), I wasn’t to satisfied with this plan.  Afterall, what is the point of going backwards for every site if you don’t need to?

Well, after I reverted back to PHP 5.2 on Ubuntu 10.04 Lucid, I wanted to research this topic a bit further.

Alas, I read several HOW-TO pages and found out how to install both PHP 5.2 and 5.3 on Ubuntu Lucid 10.04 so that they both work at the SAME TIME!.  Yes, the same time!  No need to remove one and start the other when needing to use it.

The catch is – one of them has to be run in fastcgi mode.  No big deal though.

So, if you followed my other post on how to revert back to PHP 5.2 on Ubuntu 10.04, you will need to do the following before continuing with the next instructions.  If you did not revert back to PHP 5.2, you do not need this.

1. Simply go into the /etc/apt/sources.list.d folder and delete that file you created that pulled items from the Karmic repositories.  My file was just called karmic.list.
2. Now go into the /etc/apt/preference.d folder and delete the file you created which listed all of your preferences for packages you wanted only pulled from Karmic.  Mine was just called “php” so I deleted this file.
3. Now, just perform the following command and it will download PHP 5.3.2 included with Ubuntu Lucid 10.04 and any other updates that you may need:

sudo apt-get update && sudo apt-get upgrade

Now install all of those items!

OK, so if you did not follow a guide on how to downgrade to PHP 5.2 in Ubuntu 10.04 Lucid, you start here!

At the time of this writing, the newest version of PHP 5.2 is PHP 5.2.14.  Heck, even that is an upgrade over the Karmic version of PHP 5.2.10 that was installed previously!

Now do not let these instructions get the better of you.  They are pretty simple to follow.  You have to install some development packages to allow PHP 5.2.14 to build and be used.  I tried to remove these after I finished compiling and installing PHP 5.2.14, but then I began receiving Internal Server Error messages on the one site that needs the old version.

While you may not need all of these packages below, I did in order to replicate the same modules and configuration that I was using.  Note that this DOES NOT include imagick – which I could not get installed.  But, I haven’t noticed any problems with the site without this installed.

First, install the needed dependencies.  Some of these may already be installed:

sudo apt-get install curl libcurl4-openssl-dev libcurl3 libcurl3-gnutls zlib1g zlib1g-dev libzip-dev libzip1 libxml2 libsnmp-base libsnmp15 libxml2-dev libsnmp-dev libjpeg62 libjpeg62-dev libpng12-0 libpng12-dev zlib1g zlib1g-dev libfreetype6 libfreetype6-dev libbz2-dev libmcrypt-dev libmcrypt4 libc-www007e libmysqlclient15-dev

This will install all of the dependencies you need to install PHP 5.2.14 on Ubuntu Lucid 10.04.

OK, now that those are installed, you need to head over to the php page and download the version of PHP you want to use.  Make sure you download the TAR.GZ version and NOT the TAR.BZ2 if you are following my tutorial.

Now after you have it downloaded, you need to move it to the proper directory.  Now because I didn’t want all kinds of files scattered throughout the server drive, I installed everything in the /opt location.  So, move your newly-downloaded file to that directory:

sudo mv php-5* /opt

OK, so that is moved. Now you need to go to that directory and untar the file:

cd /opt
sudo tar xvf php-5*

That should then create a new directory. If you are installing PHP 5.2.14, it will be the php-5.2.14 directory. So change the directory (cd) into that folder.

cd php-5.2.*

OPTIONAL:  If you want to install the Suhosin Hardening Patch (which is installed be default with Ubuntu releases), you need to download this patch file:

sudo wget
sudo gunzip suhosin*
patch -p 1 -i suhosin*

Now, this is the part where most people take a big gulp and think they cannot compile PHP from source. This is how you compile PHP 5.2.14 from source on Ubuntu 10.04 Lucid. Of course, it really helps when you know what to type in! So, copy or type in the following large command. Copying from this site will eliminate any typos or errors:

sudo ./configure –prefix=/opt/php5.2.14 –with-mysql=/usr –with-mysqli=/usr/bin/mysql_config –with-curl=/usr/bin –with-curlwrappers –with-openssl-dir=/usr –with-zlib-dir=/usr –enable-mbstring –with-pdo-mysql=/usr –with-ldap –with-xmlrpc –with-iconv-dir=/usr –with-snmp=/usr –enable-exif –enable-calendar –with-bz2=/usr –with-mcrypt=/usr –with-gd –with-jpeg-dir=/usr –with-png-dir=/usr –with-zlib-dir=/usr –with-freetype-dir=/usr –enable-mbstring –enable-zip –with-pear –enable-cli –enable-fastcgi –enable-discard-path –enable-force-cgi-redirect –with-imap –with-imap-ssl –enable-bcmath –enable-ctype –enable-dba –enable-dom –enable-filter –enable-ftp –with-gettext –enable-hash –enable-json –enable-libxml –with-mime-magic=/usr/share/file/magic.mime –with-pcre-regex –enable-sockets –enable-soap –with-kerberos –enable-shmop –enable-wddx –with-openssl –without-sqlite

That is a HUGE line isn’t it? Well, basically what that is doing is telling the PHP installer what kinds of modules and functions you want installed. Now, you may not need some of those to be installed, but it really isn’t going to hurt if you do – just some extra hard drive space.

Now, do this command – and it will install PHP for you into the /opt/php5.2.14 directory (notice how that is specified in the huge command above where it says “–prefix=/opt/php5.2.14”. By adding this line, it ensures all files are placed here instead of scattered throughout your disk drive).

sudo make
sudo make install

Believe it or not, you have not compiled and installed PHP 5.2.14 on Ubuntu 10.04 now!  One more step.  What I would suggest is you copying your php.ini file from the /etc/php5 directory (which is the one for the PHP 5.3.2) since you will also need a configuration file for PHP 5.2.14.  That way you can also change any config for the specific PHP installs as well:

sudo cp /etc/php5/apache2/php.ini /opt/php5.2.14/php.ini

Alright, now you are not done yet! Now that you have PHP 5.2 compiled, you need to setup the Apache web server (I’m assuming you already have this installed too) to use both PHP 5.3 and PHP 5.2 simultaneously. To do so, issue this command – which enables the “actions” modules for Apache:

sudo a2enmod actions

You need to run that because of a file that will use the “Action” directive just in a few moments. Let’s make that file now. Copy what I have below and create a new file in the /etc/apache2 directory called “php5-2-14.conf” – makes it easy to understand that the file is the configuration data to allow PHP 5.2 and PHP 5.3 to both be installed on Ubuntu 10.04.

ScriptAlias /cgi-bin-php/ “/opt/php5.2.14/bin/”
SetEnv PHP_INI_SCAN_DIR “/opt/php5.2.14/”
AddHandler php-script .php
Action php-script /cgi-bin-php/php-cgi
<FilesMatch “\.php”>
SetHandler php-script

Now, save that file. What does that file do? Well, it points an alias of “cgi-bin-php” to the location of your newly installed PHP 5.2.14 – “/opt/php5.2.14/”. Then it tells Apache where the php.ini file is at (which you copied to the directory per the above instructions, right?) Then, there is the “FilesMatch” area which basically says – match all files that end with a .php extension. It then tells Apache to open all php files (SetHandler) with the php-script Action that is just above the “FilesMatch”.

OK, enough with providing information on what the file does. Believe it or not, you have two more things to do now!

Go into your /etc/apache2/httpd.conf file (or wherever you have your VirtualHost settings), and add the following line somewhere within the VirtualHost. By adding this line, you are telling Apache to load that file and use the settings from the file (which was just made) for configuring the VirtualHost to use PHP 5.2.14. You only need to add this line to the VirtualHost areas that you want to use the older version of PHP (the one you just installed). If you want the other VirtualHost domains to use the PHP 5.3.2 version which is installed with Ubuntu 10.04, do not add this line!

Include /etc/apache2/php5-2-14.conf

Now, restart apache and you should be all done!

sudo apache2ctl restart

And there it is! You have just installed both PHP 5.2 and PHP 5.3 on Ubuntu Lucid 10.04 and they will both work at the SAME time! Now, if you so-desire, you may want to delete the PHP TAR file that you downloaded and remove the directory that was untarred to save space. These two items take up about 120 MB when the compiled PHP only takes about 60 MB.

sudo rm /opt/php-5.2.14.tar.gz
sudo rm -r /opt/php-5.2.14