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:
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.
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 http://download.suhosin.org/suhosin-patch-5.2.14-0.9.7.patch.gz
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 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
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!
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
7 thoughts on “Install Two Versions of PHP on Ubuntu Lucid 10.04 – PHP 5.3 and PHP 5.2”
First of all thanks for this howto. I tried to do but i’m getting the “somethings went wrong, check the log file”message doing the sudo ./configure
Where exactly does it show the ‘somethings went wrong’ error message? Does it seem to go through a lot of stuff before it shows this message?
I remember having this error once before – it may have been based on the IMAP function. If you don’t need the IMAP function (you are not running a webmail client on your system that uses IMAP), remove this from the configure line and see if that helps.
What’s the best way to check if this worked? I dropped a phpinfo file into the web directory that I am testing but it still shows 5.3.2 at the top.
Did you potentially skip the step of adding the information to the Apache config? Ensure you create that one file – I called “php5-2-14.conf” and it was then placed in the /etc/apache2 folder.
Then you need to ensure you add the:
To the VirtualHost (or your main host config) then reboot apache2. It should work straight away if you do the phpinfo command.
Thanks. It works for me. 🙂
You could also use Virtual Box for PHP 5.2.x and your main OS for 5.3.x and share the workspace/Apache root directory
Thank you. Awesome. Works.