Debian Squeeze comes with PHP 5.3. This is good, because PHP 5.2 is no longer maintained, but causes problems for old applications that can’t support it (e.g. ViArt 3, because it uses Zend Encoder).
Rather than permanently downgrade PHP for the whole server, I opted to install PHP 5.2 alongside it using FastCGI. It took some trial-and-error to get it to compile with all the features I need, but here is how I did it:
Directory structure
This is the directory structure (at the end), so you can visualise it:
/etc/
apache2/
sites-enabled/
test.conf <-- Apache site configuration
/opt/
php52/
bin/
php-cgi <-- PHP CGI binary
...
...
/home/
sites/
test/
cgi-bin
php52.fcgi <-- FastCGI wrapper script
conf/
php.ini <-- PHP configuration
htdocs/
phpinfo.php <-- Test script
How to install
Install & enable FastCGI
sudo apt-get install libapache2-mod-fastcgi sudo a2enmod actions fastcgi sudo /etc/init.d/apache2 restart
Download PHP
mkdir ~/php5-build cd ~/php5-build wget http://uk3.php.net/get/php-5.2.17.tar.bz2/from/this/mirror -O php-5.2.17.tar.bz2 tar jxf php-5.2.17.tar.bz2 cd php-5.2.17/
Download dependancies
sudo apt-get build-dep php5 sudo apt-get install libfcgi-dev libmhash-dev
Compile PHP
./configure \
--prefix=/opt/php52 \
--enable-force-cgi-redirect \
--enable-fastcgi \
--with-regex=php \
--enable-calendar \
--enable-sysvsem \
--enable-sysvshm \
--enable-sysvmsg \
--enable-bcmath \
--with-bz2 \
--enable-ctype \
--with-iconv \
--enable-exif \
--enable-ftp \
--with-gettext \
--enable-mbstring \
--with-pcre-regex \
--enable-shmop \
--enable-sockets \
--enable-wddx \
--with-libxml-dir=/usr \
--with-zlib \
--with-openssl=/usr \
--enable-soap \
--enable-zip \
--with-mhash=yes \
--with-gd \
--with-mysql \
--with-mysqli \
--with-pdo-mysql \
--with-pear \
--with-jpeg-dir=/usr/lib \
--enable-gd-native-ttf \
--with-ttf \
--with-freetype-dir=/usr
make
sudo make install
sudo chmod o+rX -R /opt/php52/
Create a FastCGI wrapper script
mkdir /home/sites/test/cgi-bin/ chmod o+rX /home/sites/test/cgi-bin/ vim /home/sites/test/cgi-bin/php52.fcgi
#!/bin/sh export PHP_FCGI_CHILDREN=4 export PHP_FCGI_MAX_REQUESTS=200 export PHPRC="/home/sites/test/conf/php.ini" exec /opt/php52/bin/php-cgi
chmod a+rx /home/sites/test/cgi-bin/php52.fcgi
Set the site to use FastCGI
vim /etc/apache2/sites-enabled/test.conf
<VirtualHost *>
...
# Use FastCGI version of PHP
php_admin_flag engine off
ScriptAlias /cgi-bin /home/sites/test/cgi-bin
Action application/x-httpd-php /cgi-bin/php52.fcgi
</VirtualHost>
Restart Apache
sudo /etc/init.d/apache2 restart
Notes
It took me many hours to get this right. Here are some of the things I learned:
1. If you don’t have libfcgi-dev installed when you compile PHP, it will compile fine, but FastCGI will time out when you try to use it.
2. Make sure the php52.fcgi script is saved in Unix line-ending format not Windows – otherwise, again, FastCGI will hang for 30 seconds then time out. Test the script by running it at the command line.
3. If you miss out the --with-jpeg-dir=/usr/lib switch the first time, you must run make clean before PHP will compile a second time with JPEG support.
4. If you make any changes to the wrapper script (php52.fcgi), you must kill the existing instances before you will see the changes. (I did this by restarting Apache, but I suspect there is a more graceful way!)
5. You do not have to make the PHP scripts executable, or add the shebang to the top – it is the wrapper script that is executed, not the PHP scripts.
Comments
1. tucsi
Thanks! It was a good solution to my problem! :)
Posted on 4th June 2011 at 9:06pm.
2. Terry
Many thanks!
This has allowed me to run an old Drupal 5 site alongside my newer php53 sites.
Posted on 25th June 2011 at 8:57pm.
3. piwam sur Debian Squeeze « Francois BAYART
[...] vais donc suivre l’explication du site Dave James Miller pour mettre en place PHP5.2 en plus de PHP5.3 [...]
Posted on 21st July 2011 at 7:53pm.
4. piwam sur Debian Squeeze | LoLiGrUB ASBL
[...] vais donc suivre l’explication donnĂ©e par le site Dave James Miller pour mettre en place PHP5.2 en plus de PHP5.3 [...]
Posted on 23rd July 2011 at 4:53pm.
5. Bernard Toplak
First of all thanks for sharing this tipp, it would also cost me some hours on debugging dependencies.
I have one more to add. When compiling php-5.2.17, it needs “libmhash-dev”, so add this package too to the tutorial!
Posted on 8th August 2011 at 3:06pm.
6. Dave James Miller
Thanks Bernard, I’ve added that.
Posted on 9th August 2011 at 1:10pm.
7. PHP 5.2 su Debian Squeeze | Ceci n'est pas une pipe
[...] http://blog.davejamesmiller.com/2011/03/how-to-install-php-5-2-fastcgi-on-debian-6-0-squeeze [...]
Posted on 30th October 2011 at 10:50pm.