Setting up LAMP Server With an Older PHP Version

I recently got a project for a client who was using an AWS EC2 instance to host 3 different websites: 2 WordPress websites and a custom-made CodeIgniter-based system.

This EC2 used cPanel to admin the entire server, however, not only did my client want to reduce their costs but also they wanted to keep using the cloud and AWS as they already have all things located there (a bunch of domains acquired through Amazon's Registrar, DNS services and SES services).

So, I proposed they migrate all his websites to Lightsail instances, that way we can control the costs of each website: WordPress websites were one for production and one for development, so we only needed a cheap $3/month instance for the development one, and a $5/month for the production one.

The challenge: LAMP server but with PHP version 5.6 or lower

Then, I came up with the challenge of setting up the internal system LAMP server, that uses CodeIgniter version 3, and therefore it only supports PHP 5.6 or lower.

Currently, by the time this article is being written, PHP is on version 8, which means versions 5.X is no longer supported, and hence you can't even download them from the regular Debian or Ubuntu repositories (which are the distros the client prefers to use).

Also, installing them from the source, meant it would imply a lot more work and probably more issues, so I had to figure out a way to make it work as seamlessly as possible.

This is Linux; there is always a way

This couldn't be an uncommon problem, so I started digging in Google until I came across ondrej/php repository.

So this is a repository that allows you to manage PHP installation in your Debian-based system using a regular PPA. It also allows you to install several PHP versions on a single server if you need to do so. It contains all PHP versions from PHP 5.6 (exactly the one I needed) up to PHP 8.2 at the time of writing of this article.

So, this means you would be able to have a lot of old, unsupported versions available and also the latest ones.

This article assumes you already have Apache and MySQL/MariaDB installed, hence will not specify the LAMP server setup here.

I was using Ubuntu 22.04 distro in the instance, as this is the latest Ubuntu version that this ondrej/php package has a release file for, so I started by performing updates:

apt update && apt upgrade -y

Once the system was up-to-date, it was time to add the repository. To achieve this, you need to use the add-apt-repository command which does not come by default in Ubuntu servers, so you need to do this first:

apt install software-properties-common

Depending if you are using Debian or Ubuntu, you need to perform a step prior to adding the repository from ondrej/php. In Ubuntu, you can simply avoid the very next step and add it, however, in Debian, if you try to do so, you will get an error related to the language pack not being correct.

Only for Debian

To fix this, you need to do:

apt install -y language-pack-en-base

Once that's done, add the repository:

Only for Ubuntu

add-apt-repository ppa:ondrej/php

Now you can proceed to install PHP5.6 or 7.X or 8.X. If you don't specify any version, and just request to install PHP, it will take the latest 8.X package. To specify a specific version (or several at the same time), you have to specify it like this:

apt install php5.6

Once the PHP is installed, you will find its version folder version in: /etc/php/

What about PHP modules?

Later, if you want to install modules for this specific PHP version, you will use the same procedure with the different modules, by specifying the version:

apt install php5.6-bz2 php5.6-mbstring

Each module installed will perform only within the PHP version you installed it for and will be jailed in there. This is because, you could have several PHP versions running in the same server and different web applications, each of them running their own PHP version.

Bonus Material: Multiple PHP LAMP server

If you are looking to host several web applications but with different PHP versions or some of them with old unsupported versions, this is possible.

To do this, you need to install all PHP versions you require. Each of them and their modules will live within their own version folder in the path previously specified. Then, you will need to install php-fpm, which will allow you to specify each individual PHP version on each web application:

apt install libapache2-mod-fcgid5.6 libapache2-mod-php5.6 libapache2-mod-php7.4 php5.6-fpm php7.4-fpm

Then you need to activate each PHP version in the system:

systemctl start php5.6-fpm php7.4-fpm
a2enmod actions fcgid alias proxy_fcgi
systemctl restart apache2

Once you have all PHPs installed and running, you can simply edit each individual hosting entry in the Apache configuration file located within: /etc/apache2/sites-available. Edit each website .conf file by adding.

In thisexample, I have demo.conf and demo1.conf, which are 2 different web applications:

<VirtualHost *:80>

    ServerAdmin admin@demo.com
    ServerName  demo.com
    ServerAlias www.demo.com
    DocumentRoot /var/www/demo.com
    <Directory /var/www/demo.com/>
            Options FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            Require all granted
    </Directory>
        <FilesMatch \.php>
            SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
        </FilesMatch>

    ErrorLog ${APACHE_LOG_DIR}/err-demo.com.log
    CustomLog ${APACHE_LOG_DIR}/demo.com.log combined
</VirtualHost>

demo.conf

<VirtualHost *:80>

    ServerAdmin admin@demo1.com
    ServerName  demo1.com
    ServerAlias www.demo1.com
    DocumentRoot /var/www/demo1.com
    <Directory /var/www/demo1.com/>
            Options FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            Require all granted
    </Directory>
        <FilesMatch \.php>
            SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/"
        </FilesMatch>

    ErrorLog ${APACHE_LOG_DIR}/err-demo1.com.log
    CustomLog ${APACHE_LOG_DIR}/demo.com1.log combined
</VirtualHost>

demo1.conf

Conclusion

I hope this helps and makes it easier for you to be able to install an unsupported old PHP version on your server.

Of course, it is not something that is extremely recommended as support has been dropped and it might have security and stability issues. But if like in my case there is no option to use latest supported version, this is the way to resolve it in a very clean and easy to maintain method.