Has it ever occurred to you that you want to add a new feature to an application you built years ago and the programming language’s latest version has changed a lot since you built the application?
It happens a lot. And whenever it does, it’s a dilemma for the developers. Should they code in the old version which is not as good as the latest version in terms of performance? Or should they update the old code to the latest version of the language?
In this article, I’d like to share such an experience I faced, and a solution for it.
The challenge I faced was to add a new set of features to a project I developed a few years back. Back then, I coded the application using PHP 5.4 which was the latest version at that time. But right now, the latest PHP version is in 7.4.3. The latest version offers far better performance and security compared to 5.6. So, I had three options to pursue.
1. Code the new features in the 5.6 version
2. Upgrade the old code to 7.4.3
3. Code the new features in 7.4.3 and host them on a separate server
Obviously, option 1 is not a good choice since I’d have to compromise on the performance of the application I’m building. So, it’s easy enough to discard this option. Option 2 has its benefits, but, rewriting the old code base is not economically feasible for my client. Also, it takes a lot of time to re-code everything.
Now we’re left with option three. Let’s take a deeper look at it. It is possible to code the new features of this application in the latest version of PHP and host it on a separate server. I’ll have to manage the API calls and route them to the correct server at the application layer. However, this would make the API calls slower, and eventually, the application would get slower. Also, the infrastructure costs for hosting the application would increase. So, the straightforward solution to solve this problem is to install multiple versions of PHP with Apache on the same server. Is it possible? Yes, it is!
Let’s see how to install multiple PHP versions with Apache without switching PHP versions. Here, we’ll install and configure two VirtualHost on Apache with separate PHP versions. First VirtualHost will work with PHP 5.6 and the second VirtualHost will run with PHP 7.2.
Execute the following commands from your terminal to install the latest available version of the Apache webserver.
sudo apt update sudo apt install apache2 libapache2-mod-fastcgi Ubuntu 18.04 Users: sudo apt install apache2 libapache2-mod-fcgid
For installing PHP versions, use the PPA maintained here. Execute the following commands to add the PPA to your system.
sudo apt install python-software-properties sudo add-apt-repository ppa:ondrej/php
To install the multiple PHP versions, we will use PHP FPM and FastCGI. Execute the following commands to install the following packages on your system.
apt update sudo apt install php5.6 php5.6-fpm sudo apt install php7.2 php7.2-fpm
Execute the following commands to make sure both services are running.
sudo systemctl status php5.6-fpm sudo systemctl status php7.2-fpm
We need to enable a few modules required for the configuration of multiple PHP versions with Apache. These modules are necessary to integrate PHP FPM and FastCGI with the Apache server.
sudo a2enmod actions fastcgi alias proxy_fcgi Ubuntu 18.04 Users: sudo a2enmod actions fcgid alias proxy_fcgi
Now, let’s configure websites on your Apache server. In this tutorial, I am configuring two websites to work with two different PHP versions. First, create two directories on your server.
sudo mkdir /var/www/php56 sudo mkdir /var/www/php72
Create an index.php containing the phpinfo() function.
echo "<?php phpinfo(); ?>" > /var/www/php56/index.php echo "<?php phpinfo(); ?>" > /var/www/php72/index.php
Now we can create VirtualHost. Apache keeps all the VirtualHost configuration files under /etc/apache2/sites-available with the extension .conf. Create a file for the first virtual host and edit it.
sudo vim /etc/apache2/sites-available/php56.test.com.conf
Add the following code and replace the ServerName and directory path according to your setup. This website is configured to work with PHP 5.6.
Similarly, create a second VirtualHost configuration file to work with PHP 7.2.
<VirtualHost *:80> ServerName php56.test.com DocumentRoot /var/www/php56 <Directory /var/www/php56> Options -Indexes +FollowSymLinks +MultiViews AllowOverride All Require all granted </Directory> <FilesMatch \.php$> # Apache 2.4.10+ can proxy to unix socket SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost" </FilesMatch> </VirtualHost>
sudo vim /etc/apache2/sites-available/php72.test.com.conf
<VirtualHost *:80> ServerName php72.test.com DocumentRoot /var/www/php72 <Directory /var/www/php72> Options -Indexes +FollowSymLinks +MultiViews AllowOverride All Require all granted </Directory> <FilesMatch \.php$> SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost" </FilesMatch> </VirtualHost>
Both PHP versions are now configured. To make them active, you have to create a symbolic link of config files to this directory. Execute the below command to do the same.
sudo a2ensite php56.test.com sudo a2ensite php72.test.com
After making all the changes restart Apache to reload new settings changes.
sudo systemctl restart apache2
That’s it. Your setup is completed. Go on and test it out.