Home Linux How to Install Multiple PHP Versions (5.6, 7.1 or 7.2) | PHP-FPM...

How to Install Multiple PHP Versions (5.6, 7.1 or 7.2) | PHP-FPM | Apache2 | Ubuntu Server

Key Points:
1. In some scenario, we need to install multiple PHP Versions (5.6, 7.1 or 7.2) with PHP-FPM. How??
2. Configure different PHP versions with multiple websites in Apache. How??

Its very often as a system administrator when you will get the task to configure one server with different PHP versions with two different websites. For this, we are using here Ubuntu 18.04, Apache web server along with PHP-FPM modules.

Recommended: Configure different PHP Versions+Nginx+Ubuntu Server.

Multiple PHP Versions on Ubuntu ServerMultiple PHP Versions on Ubuntu Server

Required

Ubuntu 18.04
Apache web server
PHP Multiple Versions.

Install Apache2 on Ubuntu 18.04

Run the following command to install the apache2 with libapache2-mod-fcgid module.

arun@arun-virtual-machine:~$sudo apt-get install apache2   libapache2-mod-fcgid 

For the earlier Ubuntu versions like 16.04, the package may be found with the name libapache2-mod-fastcgi .

start the apache2 service:

 $ sudo systemctl   start  apache2

Install PHP 5.6 Version

Add the repository ppa:ondrej/php to install the PHP versions on Ubuntu server.

 $ sudo  add-apt-repository ppa:ondrej/php 

Now, run the command to install PHP 5.6 version:

 $ sudo apt-get install  php5.6  php5.6-fpm 

Start the PHP5.6-FPM service:

 $  systemctl start php5.6-fpm    

To check the status of the service, run this command:

 $ sudo  systemctl status php5.6-fpm   

We done this. next step is to install another version of PHP.

Install PHP 7.1 Version

To install PHP 7.1 version run the command:

$ sudo apt-get install php7.1 php7.1-fpm

When PHP-FPM service installed, it automatically started. If not, then use this command:

 $ sudo  systemctl start php7.1-fpm  

Check the service status:

 $ sudo  systemctl status php7.1-fpm 

Apache2 Web Server Configuration

We need to enable few modules for the apache2 to work with multiple PHP versions.

$  sudo  a2enmod  actions  fcgid alias proxy_fcgi 

To apply changes need to restart the apache2 service.

$ sudo systemctl  restart  apache2

Test setup: One website running on PHP 5.6 & Another running on PHP 7.1 version

First, we need to create the setup for multiple websites. For this, creating root directory for both the websites.

$ sudo mkdir -p /var/www/html/site1.com
$ sudo mkdir -p /var/www/html/site2.com

Create the index.php file, to display the PHP version on homepage.

$ echo "<?php phpinfo(); ?>" > /var/www/html/site1.com/index.php
$ echo "<?php phpinfo(); ?>" > /var/www/html/site2.com/index.php

Next step to create the virtual host for the two websites under the /etc/apache2/sites-available with the .conf extension and paste the following code in the file.

$ sudo vi   /etc/apache2/sites-available/site1.com.conf   
 <VirtualHost *:80>                            
  ServerName site1.com  
DocumentRoot /var/www/html/site1.com  
<Directory /var/www/html/site1.com>       
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>

In similar way, create 2 virtual hosts. For website site2.com we will use PHP7.1-FPM.

 $ sudo vi   /etc/apache2/sites-available/site2.com.conf    
 
<VirtualHost *:80>   
  ServerName site2.com  
DocumentRoot /var/www/html/site2.com  
<Directory /var/www/html/site2.com>       
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/php7.1-fpm.sock|fcgi://localhost/"
    </FilesMatch>
</VirtualHost>

Now create the symbolic link of both the config files with the sites-enabled directory.

$ sudo ln -s /etc/apache2/sites-available/site1.com.conf   /etc/apache2/sites-enabled
$ sudo ln -s /etc/apache2/sites-available/site2.com.conf   /etc/apache2/sites-enabled

or you can simply run the following command to do the same thing.

$ sudo a2ensite site1.com
$ sudo a2ensite site2.com

Now, restart the apache2 service again to apply the changes done.

$ sudo systemctl  restart  apache2

If you are using local environment, then read this step otherwise just skip to next step. For local setup, make entry in etc/hosts file for DNS.

$ sudo vi /etc/hosts

192.168.150.130 site1.com
192.168.150.130 site2.com

Now, everything is done.Just browse the websites site1.com and site2.com.

website testing with specific PHP Version 5.6website testing with specific PHP Version 5.6
website testing with specific PHP Version 7.1website testing with specific PHP Version 7.1


Congratulations !!! you have configured Multiple PHP Versions (5.6, 7.1 or 7.2) with PHP-FPM with Apache2 on Ubuntu Server with the specific website.


LEAVE A REPLY

Please enter your comment!
Please enter your name here