How to install PHP 5.2 FastCGI on Debian 6.0 Squeeze

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

1
2
3
sudo apt-get install libapache2-mod-fastcgi
sudo a2enmod actions fastcgi
sudo /etc/init.d/apache2 restart

Download PHP

1
2
3
4
5
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

1
2
sudo apt-get build-dep php5
sudo apt-get install libfcgi-dev libmhash-dev

Compile PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
./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
make
sudo make install
sudo chmod o+rX -R /opt/php52/

Create a FastCGI wrapper script

1
2
3
mkdir /home/www/test/cgi-bin/
chmod o+rX /home/www/test/cgi-bin/
vim /home/www/test/cgi-bin/php52.fcgi
1
2
3
4
5
#!/bin/sh
export PHP_FCGI_CHILDREN=4
export PHP_FCGI_MAX_REQUESTS=200
export PHPRC="/home/www/test/conf/php.ini"
exec /opt/php52/bin/php-cgi
1
chmod a+rx /home/www/test/cgi-bin/php52.fcgi

Set the site to use FastCGI

1
vim /etc/apache2/sites-enabled/test.conf
1
2
3
4
5
6
7
8
9
10
<VirtualHost *>
 
    ...
 
    # Use FastCGI version of PHP
    php_admin_flag engine off
    ScriptAlias /cgi-bin /home/www/test/cgi-bin/
    Action application/x-httpd-php /cgi-bin/php52.fcgi
 
</VirtualHost>

Restart Apache

1
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.

Posted on 13th March 2011. 6 comments.

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.

Add a Comment