How to Configure Multiple PHP Versions with Apache on Ubuntu

This tutorial will help you to install multiple PHP versions on Ubuntu 20.04, 22.04 without switching PHP versions. It describes the installation and configuration of two VirtualHost on Apache with separate PHP versions.

Installing Apache2

Execute the following commands to install the latest available version of the Apache web server.

sudo apt update
sudo apt install apache2 libapache2-mod-fcgid
sudo a2enmod actions fcgid alias proxy_fcgi

Installing PHP Versions

Execute the following commands to install php7.4 and php8.1

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update sudo apt install php7.4 php7.4-fpm
sudo apt install php8.1 php8.1-fpm

Configuring Apache Virtual Hosts

First, create two directories on your server.

sudo mkdir /var/www/php74
sudo mkdir /var/www/php81

Then, Create an index.php file containing the phpinfo() function

sudo echo "<?php phpinfo(); ?>" > /var/www/php74/index.php
sudo echo "<?php phpinfo(); ?>" > /var/www/php81/index.php

Next, create two virtual host configuration files:

The first virtual host configuration file is for the website php74.example.com.conf. Here you will direct Apache to render content using php7.4 version

<VirtualHost *:80>

ServerName php74.example.com

DocumentRoot /var/www/php74

<Directory /var/www/php74>

Options -Indexes +FollowSymLinks +MultiViews

AllowOverride All

Require all granted

</Directory>

<FilesMatch \.php$>

SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost"

</FilesMatch>

</VirtualHost>

The other virtual host configuration file is for the website php8.1.example.com.conf using php8.1 version

<VirtualHost *:80>

ServerName php81.example.com

DocumentRoot /var/www/php81

<Directory /var/www/php81>

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/php8.1-fpm.sock|fcgi://localhost"

</FilesMatch>

</VirtualHost>

Then, check the Apache configuration file for any syntax errors:

sudo apachectl configtest

Next, enable both virtual host configuration files with the following commands:

sudo a2ensite php74.example.com.conf
sudo a2ensite php81.example.com.conf

Now disable the default site, since you won’t need it.:

sudo a2dissite 000-default.conf

Finally, restart the Apache service to implement your changes:

sudo systemctl restart apache2

Now that you have configured Apache to serve each site, you will test them to make sure the proper PHP versions are running.

Testing both websites

Update the /etc/hosts file by following the lines

127.0.0.1 localhost php74.example.com php81.example.com

Open a web browser and visit both of the sites. You will see that php81.example.com shows the version PHP 8.1 and php74.example.com is showing the PHP 7.4 as the configuration

Conclusion

You have now combined virtual hosts and PHP-FPM to serve multiple websites and multiple versions of PHP on a single server. The only practical limit on the number of PHP sites and PHP versions that your Apache service can handle is the processing power of your instance.

you could now secure your websites. To accomplish this, you can follow our tutorial on Hands On: How To Secure Apache with Let's Encrypt on Ubuntu 20.04

Duoc Le, DevOps @ UnDigital®

Full Article

Featured projects.

OZ Design Furniture

We executed a three-phased omnichannel headless ecommerce store, PIM and data feeds integration.

Sekisui House

We re-platformed 7 websites into a shared ecosystem and saved the business $200k in licensing and IP fees as well as thousands of dollars per month on maintenance fees.