Use multiple PHP versions at the same time in Apache

Posted on 12 February, 2019

At the time of working with frameworks like Magento, we have faced the issue that different version of the framework is compatible with different PHP versions. It often happens that specific version of Magento 2 supports only particular PHP version. So it is a very troublesome process to keep swapping the PHP version depending on the Magento(or any framework) version you need to use.
With this solution, we can install multiple PHP version in the system and make them work simultaneously.

There are several ways to achieve the same goal and run multiple websites on different PHP versions simultaneously. For running multiple PHP versions on one server, we will be using Apache with PHP-FPM.

PHP-FPM (FastCGI Process Manager) is an alternative implementation of PHP FastCGI. It provides some additional features like Adaptive process spawning which is useful for sites. In this tutorial, we are using PHP 7.0 and configuring with Apache using PHP-FPM and FastCGI on Ubuntu 16.04 system.

1) Apache2 Installation:

Install Apache2 web server from the official repository. Launch terminal on your system or login with SSH to remote systems. Execute the following commands to install the latest version of Apache2 web server and FastCGI module:

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

2) PHP Installation:

Install PHP we will use PPA Repository. Execute the following commands to install the php

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

After installing the above packages, php7.0-fpm service will get started automatically. We can verify it typing below command on terminal:

sudo systemctl status php7.0-fpm

3) Apache Configuration:

Now we are going to configure multiple PHP versions with apache. For that, you need to configure PHP-FPM with apache server.

a2enmod actions fastcgi alias proxy_fcgi

Now we are going to configure the Apache VirtualHost to run with FPM/FastCGI. (For the tutorial, I am taking test virtual host and make a site in /etc/apache2/sites-available directory. Copy the virtual host file 000-default.conf from the above location as test123.conf and then open that file in text editor.)

Cd /etc/apache2/sites-available/
sudo cp 000-default.conf test123.conf
sudo nano test123.conf
Add Below information in test123.conf file:
	- ServerName
	- ServerAlias
	- DocumentRoot  = /var/www/html/
	<FilesMatch \.php$>
	     SetHandler "proxy:unix:/var/run/php/php7.0(version)-fpm.sock|fcgi://localhost/"

4) PHP-FPM Configuration:

You have to set the user group permission at PHP-FPM

sudo nano /etc/php/7.0/fpm/pool.d/www.conf
	- Unix user/group of processes
	- user = emipro
	- group = emipro
-Set permissions for unix socket
	- listen.owner = emipro
	- = emipro

Now we need to restart apache and PHP-FPM Services and enable some modules.

sudo a2enmod rewrite
a2dismod mpm_prefork
sudo a2ensite
sudo service apache2 restart
sudo service php7.0-fpm restart

We hope that our solution has resolved your issue(s) related to multi PHP. If you have any questions or comments, feel free to post in the comment section.

Deep Popat , Software Tester

Magento Technical Notes

About Emipro

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. Our customers are companies of all sizes ranging from startups to large enterprises who realize that they need a professional internet solution to generate revenue streams, establish proper communication channels, to achieve desired goals and streamline business operations. [....] Read More

Our writings seems informative ?

Subscribe for our Magento Technical Notes and get more amazing stuff directly to your inbox!

Post Your Review


Your Review has been posted

0 Comment(s)