In this tutorial we will configure Apache to run PHP 7.2 and PHP 5.6 simultaneously, and choose between them using Virtual Hosts or .htaccess.
There are several ways to achieve the same goal and run multiple websites on different PHP versions simultaneously. As for running multiple PHP versions on one server, my choice is Apache with FastCGI.
Prerequisites
1. You should already have Apache installed and serving web pages before following this guide.
If you need to install Apache/LAMP Server on Ubuntu, please check our post.
2. To install Multiple php version check our post.
Note: Now we are assuming you have Apache and multiple PHP installed, if not please check our above reference URL.
Step 1: Let’s begin by updating the package lists.
sudo apt update
Step 2: If fpm module is not installed use below command.
sudo apt install php7.2-fpm php5.6-fpm
Once installed, you should have two new sockets in /var/run/php/.
sudo ls -al /var/run/php/
To Verify fpm serivce:
sudo systemctl status php7.2-fpm && sudo systemctl status php5.6-fpm
Step 3: Apache Configuration
Now enable few modules required for the configuration of multiple PHP versions with Apache.These modules are necessary to integrate PHP FPM and FastCGI with Apache server.
Ubuntu 20.04 / 18.04 users.
sudo a2enmod actions alias proxy_fcgi fcgid
Ubuntu 16.04 users.
sudo a2enmod actions alias proxy_fcgi fastcgi
sudo systemctl restart apache2
Step 4 : Create two Virtual Host for php 5.6 and php7.2 (Change the domain name)
Create webroot directory for your both website.
cd /var/www/ && mkdir php56 php72
Create VirtualHost for 1st website.
sudo vim /etc/apache2/sites-available/php56_example.conf
<VirtualHost *:80>
ServerName php56.example.com
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>
Create VirtualHost for 2nd website.
sudo vim /etc/apache2/sites-available/php72_example.conf
<VirtualHost *:80>
ServerName php72.example.com
DocumentRoot /var/www/php72
<Directory /var/www/php56>
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> Now we need to restart apache and PHP-FPM Services and enable modules/Website Configuration file.
Enable modules/Website Configuration file
sudo a2enmod rewrite && sudo a2ensite php72_example.conf && sudo a2ensite php56_example.conf
Restart Services
sudo service apache2 restart && sudo service php7.2-fpm restart && sudo service php5.6-fpm restart
Step 5: Test PHP
Add info.php in both the website directory and paste the below code.
<?php
phpinfo();
?> We can now load this file in the browser by going to http://php56.example.com/info.php or http://php72.example.com/info.php
Optional Step 6 : Htaccess Method
1. Need to enable AllowOverride ALL in virtual host.
2. Enable rewrite module.
*We have already enabled both steps above*
Then add the below code in .htaccess file according to php version.
<FilesMatch \.php>
SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
</FilesMatch>
turkce
Right away I am going away to do my breakfast, after having my breakfast coming yet again to read other news. Suzanne Talbot Valeda
yabanci
This is one awesome blog post. Really looking forward to read more. Great. Carlina Alasdair Keung
turkce
I really like your writing style, excellent information, thanks for putting up : D. Elfrida Waite Carolus
720p
Great post! We will be linking to this great post on our website. Keep up the great writing. Mary Ty Eliot
sikis izle
Appreciating the dedication you put into your website and in depth information you present. Mommy Marlin Ho
turkce
Greate post. Keep posting such kind of info on your page. Tybi Moritz Magavern
web-dl
Excellent article. I will be dealing with many of these issues as well.. Doti Prentice Kilk
Rajat Malik
Thank You, I hope your issue is resolved by Doc.
amateur
Great delivery. Great arguments. Keep up the great spirit. Kendra Jethro Ned
torrent
The unbelievable benefit of no banks excited about corporations with useful belongings have an interest. Janie Felicio Hendrika