In this guide we will configure Apache to run Multiple PHP Versions PHP 7.0/7.1/7.2/7.3 and PHP 5.6 simultaneously, and choose between them using .htaccess.

1. Add Repository

Let’s begin by updating the package lists.

sudo apt update 

For Ubuntu 18.04 users, install libapache2-mod-fcgid.

sudo apt install libapache2-mod-fcgid

Ubuntu 16.04 users, install libapache2-mod-fastcgi.

sudo apt install libapache2-mod-fastcgi

You may need software-properties-common in order to add a repository with add-apt-repository.

sudo apt install software-properties-common

We will add Ondřej’s PHP repository that will allow us to download co-installable versions of PHP 5.6 and PHP 7.x, then update the package lists again.

sudo add-apt-repository ppa:ondrej/php && sudo apt update

Press ENTER when prompted to add the repository.

2. Install Multiple PHP Versions

You can now install the version of PHP you require. Ondřej’s repository provides PHP 5.6 and PHP 7.x. Below are the examples to install PHP 5.6 and PHP 7.0/7.1/7.2/7.3.

sudo apt install php5.6 php5.6-fpm
sudo apt install php7.0 php7.0-fpm
sudo apt install php7.1 php7.1-fpm
sudo apt install php7.2 php7.2-fpm
sudo apt install php7.3 php7.3-fpm

Press y and ENTER when prompted to install.

Once installed, you should have multiple new sockets in /var/run/php/.

ls /var/run/php/   php7.0-fpm.sock   php7.3-fpm.sock
php5.6-fpm.sock   php7.2-fpm.sock  php-fpm.sock   php7.1-fpm.sock

In Step 4, we will use the <FilesMatch> directive to tell Apache which PHP socket to use.


Note that if you need any other libraries or extensions, they must be installed separately per PHP version. For example, if you need cURL, you would need to install it for all versions.

sudo apt install php5.6-curl
sudo apt install php7.0-curl
sudo apt install php7.1-curl
sudo apt install php7.2-curl
sudo apt install php7.3-curl

3. Configure Apache

We need to add some Apache modules using a2enmod.

Ubuntu 18.04 users.

sudo a2enmod actions alias proxy_fcgi fcgid

Ubuntu 16.04 users.

sudo a2enmod actions alias proxy_fcgi fastcgi

Restart Apache.

sudo systemctl restart apache2

You can now use either Virtual Hosts or .htaccess to instruct Apache which version of PHP to use.

4. .htaccess Method

You can add the <FilesMatch> directive to your .htaccess file. Before you do, make sure that AllowOverride is enabled in Virtual Hosts, otherwise Apache will ignore .htaccess.

Open the Apache config file.

sudo nano /etc/apache2/apache2.conf

Scroll down the the following section and make sure that  AllowOverride  is set to All.

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted

Save and exit (press CTRL + X, press Y and then press ENTER)

Restart Apache.

sudo systemctl restart apache2

Now you can add the  <FilesMatch> directive to .htaccess

Examples for different versions of PHP in .htaccess:

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

Change version number to 7.1/7.2/7.3 for different PHP versions.

6. Check PHP info

To see which version of PHP Apache is serving, create a new file called info.php in your web document root.

In this example, we will create a new file in /var/www/html/

sudo nano /var/www/html/info.php

Enter the following PHP code.

<?php phpinfo(); ?>

Save file and exit. (Press CTRL + X, press Y and then press ENTER)

We can now load this file in the browser by going to or http://your_ip/info.php

Below we can see the PHP info page with the PHP version clearly displayed.

PHP 5.6
PHP 7.0
PHP 7.1
PHP 7.2
PHP 7.3

Don’t forget to delete info.php as it contains information that could be useful to hackers.

sudo rm /var/www/html/info.php

Let me know in the comments if this helped.