We might run it to situation where we need to upgrade our environment to newer version of PHP to test our web application or we might work on different version for different application so this article will guide to have multiple web apps with different version of php running at same time without switching out different version.

We shall proceed with installing Apace web server as our server.

For Ubuntu 14.04 LTS

sudo apt-add-repository ppa:ondrej/apache2
sudo apt-get update
sudo apt install apache2 libapache2-mod-fastcgi
sudo apt-get dist-upgrade

For Ubuntu 16.04 LTS

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

For Ubuntu 18.04 LTS

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

After installing Apache we can proceed installing PHP.

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

So to run multiple version of PHP we make use of PHP-FPM mod and Fast-cgi like how ngnix serve php websites.so the next step is to configure apace to make use of FPM mod to serve php files.

For Ubuntu 14.04 and 16.04 LTS

sudo a2enmod actions fastcgi alias proxy_fcgi

For Ubuntu 18.04 LTS

sudo a2enmod actions fcgid alias proxy_fcgi

And Apache configurations for sites to serve different version of php as follows.
Create config file for PHP 5.6 with below command

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

And paste the below configuration in the file and save.

<VirtualHost *:80>
    ServerName {your domain}
    DocumentRoot /var/www/php56
    <Directory /var/www/php56>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
    </FilesMatch>
</VirtualHost>

And for PHP 7.2

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

And content of the configuration file

<VirtualHost *:80>
    ServerName {your domain}
    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>

And we shall proceed creating the test PHP file to check and verify the configuration and enable the site created for configuration.

mkdir /var/www/php56
mkdir /var/www/php72
echo "<?php phpinfo(); ?>" > /var/www/php56/index.php
echo "<?php phpinfo(); ?>" > /var/www/php72/index.php
sudo a2ensite php56.example.com
sudo a2ensite php72.example.com
sudo systemctl restart apache2

And finally make the entry of the server name in /etc/hosts and open a browser and naviage to domain to see the php info displayed.