• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Install Multiple PHP Version with Apache on Ubuntu 18.04 & 16.04

Written by Rahul, Updated on April 26, 2020

Generally, the hos manager used a separate server for each PHP version application deployment. Which increases the hosting cost. Some of the host managers are using Docker to run multiple PHP version on the single server. Also, most of you are aware of the configuration, I used in this tutorial. But some of the system administrators are not aware of this. This tutorial will help you to install multiple PHP version with Apache on Ubuntu 18.04 and 16.04 system without switching PHP versions.

This tutorial describes the installation and configuration of two VirtualHost on Apache with separate PHP versions. First VirtualHost will work with PHP 5.6 and another VirtualHost will run with PHP 7.2. So just go through this tutorial. You can also use more than two PHP versions with Apache as required but this tutorial covers two only.

Apache Installation

Install Apache web server from the official repository. Launch terminal on your system or login with ssh for remote systems. Execute the following commands to install the latest available version of Apache web server.

sudo apt update 
sudo apt install apache2 libapache2-mod-fastcgi 

Ubuntu 18.04 Users:
sudo apt install apache2 libapache2-mod-fcgid

PHP Installation

For the installation of PHP versions, we use the PPA maintained here. Use the below couple of commands to add the PPA to your system.

### On Ubuntu 18.04  
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

### On Ubuntu 16.04  
sudo apt install python-software-properties
sudo add-apt-repository ppa:ondrej/php

For this tutorial, we are using the PHP 5.6 and PHP 7.2 to configure with Apache web server. To use the multiple PHP versions, we will use PHP FPM and FastCGI. Let’s install the following packages on your system.

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

After installation, php-fpm services will be started automatically. Use the following commands to make sure both services are running.

sudo systemctl status php5.6-fpm
sudo systemctl status php7.2-fpm
  • Recommended: How to Enable/Disable PHP Modules on Ubuntu

Apache Configuration

Now enable few modules required for the configuration of multiple PHP versions with Apache. These modules are necessary to integrate PHP FPM and FastCGI with Apache server.

sudo a2enmod actions fastcgi alias proxy_fcgi

Ubuntu 18.04 Users:
sudo a2enmod actions fcgid alias proxy_fcgi

Get ready for the configuration of websites on your Apache server. For the testing purpose, I am configuring two websites to work with two different-2 PHP versions. First, create two directories on your server.

sudo mkdir /var/www/php56
sudo mkdir /var/www/php72

Now, create and index.php containing the phpinfo() function.

echo "<?php phpinfo(); ?>" > /var/www/php56/index.php
echo "<?php phpinfo(); ?>" > /var/www/php72/index.php

Let’s start the creation of VirtualHost. Apache keeps all the VirtualHost configuration files under /etc/apache2/sites-available with the extension .conf. Create a file for the first virtual host and edit in your favorite text editor.

sudo vim /etc/apache2/sites-available/php56.example.com.conf

Add the following content. Make sure to use correct ServerName and directory path according to your setup. This website is configured to work with PHP 5.6.

1
2
3
4
5
6
7
8
9
10
11
12
13
<VirtualHost *:80>
    ServerName php56.example.com
    DocumentRoot /var/www/php56
    <Directory /var/www/php56>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    <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>

Similarly, create a second VirtualHost configuration file to work with PHP 7.2. Edit configuration file in text editor:

sudo vim /etc/apache2/sites-available/php72.example.com.conf

Add the following content to file with proper ServerName and DocumentRoot.

1
2
3
4
5
6
7
8
9
10
11
12
<VirtualHost *:80>
    ServerName php72.example.com
    DocumentRoot /var/www/php72
    <Directory /var/www/php72>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost"
    </FilesMatch>
</VirtualHost>

You both of the websites are configured now. But they are still not active. Apache keeps active sites under /etc/apache2/sites-enabled directory. You can simply create a symbolic link of config files to this directory or use below command to do the same.

sudo a2ensite php56.example.com
sudo a2ensite php72.example.com

After making all the changes restart Apache to reload new settings changes.

sudo systemctl restart apache2

Your setup has been completed now. Go to the next step to test your setup.

Test Setup

Edit /etc/hosts file on your local system and make an entry like below. This will resolve temprory names to localhost IP address.

sudo vim /etc/hosts

Add following entry to end of file

127.0.0.1 php72.example.com
127.0.0.1 php56.example.com

Open a web browser and visit both of the sites. You will see that php56.example.com shows the version PHP 5.6 and php72.example.com is showing the PHP 7.2 as the configuration.

Multiple PHP Version with Apache 1

Multiple PHP Version with Apache 2

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

44 Comments

  1. Avatar RYANB Reply
    January 3, 2021 at 8:43 pm

    THanks for the tutorial, it works on me (Ubuntu 20.04).

    but when i type php –version in terminal , it show that php 7.4 is active, did you know how to switch this to 5.4 ? thanks.

  2. Avatar peter park Reply
    December 31, 2020 at 5:30 am

    Thanks

    but

    How do I solve mysql and mysqli problems?

  3. Avatar Gonzalo Reply
    October 4, 2020 at 2:30 pm

    Thank you very much from Chile.

  4. Avatar Mathieu CABARET Reply
    April 25, 2020 at 1:07 pm

    For Ubuntu 18.04 users, first part of PHP installation should be :
    sudo apt install software-properties-common
    sudo add-apt-repository ppa:ondrej/php

    rather than :
    sudo apt install python-software-properties
    sudo add-apt-repository ppa:ondrej/php

    • Rahul Rahul Reply
      April 26, 2020 at 3:41 am

      Thanks Mathieu, I have updated tutorial accordingly.

  5. Avatar Erick Carvajal Reply
    January 30, 2020 at 8:36 pm

    Nice, How i can use this tutorial to install with mysql + phpmyadmin without errors?

  6. Avatar Yv Reply
    January 22, 2020 at 10:35 am

    Cannot get it work. It shows plain text content of index.php:
    <?php
    phpinfo();

    Can you help please?

    • Rahul Rahul Reply
      January 27, 2020 at 9:02 am

      Hi Yv,

      Make sure the socket path in Apache “/var/run/php/php7.2-fpm.sock” is set to the correct version you have installed.

  7. Avatar Sebastian Czerniak Reply
    September 5, 2019 at 2:13 pm

    There is an mistake in virtualhost file in line below:

    SetHandler “proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/”

    Should be with no slash after localhost:

    SetHandler “proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost”

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

    • Rahul Rahul Reply
      November 21, 2019 at 3:51 am

      Hi Sebastian,

      Thanks for valuable information. I have corrected our tutorials accordingly.

  8. Avatar Samuel Reply
    August 29, 2019 at 3:45 pm

    I love you! haha

  9. Avatar Mike Reply
    July 17, 2019 at 10:30 pm

    Brilliant – been wondering how to do this for years
    Easy solution
    Thanks very much

  10. Avatar David Hay Reply
    May 8, 2019 at 1:22 am

    Thanks man, been struggling with a nice automated setup for my dev environment, now having php version in conf is so clean! I tried a few different methods, but this one worked first time. Thanks again, Dave.

  11. Avatar Lukas Reply
    April 30, 2019 at 4:56 pm

    Good job man, thank you a lot 🙂

  12. Avatar Marco Reply
    April 25, 2019 at 9:43 pm

    I’ve follow those steps but i’m getting a “503 Service Unavailable” error. Why es that?

  13. Avatar Ryan Reply
    April 10, 2019 at 7:46 pm

    I’ve followed these instructions before on one server and it worked but now I’m trying on a different server and the php for version 7.2 is being rendered in the browser. Any idea why that would be happening?

  14. Avatar treminaor Reply
    March 24, 2019 at 4:47 pm

    This worked perfectly on my Ubuntu 16.04 server. If you want to control your PHP version on a per-directory basis instead of using VirtualHosts you can add this to an .htaccess file in a directory you want to specify a non-default PHP version:

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

    I needed php5.6 in my example but you need to change the version number to what you installed.

  15. Avatar Ejoo Reply
    March 11, 2019 at 9:52 pm

    I was looking for this and finally found it here! Thanks a lot, saved my day (night to be exact 😉 ). I did it just the same way with v7.1 and v7.3, works fine
    For Hersdyanata: I missed the line with “sudo a2enmod actions fcgid alias proxy_fcgi” and had just white pages (module proxy_fcgi was not activated).

  16. Avatar Washiul Hoque Reply
    February 27, 2019 at 10:57 am

    Thanks a lot..

  17. Avatar Hugo Nery Reply
    February 11, 2019 at 4:06 pm

    Muito obrigado!

    From Brazil

  18. Avatar Andrew Reply
    January 29, 2019 at 2:51 pm

    Thank you. This worked great for me. I had done the version where you install tasksel and then lamp-server and it came with Apache 2.0 handlers which I did not want, plus I wanted 2 versions. I removed all the PHP packages (using Webmin) and then was able to follow this guide and it worked great on 18.04!

  19. Avatar lautaro Reply
    January 27, 2019 at 10:36 pm

    Thanks!!! u are great

  20. Avatar Vladimir Reply
    January 8, 2019 at 10:24 am

    Very clear manual. Thanks a lot!

  21. Avatar Alex Reply
    January 2, 2019 at 12:33 am

    Much appreciated!

    It worked on Debian 9.6 with phpbrew for PHP 7.1, 7.2 y 7.3

  22. Avatar Hafiz Umer Reply
    December 17, 2018 at 3:21 pm

    Sir you miss just 1 step.

    sudo nano /etc/hosts/

    then hosts file is open on Command line just add

    127.0.0.1 php72.example.com
    127.0.0.1 php56.example.com

  23. Avatar Hafiz Umer Reply
    December 17, 2018 at 2:17 pm

    I did same steps that you mention above But it not working.
    Can any developer help me. already i spend 8 hours.

  24. Avatar Sebas Reply
    October 30, 2018 at 4:29 pm

    Thanks for the tuto, but I still have issue. I received Server not Found error when I run it.

    And when running this line : “sudo a2enmod actions fastcgi alias proxy_fcgi”, fastcgi didn’t work.

    • Avatar José Ayram Reply
      November 12, 2018 at 1:36 pm

      Hello!

      I have the same issue with “sudo a2enmod actions fastcgi alias proxy_fcgi” in Ubuntu 18.04.

      I got this error: ERROR: Module fastcgi does not exist!

      • Rahul Rahul K. Reply
        November 13, 2018 at 5:23 am

        Hi Jose & Sebas,

        There is little update with Ubuntu 18.04. This article is also updated accordingly.

        Install Package:

        $ sudo apt install libapache2-mod-fcgid

        Enable Module:

        $ sudo a2enmod fcgid

  25. Avatar John Reply
    September 27, 2018 at 2:34 am

    Simple and great post! Thank you for it!

    One note, the `libapache2-mod-fastcgi` package you have listed is named `libapache2-mod-fcgid` in more recent repos.

    Thanks again!

  26. Avatar Corona Reply
    September 13, 2018 at 9:24 am

    There is no package libapache2-mod-fastcgi in 18.04

  27. Avatar Mathieu Reply
    July 28, 2018 at 1:34 pm

    Thanks for this tutorial
    How does it work for a
    I try and it does not work
    It does not take into account the chosen php

    Thank you

  28. Avatar Audrey Reply
    July 28, 2018 at 4:45 am

    can we set what php using on .htaccess like cPanel did?

    maybe if branch or function?

    SetHandler “proxy:unix:/var/run/php/php{ReadFormHandler}-fpm.sock|fcgi://localhost/”

  29. Avatar alkaaf Reply
    July 25, 2018 at 4:57 pm

    Sir. Just… Thank you. You save a lot of my time to upgrade my code into newer php version

  30. Avatar dibet Reply
    June 12, 2018 at 11:06 pm

    Man , Fantastic. Thanks.

  31. Avatar Ela Reply
    May 30, 2018 at 9:09 pm

    What this line used for?

    SetHandler “proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/”

    Normally I noticed apache vs mod_php and nginx vs php fpm packages

    • Avatar Arnothar Reply
      June 7, 2018 at 12:25 pm

      It redirects all php-script to the php interpreter binary using a local socket.

  32. Avatar Jos Reply
    May 10, 2018 at 6:15 am

    Quickly ran into this error:

    Package libapache2-mod-fastcgi is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package ‘libapache2-mod-fastcgi’ has no installation candidate

    I’ve tried several online solutions for getting the lib after adding a lot of repo’s, but error remains.
    Any ideas?

    • Avatar Dan Reply
      August 22, 2018 at 12:39 am

      try searching:
      $ apt-cache search fastcgi | grep apache2
      apache2-utils – Apache HTTP Server (utility programs for web servers)
      libapache2-mod-fcgid – FastCGI interface module for Apache 2
      …

      so, apt-get install libapache2-mod-fcgid

  33. Avatar Johny Reply
    May 8, 2018 at 5:56 am

    Thank you so much. It helps me.

  34. Avatar Steven Reply
    April 16, 2018 at 2:26 pm

    Thank you so much for this instruction! 🙂

  35. Avatar Umair Reply
    March 8, 2018 at 5:51 am

    Great Work

  36. Avatar hersdyanata Reply
    March 5, 2018 at 9:24 am

    hey, i follows your instructions. but mine cannot render the php script. where did i miss?

  37. Avatar Jimmy Reply
    February 26, 2018 at 4:32 pm

    Thanks, it saved additional server cost…

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy