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

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.

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

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

.htaccess
<FilesMatch \.php> 
    # Apache 2.4.10+ can proxy to unix socket 
    SetHandler "proxy:unix:/var/run/php/php7.4-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
1 Star2 Stars3 Stars4 Stars5 Stars 4.33 (18 votes)

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.

Subscribe
Notify of
guest
34 Comments
newest
oldest
Inline Feedbacks
View all comments
Leo Júnior
Leo Júnior
1 month ago

Ei man thank you =)

theto
theto
2 months ago

how to setup multiple php versions for nginx?

Djam
Djam
3 months ago

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

Mayur
Mayur
3 months ago

php myadmin is not working with php 5.6 but working with 7.2 how to fix it?

Mohammed Helewa
Mohammed Helewa
5 months ago

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

Walter Nuñez
Walter Nuñez
6 months ago

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

René Mansveld
René Mansveld
6 months ago

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…!

looper
looper
7 months ago

all perfect but phpmyadmin not work, can u help me?

Mayur
Mayur
3 months ago
Reply to  looper

did you fixed the issue? cause mine too not working bruh

Shah Alom
Shah Alom
8 months ago

Thanks Man! You just saved the day &lt;3

Ale
Ale
8 months ago

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

sudo service php5.6-fpm start

sudo service php5.6-fpm start

best!

Ale
Ale
8 months ago

thank you!!!

Daniele
Daniele
10 months ago

Perfect!

Leo
Leo
1 year ago

Briljant instruction, thanks.

FC
FC
1 year ago

Thanks, it worked for me.

Carlos
Carlos
1 year ago

Great tutorial, thanks for share!

Santanu
Santanu
1 year ago

Thanks for your tutorial , it worked perfectly.

Santanu

Sukhveer Singh
Sukhveer Singh
1 year ago

Thankyou so much, it worked for me.

Shubham Hirau
Shubham Hirau
1 year ago

Thanks for this Knowledge.

Pavel
Pavel
1 year ago

Thanks!

Erison Vinicios
Erison Vinicios
1 year ago

Muito obrigado, me salvou. Este foi o unico tutorial que funcionou pra mim. PARABENS

va
va
1 year ago

a2enconf php5.6-fpm is missing

Shawn
Shawn
1 year ago

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.

Sebastian Czerniak
Sebastian Czerniak
1 year ago

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.

Javier
Javier
1 year ago

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.

Michel
Michel
1 year ago

Thanks a lot, it worked perfectly.

dahry
dahry
1 year ago

how to install phpmyadmin in here. not work

dahry
dahry
1 year ago

how to install in here.

Miklos
Miklos
1 year ago

How do you handle redis and/or memcached in this situation?

Michael
Michael
1 year ago

Great Tutorial helped me a lot working with a specific web app that needs php 5.6