How to Run Multiple PHP Versions On Ubuntu 20.04 / 18.04 / 16.04 with Apache

Hostnbit

In this guide we will configure Apache to run PHP 7.x and PHP 5.6 simultaneously, and choose between them using Virtual Hosts or .htaccess.

Prerequisites

You should already have Apache installed and serving web pages before following this guide.

This guide was tested on Ubuntu 20.04, Ubuntu 18.04 and Ubuntu 16.04, though it should also be useful for other Debian-based systems.

1. Add Repository

Let’s begin by updating the package lists.

sudo apt update 

For Ubuntu 20.04 and 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 PHP

You can now install the version of PHP you require. Ondřej’s repository provides PHP 5.6 and PHP 7.x. In this example we will install PHP 5.6 and PHP 7.2.

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

Press y and ENTER when prompted to install.

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

ls /var/run/php/
total 8
-rw-r--r-- 1 root     root     4 Feb 17 16:50 php5.6-fpm.pid
srw-rw---- 1 www-data www-data 0 Feb 17 16:50 php5.6-fpm.sock
-rw-r--r-- 1 root     root     5 Feb 17 16:51 php7.2-fpm.pid
srw-rw---- 1 www-data www-data 0 Feb 17 16:51 php7.2-fpm.sock

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

MySQL Extension

If you intend on using MySQL, you must install the MySQL extension for both PHP 7.x and PHP 5.6.

sudo apt install php5.6-mysql
sudo apt install php7.2-mysql

Other Extensions/Libraries

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 both versions.

sudo apt install php5.6-curl
sudo apt install php7.2-curl

3. Configure Apache

We need to add some Apache modules using a2enmod.

Ubuntu 20.04 / 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. Virtual Hosts Method

Open up your Apache .conf file and add the <FilesMatch> directive to your Virtual Host.

This instructs Apache which PHP socket to use.

PHP 5.6 Example

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
    <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> 

Make sure to restart Apache after making changes to the Virtual Hosts.

sudo systemctl restart apache2

PHP 7.2 Example

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
    <FilesMatch \.php> # Apache 2.4.10+ can proxy to unix socket 
        SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/" 
    </FilesMatch> 
</VirtualHost>

Make sure to restart Apache after making changes to the Virtual Hosts.

sudo systemctl restart apache2

5. htaccess Method

You can also add the <FilesMatch> directive to your .htaccess file. Before you do, make sure that AllowOverride is enabled, 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./etc/apache2/apache2.conf

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

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

PHP 5.6 Example

.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 7.2 Example

.htaccess

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

6. Test PHP

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./var/www/html/info.php

<?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 http://example.com/info.php or http://your_ip/info.php

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

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

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

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Menu Title
Scroll to Top