Although Drupals 7+ run smoothly on PHP 5.3, Drupal 6 still feels much better with PHP 5.2. Even though D6 core is compatible with PHP 5.3 for quite some time now, a lot of contributes modules still get nasty hiccup when asked to run on the newer version. Therefore developing for both D7 and D6 at the same time becomes much less painful when running both versions of PHP in parallel.

One way of doing it is using mod_php5 Apache module to serve PHP 5.3 applications, while running PHP 5.2 applications using fastcgi module. Under find any good fix (relatively easy to apply anyway), so finally ended up with the most recent version from 5.2 branch - 5.2.17, where those errors haven't occured anymore.

Let's then download and uncompress PHP sources into ~/Downloads/php-5.2.17 directory:

mkdir -p ~/Downloads/php-5.2.17
cd ~/Downloads/php-5.2.17
wget http://museum.php.net/php5/php-5.2.17.tar.gz
tar
zxf php-5.2.17.tar.gz

Configure

Time to configure the package. Example ./configure call could look as follows:

sudo ./configure \
  --prefix=/usr/share/php52 \
  --datadir=/usr/share/php52 \
  --mandir=/usr/share/man \
  --bindir=/usr/bin/php52 \
  --with-libdir=lib/php5/libexec \
  --without-mm \
  --with-curl=shared,/usr \
  --with-zlib-dir=/usr \
  --with-gd=shared,/usr \
  --enable-gd-native-ttf \
  --with-gmp=shared,/usr \
  --with-jpeg-dir=shared,/usr \
  --with-xpm-dir=shared,/usr/X11R6 \
  --with-png-dir=shared,/usr \
  --with-freetype-dir=shared,/usr \
  --with-ttf=shared,/usr \
  --with-t1lib/x86_64-linux-gnu \
  --with-pdo-sqlite=shared \
  --with-sqlite=shared \
  --enable-ipv6 \
  --with-mcrypt \
  --with-imap-ssl

Obviously you need to adapt it to yur specific needs by adding and/or removing relevant options. You can read more about options you want (or don't want) to include in PHP core configure options documentation.

Configure errors

Now, that probably didn't work out of the box, did it?

In most cases quite a lot of dependencies will be missing. You can try to take care of them in one shot, if you don't care too much about installing a little too much compared to what is really needed:

sudo apt-get install libxml2-dev libpcre3-dev libbz2-dev libcurl4-openssl-dev libdb4.8-dev libjpeg-dev libpng12-dev libxpm-dev libfreetype6-dev libmysqlclient-dev postgresql-server-dev-9.1 libt1-dev libgd2-xpm-dev libgmp-dev libsasl2-dev libmhash-dev unixodbc-dev freetds-dev libpspell-dev libsnmp-dev libtidy-dev libxslt1-dev libmcrypt-dev

You can also remedy missing dependencies one by one, and install only those packages that are really needed.

Let's go through some of the possible errors then (you can skip to the next section if your ./configure finished without any errors and displayed nice Thank you for using PHP at the end of its execution):

  • error: xml2-config not found. Please check your libxml2 installation.

    This error message suggests you don't have libxml2 installed. What it really means though is that you don't have its development version installed!

    Let's then search what we can configure: find pcre.h in /usr

    sudo apt-get install libpcre3-dev
  • error: Please reinstall the BZip2 distribution

    sudo apt-get install libbz2-dev
  • error: Please reinstall the libcurl distribution - easy.h should be in /include/curl/

    sudo apt-get install libcurl4-openssl-dev
  • error: DBA: Could not configure: configure: configure: configure: configure: configure: Cannot configure: lib distribution is not installed correctly. Please reinstall it.

    sudo apt-get install libt1-dev
  • error: Unable to configure: configure: Cannot configure: configure: cannot configure: configure: configure: Cannot configure: configure: Cannot configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

    sudo apt-get install libxslt1-dev
  • error: mcrypt.h not found. Please reinstall libmcrypt.

    sudo apt-get install libmcrypt-dev
  • error: find OpenSSL's libraries

    Add following switch to your ./configure options (optionally updating the path to reflect your system):

    --with-libdir=/configure: error: You've configured extension pdo_sqlite to build statically, but it depends on extension pdo, which you've configured to build shared. You either need to build pdo_sqlite shared or build pdo statically for the build to be successful.

    Add following switches to your ./configure options:

      --with-pdo-sqlite=shared
      --with-sqlite=shared

Make

Everything configured properly and without errors? Then it is time to compile (and go for a coffee while it is running):

sudo make

Now sipping your coffee wait for approaching errors...

Make errors

Yes, things can go awry here too. And most probably will.

There are two make errors and one warning you are most probably going to experience:

  • ext/openssl/.libs/xp_ssl.o: In function `php_openssl_setup_crypto':
    ext/openssl/xp_ssl.c:357: undefined reference to `SSLv2_server_method'
    ext/openssl/xp_ssl.c:337: undefined reference to `SSLv2_client_method'

    This is due to PHP bug #54736, which could be easily resolved using patch debian_patches_disable_SSLv2_for_openssl_1_0_0.patch attached to that bug report.

    Download this patch to your ~/Downloads/php-5.2.17 directory, and execute:

    patch -p1 < debian_patches_disable_SSLv2_for_openssl_1_0_0.patch.patch

    You should see friendly success messages like these:

    patching file ext/openssl/xp_ssl.c
    Hunk #1 succeeded at 332 (offset 4 lines).
    Hunk #2 succeeded at 354 (offset 4 lines).
    Hunk #3 succeeded at 583 (offset -50 lines).
    Hunk #4 succeeded at 819 (offset -98 lines).
  • ext/gmp/gmp.c: In function ‘zif_gmp_random’:
    ext/gmp/gmp.c:1399:69: lib/.libs/zip_dirent.o: In function `memset':
    /usr/include/x86_64-linux-gnu/bits/string3.h:82: warning: memset used with constant zero length parameter; this could be due to transposed parameters

    Finally PHP bug #53568, with a pretty easy fix: edit file ext/zip/lib/zip_dirent.c and in line 478 replace:

    memset(&tm, sizeof(tm), 0);

    with
    memset(&tm, 0, sizeof(tm));

There, no more errors!

Install

The simplest way of installing your new shiny PHP 5.2 now is:

sudo make install

Alternatively, if you plan to repeat the same installation again on other machines, or just want to keep the installation package file for the future, you can run:

sudo checkinstall

which, together with installing new PHP, would also create .deb package file in your current directory.

Whichever way you choose, now you have PHP 5.2 installed in /usr/bin/php52

Apache conf files

The /etc/php52/apache2/ configuration directory for PHP 5.2 should have already been created during the installation process (create it now if it is not the case). Now you need to let PHP 5.2 use the same extensions as your version 5.3 is using:

sudo ln -s /etc/php5/conf.d /etc/php52
sudo ln -s /etc/php5/cli /etc/php52

Copy example php.ini-recommended file provided with PHP installation into its new directory (which will be defined in the next step):

sudo cp php.ini-recommended /etc/php52/apache2/php.ini

and make required amends to it if need be.

Apache and FastCGI

Next thing to do is to set up Apache to run PHP 5.2 using FastCGI. Start with installing fastcgi module:

sudo apt-get install libapache2-mod-fastcgi

Make sure all required modules are enabled and restart Apache:

sudo a2enmod cgi fastcgi actions
sudo service apache2 restart

Create a wrapper script called php52-cgi to run the fastcgi enabled version of PHP and place it under /usr/lib/cgi-bin/php52-cgi

Finally create new include file /etc/apache2/php52.conf, which will be used by those virtual hosts that need to run off PHP 5.2. Add the following content to it:

# Include file for virtual hosts that need to run PHP 5.2

<FilesMatch "\.php">
   SetHandler application/x-httpd-php5
</FilesMatch>

ScriptAlias /php52-cgi /usr/Include php52.conf

and reload new configuration:

sudo service apache2 reload

Final result? For all you Drupallers out there - you successfully managed to change this:

into this:

Well done!

References