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

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

Last updated on | 37 replies

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.


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.

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

sudo apt install php5.6 php5.6-fpm
sudo apt install php7.4 php7.4-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
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
srw-rw---- 1 www-data www-data 0 Feb 17 16:51 php7.4-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.4-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.4-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

You may need to add these two PHP services to the Apache config with:

sudo a2enconf php5.6-fpm
sudo a2enconf php7.4-fpm

And then start them with:

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

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

<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/" 

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

sudo systemctl restart apache2

PHP 7.4 Example

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

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.

<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

PHP 5.6 Example

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

PHP 7.4 Example

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

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.


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 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 if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

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

37 replies

Leave a reply

Your email address will not be published.

    1. make a file .htaccess in a folder in /var/www/html folder (example 7.4)
      put that .htaccess in folder 7.4
      open .htaccess

      fill this :

      # Apache 2.4.10+ can proxy to unix socket
      SetHandler “proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/”

      and save.

      for phpmyadmin
      download manually.
      extract and put in folder 7.4

  1. I had missed a step – instructing PHP which version to use. Since I am running my site with SSL, I edited /etc/apache2/sites-available/

    DocumentRoot /var/www/
    # Apache 2.4.10+ can proxy to unix socket
    SetHandler “proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost/”

    But in reality – with or without this edit, phpinfo.php reports the same thing – that the Server API is FPM/FastCGI. I include the first part of the output below – it looks nothing like yours, which doesn’t appear to make reference to php-fpm. Elsewhere, folks are telling me php-fpm is deciding ownership and permissions for uploaded files, not Apache, which would explain why my changes to envvars don’t seem to have any effect. Any ideas about what accounts for the difference, apart from the fact that I am using SSL?

    PHP Version 7.4.20
    System Linux 5.4.0 #1 SMP Thu Apr 22 16:18:59 MSK 2021 x86_64
    Build Date Jun 4 2021 21:24:55
    Server API FPM/FastCGI
    Virtual Directory Support disabled
    Configuration File (php.ini) Path /etc/php/7.4/fpm
    Loaded Configuration File /etc/php/7.4/fpm/php.ini
    Scan this dir for additional .ini files /etc/php/7.4/fpm/conf.d
    Additional .ini files parsed /etc/php/7.4/fpm/conf.d/10-mysqlnd.ini, /etc/php/7.4/fpm/conf.d/10-opcache.ini, /etc/php/7.4/fpm/conf.d/10-pdo.ini, /etc/php/7.4/fpm/conf.d/15-xml.ini, /etc/php/7.4/fpm/conf.d/20-calendar.ini, /etc/php/7.4/fpm/conf.d/20-ctype.ini, /etc/php/7.4/fpm/conf.d/20-dom.ini, /etc/php/7.4/fpm/conf.d/20-exif.ini, /etc/php/7.4/fpm/conf.d/20-ffi.ini, /etc/php/7.4/fpm/conf.d/20-fileinfo.ini, /etc/php/7.4/fpm/conf.d/20-ftp.ini, /etc/php/7.4/fpm/conf.d/20-gettext.ini, /etc/php/7.4/fpm/conf.d/20-iconv.ini, /etc/php/7.4/fpm/conf.d/20-json.ini, /etc/php/7.4/fpm/conf.d/20-mbstring.ini, /etc/php/7.4/fpm/conf.d/20-mysqli.ini, /etc/php/7.4/fpm/conf.d/20-pdo_mysql.ini, /etc/php/7.4/fpm/conf.d/20-phar.ini, /etc/php/7.4/fpm/conf.d/20-posix.ini, /etc/php/7.4/fpm/conf.d/20-readline.ini, /etc/php/7.4/fpm/conf.d/20-shmop.ini, /etc/php/7.4/fpm/conf.d/20-simplexml.ini, /etc/php/7.4/fpm/conf.d/20-sockets.ini, /etc/php/7.4/fpm/conf.d/20-sysvmsg.ini, /etc/php/7.4/fpm/conf.d/20-sysvsem.ini, /etc/php/7.4/fpm/conf.d/20-sysvshm.ini, /etc/php/7.4/fpm/conf.d/20-tokenizer.ini, /etc/php/7.4/fpm/conf.d/20-xmlreader.ini, /etc/php/7.4/fpm/conf.d/20-xmlwriter.ini, /etc/php/7.4/fpm/conf.d/20-xsl.ini
    PHP API 20190902
    PHP Extension 20190902
    Zend Extension 320190902
    Zend Extension Build API320190902,NTS
    PHP Extension Build API20190902,NTS

    1. Unfortunately, I don’t have an article for Nginx yet, but steps 1 and 2 are the same, instead you edit your Nginx conf and add/edit fastcgi_pass unix:/run/php/phpX.X-fpm.sock changing phpX.X to the version of PHP you want to run for that particular virtual host.

  2. Excellent solution, but I agree with the friends below, steps missing:

    a2enconf php5.6-fpm
    a2enconf php7.2-fpm

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

    service apache2 reload

  3. Thank you for this tutorial.
    I want to ask how to specify a version of each to install special extension say for eample oci8?

  4. FastCGI or FPM cant implement php_value in .htaccess files, If I follow this tutorial, how can I solve the need to use phpvalue?

  5. I keep getting a ‘503 Service unavailable’ error.
    I followed the instructions here, except for ‘4. Virtual Hosts Method’.
    I even followed the instructions in the comments here on this problem.
    Still I only get ‘503 Service unavailable’.
    Please, anyone, help me…!

  6. If you get “Service unavailable” then you need to start the services:

    sudo service php5.6-fpm start

    sudo service php5.6-fpm start


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

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

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

    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.