Every now and then you’ll need to migrate a couple of old websites to a new Plesk server that require an earlier version of PHP than you’re currently using. Drupal 5 is a great example of this; it just won’t run on a Linux box running PHP 5.3 or later, of course you can use some hackery but that’s usually not going to cut it. So, how do you get PHP 5.2 on a Plesk 11 server that’s already running PHP 5.3? Easy:

First you’ll need to download the version of PHP you want to use from museum.php.net via wget, I used PHP 5.2.17. Then unpack the tarball. It’s required that you do this while loged in via an SSH shell.

# wget http://museum.php.net/php5/php-5.2.17.tar.gz
# tar zxvf php-5.2.17.tar.gz

Once you’ve unpacked the tarball cd into the folder that was created, now it’s time to compile PHP. We don’t compile PHP with the usual parameters, if you do it’ll destroy your current PHP install, so we use the /usr/local/bin prefix. I used the following parameters for PHP 5.2.17 on a server running Plesk 11 on a CentOS 6 x64 box.

‘./configure’ ‘–build=x86_64-unknown-linux-gnu’ ‘–host=x86_64-unknown-linux-gnu’ ‘–target=x86_64-redhat-linux-gnu’ ‘–with-libdir=lib64′ ‘–cache-file=../config.cache’ ‘–prefix=/usr/local/bin/php52-cgi’ ‘–with-config-file-path=/usr/local/bin/php52-cgi/etc’ ‘–disable-debug’ ‘–with-pic’ ‘–disable-rpath’ ‘–with-bz2′ ‘–with-curl’ ‘–with-freetype-dir=/usr/local/bin/php52-cgi’ ‘–with-png-dir=/usr/local/bin/php52-cgi’ ‘–enable-gd-native-ttf’ ‘–without-gdbm’ ‘–with-gettext’ ‘–with-gmp’ ‘–with-iconv’ ‘–with-jpeg-dir=/usr/local/bin/php52-cgi’ ‘–with-openssl’ ‘–with-pspell’ ‘–with-pcre-regex’ ‘–with-zlib’ ‘–enable-exif’ ‘–enable-ftp’ ‘–enable-sockets’ ‘–enable-sysvsem’ ‘–enable-sysvshm’ ‘–enable-sysvmsg’ ‘–enable-wddx’ ‘–with-kerberos’ ‘–with-unixODBC=/usr’ ‘–enable-shmop’ ‘–enable-calendar’ ‘–with-libxml-dir=/usr/local/bin/php52-cgi’ ‘–enable-pcntl’ ‘–with-imap’ ‘–with-imap-ssl’ ‘–enable-mbstring’ ‘–enable-mbregex’ ‘–with-gd’ ‘–enable-bcmath’ ‘–with-xmlrpc’ ‘–with-mysql=/usr’ ‘–with-mysqli’ ‘–with-snmp’ ‘–enable-soap’ ‘–with-xsl’ ‘–enable-xmlreader’ ‘–enable-xmlwriter’ ‘–enable-pdo’ ‘–with-pdo-mysql’ ‘–with-pdo-pgsql’ ‘–with-pear=/usr/local/bin/php52-cgi/pear’ ‘–with-mcrypt’ ‘–without-pdo-sqlite’ ‘–without-unixODBC’ ‘–enable-force-cgi-redirect’ ‘–enable-fastcgi’ ‘–with-config-file-scan-dir=/usr/local/bin/php52-cgi/php.d’

After you’ve compiled PHP successfully you can install it by using the following commands:

# make
# make install

After everything has been installed correctly it’s time to enable it on a subscription. But before we can do this it’s important to put PHP in FastCGI mode on that subscription, all steps mention below need to be followed every time you want to use PHP 5.2 on a new subscription.

# cd /var/www/vhosts/domain.com/cgi-bin
# mkdir .cgi_wrapper
# cd .cgi_wrapper
# touch .phpwrapper

Once you’ve created the wrapper in the subscription, fill it with the following script.

#!/bin/sh
export PHPRC=/var/www/vhosts/domain.com/etc/
export PHP_FCGI_CHILDREN=4
export PHP_FCGI_MAX_REQUESTS=1000
exec /usr/local/bin/php52-cgi/bin/php-cgi

Now it’s time to secure the wrapper and set the right owner on the file.

# cd /var/www/vhosts/domain.com/cgi-bin
# chmod 101 .cgi_wrapper
# chmod 500 .cgi_wrapper/.phpwrapper
# chown domain.com:psacln .cgi_wrapper -R
# chattr -R +i .cgi_wrapper

The wrapper is just there to call on PHP 5.2, before it can do that we need to activate it in vhost.conf.

# cd /var/www/vhosts/domain.com/conf
# touch vhost.conf

Fill this file with the following script.

<Directory /var/www/vhosts/domain.com/httpdocs>
RemoveHandler fcgid-script
<IfModule mod_fcgid.c>
AddHandler fcgid-script .php
<Files ~ (\.php)>
SetHandler fcgid-script
FCGIWrapper /var/www/vhosts/domain.com/cgi-bin/.cgi_wrapper/.phpwrapper .php
Options +ExecCGI
allow from all
</Files>
</IfModule>
</Directory>

Now that we’re all set and don you’ll need to reconfigure the domain and restart Apache.

# /usr/local/psa/admin/sbin/httpdmng –reconfigure-domain domain.com
# /etc/init.d/httpd restart

And now, every time you use PHP within that subscription, it’ll use the version of PHP the wrapper is pointing to. In this case PHP 5.2!

Hopefully this helps you when trying to run old code on new Plesk servers, if you have any questions feel free to ask them in the comment section!