It is sometimes necessary to run a legacy application against an older version of PHP in order to start a migration process. The problem is that it’s very difficult to find a platform that still allows for an easy installation of an older version of PHP. This is where specialized stacks, like Linux for PHP (LfPHP), step in. These Docker-based containers allow us to compile almost any version of PHP. In this short article, we’ll be compiling PHP 5.2.17 on Linux for PHP.

Running Linux for PHP

First of all, you need to have Docker on your computer. If you don't, please go to the following URL for instructions: https://docs.docker.com/install/overview/.

Next, you'll need to start a generic Linux for PHP image on your computer from the command line:

docker run -it -p 8181:80 asclinux/linuxforphp-8.1-ultimate:src /bin/bash

This line will instruct Docker to start the ‘src’ image of LfPHP, and make its port 80 available on your computer’s port 8181. Docker will also automatically download all of the LfPHP images that are needed to run the ‘src’ image.

Configuring and Installing PHP 5.2

From the container’s CLI, we can now start downloading, and unpacking PHP 5.2.17:

cd
wget php-5.2.17.tar.gz http://museum.php.net/php5/php-5.2.17.tar.gz
tar -xzvf php-5.2.17.tar.gz
cd php-5.2.17

Once the archive is unpacked, it will be necessary to apply two patches to the PHP package in order to get SimpleXML and the APXS interface to compile smoothly:

wget -O php-5.2.17.patch https://linuxforphp.net/download_file/force/86/0
patch -p0 < ./php-5.2.17.patch
wget -O sapi_apxs_interface_66557.patch https://linuxforphp.net/download_file/force/87/0
patch sapi/apache2handler/php_functions.c < ./sapi_apxs_interface_66557.patch

Then, we are ready to configure the package. We'll be omitting the MySQL/MySQLi compile options, as these would require that we download old MySQL headers in order to avoid some backward compatibility breaks that were introduced over the years by MySQL/MariaDB. Although we are not covering this aspect of compilation in this article, we might cover it in an upcoming article. For now, let’s proceed to configure PHP with PDO, but without MySQL/MySQLi:

./configure --prefix=/usr \
--sysconfdir=/etc \
--localstatedir=/var \
--datadir=/usr/share/php \
--mandir=/usr/share/man \
--with-config-file-path=/etc \
--with-apxs2=/usr/bin/apxs \
--enable-force-cgi-redirect \
--disable-cgi \
--with-zlib \
--enable-bcmath \
--with-bz2 \
--enable-calendar \
--enable-dba=shared \
--with-gdbm \
--enable-ftp \
--with-gettext=/usr \
--enable-mbstring \
--with-readline \
--with-mysql-sock=/run/mysqld/mysqld.sock \
--with-curl \
--with-openssl \
--with-openssl-dir=/usr \
--with-mhash \
--with-mcrypt \
--with-libxml-dir=/usr \
--with-libdir=/lib64 \
--enable-sockets \
--enable-libxml \
--enable-soap \
--with-gd \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-zlib-dir=/usr \
--with-freetype-dir=/usr \
--enable-exif \
--with-xsl \
--with-xmlrpc \
--with-pgsql \
--with-pdo-mysql=/usr \
--with-pdo-pgsql \
--with-ldap \
--with-ldap-sasl

Once configured, we need to compile and install the package:

make && make install
libtool --finish /root/php-5.2.17/libs
cp -p .libs/libphp5.so /usr/lib/httpd/modules
install -v -m644 php.ini-recommended /etc/php.ini
sed -i 's@php/includes"@&\ninclude_path = ".:/usr/lib/php"@' /etc/php.ini

After installation is complete, you can make sure that PHP 5.2 is properly installed by entering the following command:

php -v

The PHP CLI should indicate that it is version 5.2.17.

From here, we must enable mod_php on Apache, and thus make PHP available to run the Web server’s scripts by modifying Apache’s httpd.conf file, as follows:

sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' /etc/httpd/httpd.conf
head -n -16 /etc/httpd/httpd.conf > temp1.txt
cat >> temp2.txt << 'EOF'

# Make sure there's only **1** line for each of these 2 directives:
# Use for PHP 4.x:
#LoadModule php4_module /usr/lib/httpd/modules/libphp4.so
#AddHandler php-script .php

# Use for PHP 5.x:
LoadModule php5_module /usr/lib/httpd/modules/libphp5.so
AddHandler php5-script .php 

AddType text/html .php

# PHP Syntax Coloring
# (optional but useful for reading PHP source for debugging):
AddType application/x-httpd-php-source phps
EOF
cat temp1.txt temp2.txt > /etc/httpd/httpd.conf
rm temp1.txt temp2.txt

Now, the only commands left to enter are the ones to create a ‘phpinfo()’ file and start the Apache Web server itself:

echo "<?php phpinfo();" > /srv/www/index.php
/etc/init.d/httpd start

If everything went well, you should now see the ‘phpinfo’ page by requesting the http://localhost:8181/index.php URL in your browser:

PHP 5.2 phpinfo page



Enjoy!


Loading Conversation