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

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

Last updated on

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 16.04 and Ubuntu 18.04, though it should also be useful for other Debian-based systems.

If you need to install Apache on Ubuntu, please see:

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

PHP 7 info test page on Apache and Ubuntu 18.04 Bionic Beaver

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. Follow me on Twitter, Facebook and YouTube.

p.s. I increased my AdSense revenue by 68% using AI 🤖. Read my Ezoic review to find out how.

1 Star2 Stars3 Stars4 Stars5 Stars 5.00 (7 votes)

Leave a Reply

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

We use Markdown to style comments, like on Github and Reddit.
To do a line break, type two spaces after the sentence.
You can add inline code by wrapping it in backticks: `code here`

    To do an entire block of code  
    type four spaces before the line
    and it will appear in a block like this.
    <-- four empty spaces

11 replies

Hi,
I am getting a 503 Error.
Service Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

Please help thanks.

There is an error in virtualhost file at following line:
SetHandler “proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/”

You should remove slash after localhost like below:
SetHandler “proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost”

Otherwise, $_SERVER[‘SCRIPT_FILENAME’] will set with two leading slashes.

Thanks for the tutorial, it was very useful.

I’m not an expert in this area and my 2 local sites are now running with PHP 7.2 and 5.6 respectively.

The only thing I had to do was to start these services after installing PHP:

sudo service php5.6-fpm start
sudo service php7.2-fpm start

to have the following files available:
/var/run/php/php*-fpm.sock

I don’t know if that was the right thing to do, but after reading here and there, I tried that and it worked.

I already had php 7.2 running, so I only added php 5.6.

Another thing I noticed is that in PHP info output, before I had:

Server API : Apache 2.0 Handler

and after following this guide, now PHP info shows:

Server API: FPM/FastCGI

I’d like to understand a bit more about why I see this change just to understand better if you have the time.

I’m running Apache on Ubuntu 18.04.

Anyways thanks again for the guide.