How to Install Multiple PHP Version with Apache on Debian 11/10

Channel: Linux
Abstract: 80> ServerName php72.example.com DocumentRoot /var/www/php72 Options -Indexes +FollowSymLinks +MultiViews AllowOverride All

Generally, the host manager uses the separate system for each PHP version application deployment. Which increases the hosting cost. Some of the host managers are using Docker to run multiple PHP version on the single server. Also, most of you are aware of the configuration. Might be some of the system administrators are not aware of this. This tutorial will help you to install multiple PHP version with Apache on Debian 11, Debian 10, and Debian 9 systems without switching PHP versions.

This tutorial describes the installation and configuration of two VirtualHost on Apache with separate PHP versions. First VirtualHost will work with PHP 5.6 and another VirtualHost will run with PHP 7.2. So just go through this tutorial. You can also use more than two PHP versions with Apache as required but this tutorial covers two only.

PHP Installation

For the installation of PHP versions, we use the PPA maintained here. Use the below couple of commands to add the PPA on your Debian system.

sudo apt install ca-certificates apt-transport-https 
wget -q https://packages.sury.org/php/apt.gpg -O- | sudo apt-key add -
echo "deb https://packages.sury.org/php/ stretch main" | sudo tee /etc/apt/sources.list.d/php.list

This tutorial uses PHP 5.6 and PHP 7.2 to configure with Apache web server. To use the multiple PHP versions, we will use PHP FPM and FastCGI. Run the following commands to install PHP and FPM on the Debian system.

apt update
sudo apt install php5.6 php5.6-fpm
sudo apt install php7.2 php7.2-fpm

After installation, php-fpm services will be started automatically. Use the following commands to make sure both services are running.

sudo systemctl status php5.6-fpm
sudo systemctl status php7.2-fpm
Apache Installation

Install Apache web server from the official repository. Launch terminal on your system or login with ssh for remote systems. Execute the following commands to install the latest available version of Apache web server.

sudo apt update 
sudo apt install apache2 libapache2-mod-fcgid
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.

sudo a2enmod actions fcgid alias proxy_fcgi

Now create two seprate directories on your system for both sites configuring on your system.

sudo mkdir /var/www/php56
sudo mkdir /var/www/php72

Now, create and index.php containing the phpinfo() function. This will help users to view and confirm, what PHP version is used by the VirtualHost.

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

Let’s start the creation of VirtualHost. Apache keeps all the VirtualHost configuration files under /etc/apache2/sites-available with the extension .conf. Create a file for the first virtual host and edit in your favorite text editor.

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

Add the following content. Make sure to use correct ServerName and directory path according to your setup. This website is configured to work with PHP 5.6.

<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$> # Apache 2.4.10+ can proxy to unix socket SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/" </FilesMatch> </VirtualHost>12345678910111213<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$>        # 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 a second VirtualHost configuration file to work with PHP 7.2. Edit configuration file in text editor:

sudo vim /etc/apache2/sites-available/php72.example.com.conf

Add the following content to file with proper ServerName and DocumentRoot.

<VirtualHost *:80> ServerName php72.example.com 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>123456789101112<VirtualHost *:80>    ServerName php72.example.com    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>

You both of the websites are configured now. But they are still not active. Apache keeps active sites under /etc/apache2/sites-enabled directory. You can simply create a symbolic link of config files to this directory or use below command to do the same.

sudo a2ensite php56.example.com
sudo a2ensite php72.example.com

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

sudo systemctl restart apache2

Your setup has been completed now. Go to the next step to test your setup.

Test Setup

Open a web browser and visit both of the sites. You will see that php56.example.com shows the version PHP 5.6 and php72.example.com is showing the PHP 7.2 as the configuration.

Ref From: tecadmin

Related articles