Install and Configure Multiple PHP Version with Apache on Ubuntu 20.04

Hi, Friends welcome to TechNHIT. In this article you will learn how to install and Configure Multiple PHP Version with Apache on Ubuntu 20.04

It’s very common when you working with Servers for Hosting web application, you needed multiple PHP version for the deferent web application as per their requirements and you have to manage these multiple requirements in a single server in a cost-effective way. Configuring multiple PHP in a single server makes you happy in this sense.

So, in order to do this entire job perquisite, and environment I used.

  • One Linux Box / VM / Container having Ubuntu 20.04LTS
  • IP 10.0.50.51 (Local IP), I am doing this test in a local environment. You can replace it with Public IP for the production server.

Let’s go and logon to the system, and follow the steps. 

Step 1) installing the Apache Server.

After updating the package list do install apache and modfcgid by typing the following command.

sudo apt update
sudo apt install apache2 libapache2-mod-fcgid

Step 2) installing PHP and PHP FPM

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update 
sudo apt install php5.6 php5.6-fpm
sudo apt install php7.4 php7.4-fpm

we are using PHP 5.6 and PHP 7.4 to configure with the Apache webserver. And we will use PHP FPM and FastCGI as an API

Step 3) Configuring PHP with apache.

Now enable few modules that 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 fcgid alias proxy_fcgi

Now configure websites on your Apache server, for testing purpose we are going to create two application directories for both PHP version to test the configuration.

sudo mkdir /var/www/php56-app
sudo mkdir /var/www/php74-app

Now create a file named index.php in each directory and push this phpinfo() HTML tag.

echo "<?php phpinfo(); ?>" > /var/www/php56-app/index.php
echo "<?php phpinfo(); ?>" > /var/www/php74-app/index.php

phpinfo() basically tales you about a brief of PHP information like version, module, server API, etc.

Now create an apache virtual host file and configure the required PHP version inside it. Go ahead and create the following host file under site available directory. Put the following configuration and tell the set handler to use your desire PHP fpm sock file.

sudo nano /etc/apache2/sites-available/php56.example.com.conf

<VirtualHost *:80>
    ServerName php56.example.com
    DocumentRoot /var/www/php56-app
    <Directory /var/www/php56-app>
        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>

Similarly, create vhost for PHP 7.4 and configure the set handler

sudo nano /etc/apache2/sites-available/php74.example.com.conf
<VirtualHost *:80>
    ServerName php74.example.com
    DocumentRoot /var/www/php74-app
    <Directory /var/www/php74-app>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost"
    </FilesMatch>
</VirtualHost>

Now enable both virtual hosts using following command a2ensite

sudo a2ensite php56.example.com
sudo a2ensite php74.example.com

After making all the changes restart Apache to reload new settings changes

sudo systemctl reload apache2
sudo systemctl restart apache2

Step 4) Finally, Test Your Setup.

Now as we are in a local environment your system needs a local resolver to resolve the host. In order to do that in Linux system edit /etc/hosts file and put the following DNS resolver.

sudo nano /etc/hosts

Add following entry to the end of file

10.0.50.51/<your IP>/localhost   php74.example.com
10.0.50.51/<your IP>/localhost   php56.example.com

For windows system edit host file in following path “C:\Windows\System32\drivers\etc”

Now go ahead and open a web browser and test both URL.

http://php74.example.com

screen1

http://php56.example.com

screen2

Now your server able to handle multiple PHP versions as per your application requirements.

Hope this article helpful to you. Please like and share this. Leave a comment if you have any questions.  Thank you.

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

One Comment

  • Thank you very much. It work well with any errors.

One Pingback

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.