Apache is a free and open-source web server software, known for its stability. This guide will show you how to configure Apache in order to have the ability to run multiple versions of PHP at the same time while still being able to switch between them with ease.

To run multiple versions of PHP simultaneously on Apache, you will need to use the Apache module fcgid. This module allows you to use FastCGI to execute scripts, including PHP scripts, and can be used to specify the version of PHP that should be used for a particular virtual host.

Here is an example of how to configure Apache to use PHP 8.x, PHP 7.x, and PHP 5.6 simultaneously:

Overview:

  1. Install Apache2
  2. Install PHP 8.x, PHP 7.x, and PHP 5.6
  3. Start PHP-FPM Services
  4. Configure Apache2
  5. Virtual Hosts Method
  6. .htaccess Method

Steps:

1. Install Apache2

we’ll start by updating the system. To update run below commands.

sudo apt update

To install Apache2 run below command

sudo apt install apache2 -y

To check Apache status

sudo systemctl status apache2

if it’s not active start Apache by running the below command.

sudo systemctl start apache2

Now, open your browser and enter the IP to access the default web page http://<your_ip>. For localhost, simply type localhost in browser. http://localhost

Sample output:

Apache2 default page

2. Install PHP 8.x, PHP 7.x, and PHP 5.6

First, we need to add PHP repository. which will allow us to download and install PHP versions.

Run the below commands to add and install PHP versions.

sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update -y
sudo apt install libapache2-mod-fcgid

To Install PHP 5.6

sudo apt install php5.6 php5.6-fpm php5.6-mysql libapache2-mod-php5.6 -y

To Install PHP 7.4

sudo apt install php7.4 php7.4-fpm php7.4-mysql libapache2-mod-php7.4 -y

To Install PHP 8.1

sudo apt install php8.1 php8.1-fpm php8.1-mysql libapache2-mod-php8.1 -y

You can verify the installation by checking sockets in  /var/run/php/

php --version

Sample Output:

PHP sockets

Some common extensions used by PHP applications.(Optional)

Make sure to replace 8.1 with the version you require.

sudo apt install php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-intl -y

3. Start PHP-FPM Services

PHP-FPM (FastCGI Process Manager) is an implementation of FastCGI, a protocol for interacting with a web server, which is designed to handle high loads and reduce the overhead of executing PHP scripts. It is typically used to improve the performance of PHP applications by allowing them to run as a separate process, rather than being run within the web server process itself.

Make sure php-fpm service is running.

Start php-fpm service (Make sure to replace 8.1 with the version you require. )

sudo systemctl start php8.1-fpm
sudo systemctl status php8.1-fpm

Sample Output:

sudo systemctl status php8.1-fpm

4. Configure Apache2

We’ll enable some Apache modules using a2enmod

sudo a2enmod actions fcgid alias proxy_fcgi

Restart Apache.

sudo systemctl restart apache2

Now we’re ready to use different verions of PHP using Virtual Hosts or .htaccess

5. Virtual Hosts Method

Now, let’s create a virtual host for demo.com, and test with php5.6, php7.4, and php8.1 respectively.

cd /etc/apache2/sites-available
sudo vi demo.com.conf

Add the below config and save the file.

<VirtualHost *:80>
	ServerAdmin admin@demo.com
	ServerName  demo.com
	ServerAlias www.demo.com
	DocumentRoot /var/www/demo.com
	<Directory /var/www/demo.com/>
            Options FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            Require all granted
	</Directory>
        <FilesMatch \.php>
            SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"
        </FilesMatch>
	ErrorLog ${APACHE_LOG_DIR}/err-demo.com.log
	CustomLog ${APACHE_LOG_DIR}/demo.com.log combined
</VirtualHost>

you check the syntax using apachectl

sudo apachectl -t

Enable site using below command

sudo a2ensite demo.com.conf

Create DocumentRoot directory

sudo mkdir /var/www/demo.com

Create php info page.

cd /var/www/demo.com
sudo vi info.php

Add the below config and save the file.

<?php
phpinfo(); 
?>

Restart Apache2

sudo systemctl restart apache2

Now open your browser to verify PHP version. use the below URL format.

http://<your_ip>/info.php

Sample Page:

PHP 8.1 info Page

To Test php5.6 and php7.2 simply update the <FilesMatch> directive with PHP version and restart apache2.

PHP 5.6 info page
PHP 7.4 info page

6. .htaccess Method

To use .htaccess we need to set AllowOverride All

Sample config

<VirtualHost *:80>
        ServerAdmin admin@demo.com
        ServerName  demo.com
        ServerAlias www.demo.com
        DocumentRoot /var/www/demo.com
        <Directory /var/www/demo.com/>
            Options FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            Require all granted
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/err-demo.com.log
        CustomLog ${APACHE_LOG_DIR}/demo.com.log combined
</VirtualHost>

Search for AllowOverride and replace None with All for

Restart Apache2.

sudo systemctl restart apache2

Now go to DocumentRoot and create file .htaccess with the below config.

<FilesMatch \.php>
    SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"
</FilesMatch>

To Test php5.6 and php7.2 simply update the <FilesMatch> directive with PHP version and restart apache2.

That’s all folks for now.

I hope this helps! Let me know if you have any questions.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments