run multiple php versions in apache

How to Run Multiple PHP Versions in Apache

Apache is a popular web server used by millions of websites and organizations. It allows you to build highly scalable websites. Sometimes system administrators need to run multiple PHP versions in Apache in order to support multiple websites on a single server. In this article, we will learn how to run multiple PHP versions in Apache.


How to Run Multiple PHP Versions in Apache

Here are the steps to run multiple PHP versions in Apache.


1. Install PHP 7.2 and 7.3 with PHP-FPM

First we will install PHP versions 7.2 and 7.3 as well as PHP-FPM extensions. For this purpose, we will first add Ondrej repository. Run the following command to install software-properties-common.

$ sudo apt-get install software-properties-common -y

The above command provides the add-apt-repository command. Run the ondrej/php repository.

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

Run the following command to update the repository.

$ sudo apt-get update -y

Install php7.2, php7.2-fpm, php7.2-mysql, libapache2-mod-php7.2, and libapache2-mod-fcgid.

$ sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 libapache2-mod-fcgid -y

Now repeat the same for PHP 7.3.

$ sudo apt-get install php7.3 php7.3-fpm php7.3-mysql libapache2-mod-php7.3 -y

Next, start PHP service.

$ sudo systemctl start php7.2-fpm
$ sudo systemctl start php7.3-fpm

Enable several modules that help Apache to run multiple websites.

$ sudo a2enmod actions fcgid alias proxy_fcgi

Restart Apache web server.

$ sudo systemctl restart apache2


2. Create Directory Structure for Websites

Next, we will create directory structure for both websites site1.your_domain and site2.your_domain.

$ sudo mkdir /var/www/site1.your_domain
$ sudo mkdir /var/www/site2.your_domain

By default, Apache runs websites using www-data user and group. So we will change ownership and permissions of above created folders so that Apache process can easily access them.

$ sudo chown -R www-data:www-data /var/www/site1.your_domain
$ sudo chown -R www-data:www-data /var/www/site2.your_domain
$ sudo chmod -R 755 /var/www/site1.your_domain
$ sudo chmod -R 755 /var/www/site2.your_domain

Next, we will create info.php file for each website, just to view configuration settings. Use text editor to create info.php file.

$ echo "<?php phpinfo(); ?>" > /var/www/site1.your_domain/info.php
$ echo "<?php phpinfo(); ?>" > /var/www/site2.your_domain/info.php

phpinfo() function is a builtin function that automatically fetches and displays all PHP & Apache configuration related to a website.


3. Configure Both Websites

Next, we will create 2 virtual host configuration files – one for each website. We will create new files at http://etc/apache2/sites-available/.

Create new virtual host configuration file for site1 with the following command.

$ sudo vi /etc/apache2/sites-available/site1.your_domain.conf

Add the following lines to it.

<VirtualHost *:80>
ServerAdmin admin@site1.your_domain
ServerName site1.your_domain
DocumentRoot /var/www/site1.your_domain
DirectoryIndex info.php

<Directory /var/www/site1.your_domain>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

<FilesMatch \.php$>
# From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>

ErrorLog ${APACHE_LOG_DIR}/site1.your_domain_error.log
CustomLog ${APACHE_LOG_DIR}/site1.your_domain_access.log combined
</VirtualHost>

Create new virtual host configuration file for site2.

$ sudo vi /etc/apache2/sites-available/site2.your_domain.conf

Add the following lines to it.

<VirtualHost *:80>
ServerAdmin admin@site2.your_domain
ServerName site2.your_domain
DocumentRoot /var/www/site2.your_domain
DirectoryIndex info.php

<Directory /var/www/site2.your_domain>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

<FilesMatch \.php$>
# 2.4.10+ can proxy to unix socket
SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
</FilesMatch>

ErrorLog ${APACHE_LOG_DIR}/site2.your_domain_error.log
CustomLog ${APACHE_LOG_DIR}/site2.your_domain_access.log combined
</VirtualHost>

The 2 virtual host configuration files use different domain names, as well as PHP versions. They also refer to different website folders to serve files.

Enable the two virtual hosts with the following commands.

$ sudo a2ensite site1.your_domain
$ sudo a2ensite site2.your_domain

Next, we disable the two sites with the following command.

$ sudo a2dissite 000-default.conf

Restart Apache service with the following command.

$ sudo systemctl restart apache2

When you create virtual host configuration files, Apache will determine the appropriate virtual host to serve files, depending on the domain mentioned in requested URL.


4. Test Both Websites

Open browser and go to the following website – http://site1.your_domain and http://site2.your_domain. You should be able to see the PHP configuration of each website when you enter its URL.

Once you have tested the websites, you can run the following command to remove the info.php pages so that unauthorized users do not get to access this information.

$ sudo rm -rf /var/www/site1.your_domain/info.php
$ sudo rm -rf /var/www/site2.your_domain/info.php

In this article, we have learnt how to run multiple PHP versions in Apache web server. Sometimes you may need to restart Apache server to apply changes, in case it is not working. We have used the same approach as hosting multiple websites from a single server in Apache, even when you use same PHP installation. The main difference in this case is that we have specified different PHP versions in SetHandler directive.

Also read:

How to Run Multiple PHP Versions in NGINX
How to Create & Execute JAR File
How to Fix Plain HTTP Request Was Sent to HTTPS
How to Block USB Storage Devices in Linux
How to Disconnect Inactive or Idle SSH Sessions

Leave a Reply

Your email address will not be published. Required fields are marked *