PHP-5.2 and PHP-5.3 for Drupal applications under Apache on Ubuntu 10.04 LTS

By Christoph Breidert  

Why do this?

For running legacy Drupal applications it can be necessary to have different version of PHP on your system. Drupal5 runs on PHP-5.2, Drupal6 and later versions run on PHP-5.3, more about this here: http://drupal.org/requirements.

In a situation like this you might want the PHP-5.2 application (e.g., Drupal5) and the PHP-5.3 application (e.g., Drupal6) to be served from the same Apache. One way of doing it is using mod_php5 to serve PHP-5.3 applications and run PHP-5.2 applications over fast-cgi. Under Ubuntu 10.04 this can be achieved by installing PHP-5.3 from the repository and manually compiling and installing PHP-5.2.

I was facing this problem and this post is a write-up what I did to get it running in a step-by-step tutorial with all necessary resources and configurations.

There are two excellent blog posts, about how this can be done. The earlier is from Dave Parrish, a bit later came Brett with a nice tutorial. However, in spite of these excellent posts I encountered some pitfalls and discovered some caveats.

Setup Apache with mod_php5 (PHP-5.3)

Under Ubuntu it is really easy to set up apache with php. You simply install the necessary packages from the repository. The php version shipped with Ubuntu 10.04 is PHP-5.3. In my test environment I have the following installed:

sudo apt-get install apache2
sudo apt-get install php5 php5-dev libapache2-mod-php5

Checking localhost by directing the browser to http://localhost shows apache’s index.html saying It works!

To test if php works create a php file with f.x.

echo "<?php phpinfo(); ?>" | sudo tee /var/www/testphp.php

Testing php by directing the browser to http://localhost/testphp.php shows the php version information for PHP-5.3 from mod_php5.

To set up Drupal6 you can also install from the repositories by running

sudo apt-get install mysql-server
sudo apt-get install drupal6

With this setup Drupal6 applications can painlessly be run. However for production you might want to set up Drupal differently, here is some guidance: http://drupal.org/node/251019.

Install and test PHP-5.2

To install PHP-5.2 download the latest version from http://php.net/downloads.php to some local directory. Change into the directory and unpack php and change into the resulting directory

gunzip php-5.2.17.tar.gz
tar -xf php-5.2.17.tar
cd php-5.2.17/

Install the build tools to manually compile PHP-5.2.

sudo apt-get install libxml2-dev libmysqlclient-dev libcurl4-gnutls-dev libpng12-dev libjpeg62-dev

Then configure and install PHP-5-2 to /opt/php5.2

./configure --prefix=/opt/php5.2 --with-config-file-path=/opt/php5.2 --with-mysqli --with-mysql --with-curl --with-gd --with-jpeg-dir --enable-cli --enable-fastcgi --enable-discard-path --enable-force-cgi-redirect
make
sudo make install

Now you have PHP-5.2 installed in /opt/php5.2

You can test if PHP-5.2 works by having the testphp.php interpreted by the cgi binary

/opt/php5.2/bin/php /var/www/testphp.php

Set up Apache for FastCGI

Next thing to do is to set up apache to run PHP-5.2 over FastCGI

sudo apt-get install libapache2-mod-fastcgi

Make sure all neccesary modules are enabled in apache

sudo a2enmod cgi
sudo a2enmod fastcgi
sudo a2enmod actions
sudo /etc/init.d/apache2 restart

It is smart  to use a shell script that executes PHP-5.2 for you, in order to configure FastCGI and make a php.ini available. To do this create a file name php52-cgi and place it in Apaches cgi-bin directory /usr/lib/cgi-bin/ with the following content

#!/bin/sh
PHPRC="/opt/php5.2/"
export PHPRC
PHP_FCGI_CHILDREN=4
export PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS
exec /opt/php5.2/bin/php-cgi

The first parameter tells PHP-5.2 where to look for a php.ini, the second pre-fork threads with the FastCGI process manager, the third limits the requests. The last line executes php. More about this can be found at http://www.fastcgi.com/drupal/node/5?q=node/10.

Make sure the file is executable

sudo chmod +x /usr/lib/cgi-bin/php52-cgi

Please note that there is no php.ini in /opt/php5.2/. You have to manually copy and rename the php.ini that was shipped with PHP-5.2. It is located at in the php folder you unzipped at php-5.2.17/php.ini-recommended.

You can test if the script works by executing

/usr/lib/cgi-bin/php52-cgi < /var/www/testphp.php

To set up Apache create a Include file named php52.conf at /etc/apache2/ that can be used in any virtual host, which needs to use PHP-5.2. Write the following content in the /etc/apache2/php52.conf

#include for virtual hosts that need to run php-5.2
<FilesMatch "\.php">
   SetHandler application/x-httpd-php5
</FilesMatch>
ScriptAlias /php52-cgi /usr/lib/cgi-bin/php52-cgi
Action application/x-httpd-php5 /php52-cgi
AddHandler application/x-httpd-php5 .php

The FilesMatch forces the handler to execute files with the suffix .php. This must be set, because if mod_php with PHP-5.3 is installed, the module interprets the php file and not the cgi script. The ScriptAlias sets the path to the cgi script. The AddHandler registers a new handler. The Action activates the cgi script for the files passed from the handler. You can read up on this under http://httpd.apache.org/docs/2.2/mod/mod_actions.html, http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler, and http://httpd.apache.org/docs/2.2/mod/core.html#sethandler.

See if it works

For testing you can runa PHP-5.3 application and a PHP-5.2 application in two different virtual hosts. Create a new entry in /etc/hosts with the line

127.0.0.1	php52.localhost php53.localhost

Now when you ping php52.localhost or php53.localhost you should receive packages from your local apache on the ip-address 127.0.0.1.

Now we create two virtual host files in /etc/apache2/sites-available, one named php52 and php53. In the php52 we add the Include file created earlier.

php52:

<VirtualHost *:80>ServerAdmin webmaster@localhostServerName php52.localhostDocumentRoot /var/www#Include php5.2Include php52.conf
ErrorLog /var/log/apache2/error.log# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.LogLevel debugCustomLog /var/log/apache2/access.log combined </VirtualHost>

php53:

<VirtualHost *:80>ServerAdmin webmaster@localhostServerName php53.localhostDocumentRoot /var/wwwErrorLog /var/log/apache2/error.log# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.LogLevel debugCustomLog /var/log/apache2/access.log combined </VirtualHost>

Enable the new virtual hosts and restart apache

cd /etc/apache2/sites-enabled
sudo ln -s ../sites-available/php52 php52
sudo ln -s ../sites-available/php52 php52
sudo /etc/init.d/apache2 restart

When you now direct your browser to http://php52.localhost/testphp.php you should see the output of PHP-5.2, when you direct your browser to http://php53.localhost/testphp.php you see the output of PHP-5.3.

So for any virtual host that requires PHP-5.2 you simply need to add the include /etc/apache2/php52.conf.

Have fun.


37 Comments

  1. Posted March 4, 2011 at 19:35 | Permalink | Reply

    Excellent post Chris. Very thorough. Frankly, I’m not sure why mine worked out of the box. I play with Apache servers for a living and perhaps made some modifications that I forgot about.

    Thanks for your contribution to the community. Many PHP developers will profit.

    Brett

    • Posted March 4, 2011 at 19:41 | Permalink | Reply

      I don’t know either, maybe it is the ubuntu version …

      I really banged my head on the keyboard over this one for some days….

      Thx again for your help, I appretiate it.

      Cheers, Chris

  2. Posted April 8, 2011 at 15:56 | Permalink | Reply

    Thanks Chris,

    I followed Bret’s description which wasn’t worked, then your version did what I expected.

    There are two differences:
    1)
    PHPRC=”/opt/php5.2/”
    export PHPRC

    2)

    SetHandler application/x-httpd-php5

    I don’t know which was the crutial, maybe the second one.

    Many thanks again!
    Péter

  3. Posted April 25, 2011 at 00:14 | Permalink | Reply

    Christoph: I greatly appreciate this post. I use the same OS for my servers, so having this all in one place was really helpful.

    I differ in that I’m running PHP using fcgid…simply because that’s what Virtualmin sets up :) but it’s about the same as FastCGI in terms of configuration. It’s a bit easier to use wrappers. Let me know if you want more details and I’ll try to provide them (or to blog about it…)

    • Posted April 25, 2011 at 07:21 | Permalink | Reply

      Hi Kevin,

      thx for your comment. Sounds interesting, probably it’s best if you blog about your setup, let me know, I will then set a link to your blog.

      Happy easter,

      Christoph

  4. Posted April 25, 2011 at 08:59 | Permalink | Reply

    Thanks. Hey, I am wondering if you know how to compile eAccelerator for PHP 5.2? I got as far as /opt/php5.2/bin/phpize but don’t know how to override the other paths (which point to PHP 5.3) in the ./configure command. Maybe it’s not worth it, but was curious, and I know I will want to compile things like Xdebug for it…though maybe I can just use /opt/php5.2/bin/pecl for that.

  5. Posted April 25, 2011 at 23:09 | Permalink | Reply

    Thanks for this excellent writeup. I’ve gone through the steps and everything has worked correctly up until I tired to implement the final step of getting it to work on one of my sites.

    phpInfo() shows php 5.3. If I disable php using sudo a2dismod php5 none of the php fails altogether. the only major thing I see is that I’m running ISPManager on this box. However, it does let you edit the vhosts files.

    I’m curious if you can think of any additional troubleshooting or configuration steps I might try to get things cooking.

    Thanks,

    -peel

    • Posted April 26, 2011 at 06:58 | Permalink | Reply

      Hi John,
      I assume you have php52 over cgi working from the command line. You can test this with a php file like this:
      /usr/lib/cgi-bin/php52-cgi < /var/www/testphp.php.
      Then the problem must be your apache configuration. You could check if all necessary modules are enabled (cgi, fastcgi, actions). If that is ok, you could test pasting the code from the include file directly into your main apache conf file.
      Good luck, and please drop me a line if you figure out what the problem was.
      Cheers, Christoph

  6. Posted April 27, 2011 at 00:27 | Permalink | Reply

    Hey Christoph,

    I actually figured it out. When you run ./configure on the PECL extension, do:

    ./configure –with-php-config=/path/to/php/bin/php-config

    Works like a charm.

    I always say I’m going to blog about things but never do. But I’ll see if I can. Pretty excited about this working.

  7. Posted May 9, 2011 at 10:36 | Permalink | Reply

    Hi Christoph

    Thanks for your post!

    It helped me set up a local dev environment for a legacy CodeIgnitor application that I took over.

    It seems like the upgrade from PHP 5.2 to 5.3 caught a number of people off guard…

    Regards
    Stuart

  8. krzys
    Posted May 30, 2011 at 12:16 | Permalink | Reply

    Very goog tut, thanks so much!!:) You are great!

  9. Posted July 9, 2011 at 12:39 | Permalink | Reply

    Excellent tutorial. I had to add a few of the dev libraries before make would work for me, but a great help.

    Thank you very much for adding this.

    Colin

    • Posted July 18, 2011 at 09:53 | Permalink | Reply

      Thx Colin,

      good feedback is always nice to get. Pls post the libraries you had to add to make it work, maybe this helps others.

      Cheers,

      Christoph

  10. Posted July 11, 2011 at 03:36 | Permalink | Reply

    Wanted to thank you for an excellent guide!

  11. Posted July 31, 2011 at 11:29 | Permalink | Reply

    Awesome post man..

    Thank you .

  12. Posted August 16, 2011 at 12:46 | Permalink | Reply

    Great guide! Thanks for taking the time to put it together – making the separate conf file that you can include in your virtual host definition is really clever. Worked great on Ubuntu 11.04.

  13. Scott
    Posted September 23, 2011 at 18:15 | Permalink | Reply

    Great resource! Thanks a lot for posting this. In addition I installed memcache and apc extensions as follows:

    sudo /opt/php5.2/bin/pecl install memcache
    sudo /opt/php5.2/bin/pecl install apc

    That installs versions built for 5.2 in the /opt/php5.2/ directory structure. Then I enabled the extensions in my 5.2 php.ini file adding:

    extension=memcache.so
    extension=apc.so

    They load successfully and memcache appears to work after configuring it in my php.ini file (session.save_handler = memcache, etc.,). APC loads but when I run apc.php it doesn’t look like APC is actually caching anything (Hits: 1, Misses: 1, Cached Files: 1). Not exactly sure why though.

    I recompiled php 5.2.17 with php-fpm as hinted at here (http://pecl.php.net/bugs/bug.php?id=11988):

    wget -O php-5.2.17-fpm-0.5.14.diff.gz http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz

    After I applied the above patch to a fresh php 5.2 distro I added ‘–enable-fpm’ to my configure arguments and followed the procedure you outline with my patched version of 5.2. I got the impression that using fpm would cause APC to cache as expected (coming from running php as an Apache mod) but still no luck.

    Anyway, aside from my apparently unsuccessful APC modifications this tutorial is excellent. Thanks again!

  14. Posted October 14, 2011 at 23:24 | Permalink | Reply

    I just did your instructions on a clean install of ubuntu 11.10 and found only a few issues -

    You need to link the jpg and png libraries
    sudo ln -s /usr/lib/i386-linux-gnu/libjpeg.so libjpeg.so
    sudo ln -s /usr/lib/i386-linux-gnu/libpng.so libpng.so

    You need to install make
    sudo apt-get install make

    You can use mod-fcgi instead of mod-fastcgi
    sudo apt-get install libapache2-mod-fcgid

    and to restart the apache2 server
    sudo service apache2 restart

    Thanks for the great work here!

  15. Matt White
    Posted October 18, 2011 at 22:08 | Permalink | Reply

    Does this setup actually use FastCGI, or is it using CGI? The reason I ask is that when you turn on mod_fastcgi it only handles requests for scripts that end in .fcgi. This looks like it would be launching PHP in CGI mode, which is super slow… I actually couldn’t get this to work, I had to rename the php52-cgi script to php52.fcgi, which caused it to be launched by mod_fastcgi. Previously to that I was getting premature end of headers errors thrown by Apache.

    • Posted October 20, 2011 at 19:01 | Permalink | Reply

      Hi Matt,
      dunno why it did not work for you. This post is a write-up that worked for me.
      Cheers, Christoph

  16. Posted October 19, 2011 at 23:34 | Permalink | Reply

    Hi,
    really good job …

    Thank you …

    Just for who has an error in libjpeg.so , he must do this :

    ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/libjpeg.so

    just linking where the lib exist ..

    Thanks for the great work here!

  17. Anthony
    Posted October 28, 2011 at 08:41 | Permalink | Reply

    Hi Christoph

    I am running server which uses Plesk and Plesk creates it own conf files. I can add a vhost.conf but what is the best method of including “Include /etc/apache2/php52.conf” in the vhost.conf. I have tried some variants but no luck yet, the site breaks or Apache won’t restart.

    Thanks in advance

    • Posted October 29, 2011 at 11:04 | Permalink | Reply

      Hi Anthony,
      difficult to diagnose from here. You should be able to include a custom php52.conf in your vhosts.conf. Maybe your conf files contain syntax errors, or apache cannot read the custom conf file? You can check this by running apache2ctl configtest before restarting apache.
      Good luck,
      Christoph

  18. dvrslype
    Posted November 9, 2011 at 18:13 | Permalink | Reply

    this made my day, total stress relief! :)

  19. Stefan Ivanov
    Posted February 2, 2012 at 23:24 | Permalink | Reply

    I put any combination of files in “Include” but:
    Invalid command ‘php52.localhostDocumentRoot’, perhaps misspelled or defined by a module not included in the server configuration

  20. william
    Posted February 12, 2012 at 20:51 | Permalink | Reply

    Great guide Christoph. Thank you!

    The only thing I had to add was “–with-pdo-mysql=/usr” to the end of my php5.2 configuration.

    And I also needed these two symlinks:
    ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/libjpeg.so
    ln -s /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/libpng.so

    That fixed the error that the libjpeg.so could not be found.

    My setup:
    Ubuntu 11.10 (64bit)
    Gnome 3.2.1

  21. Posted May 9, 2012 at 13:01 | Permalink | Reply

    Thankx a ton ….It works 100% . Only on virtual host Include php52.conf give the full path like /etc/apache2/php52.conf….

Post a Comment

Your email is never shared. Required fields are marked *

*
*
Easy AdSense by Unreal