How to Install Multiple Versions of PHP on Ubuntu 16.04/17.10

This tutorial shows you how to install multiple versions of PHP on Ubuntu 16.04/Ubuntu 17.10 and let a particular Apache virtual host or Nginx server block use one of them. This is useful when you have multiple web applications on your server but one or two of them isn’t compatible with the PHP version in the default Ubuntu repository. Ubuntu 16.04 repository includes PHP7.0 and Ubuntu 17.10 repository includes PHP7.1. We will see how to install PHP5.6, PHP7.0, PHP7.1 and PHP7.2 on a single Ubuntu 16.04/17.10 server.

Install Multiple Versions of PHP on Ubuntu 16.04/17.10 via PPA

The easiest way to install multiple versions of PHP is by using the PPA from Ondřej Surý, who is also the maintainer of certbot PPA. To add this PPA, run the following commands in terminal. The software-properties-common package is needed if you want to install software from PPA. It’s installed automatically on Ubuntu desktop, but might be missing on your Ubuntu server.

sudo apt install software-properties-common

sudo add-apt-repository ppa:ondrej/php

sudo apt update

Now you can install PHP7.2 on Ubuntu 16.04/17.10 by executing the following command.

sudo apt install php7.2 php7.2-fpm

And install some common PHP7.2 extensions.

sudo apt install php7.2-mysql php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl

You can view all available PHP7.2 extensions by typing in sudo apt install php7.2 and pressing Tab key twice.

ubuntu 16.04 multiple php versions

To install PHP7.1 on Ubuntu 16.04/17.10, run

sudo apt install php7.1 php7.1-fpm

Install some common PHP7.1 extensions.

sudo apt install php7.1-mysql php7.1-mbstring php7.1-xml php7.1-gd php7.1-curl

You can install PHP7.0 and PHP5.6 in the same way. Simply replace the version number in these commands.

Switching PHP Version in Apache Virtual Host

By default, Apache uses one PHP version across all virtual hosts. If you want to use different PHP version in a particular virtual host, you will need to disable Apache PHP module and run PHP code via PHP-FPM. Check if mod_php is installed.

dpkg -l | grep libapache2-mod-php

If it’s installed, you need to disable it. For example, I have libapache2-mod-php7.1 installed on my Ubuntu 17.10 server,

multiple php versions apache ubuntu

I disable it by running:

sudo a2dismod php7.1

You also need to disable the prefork MPM module.

sudo a2dismod mpm_prefork

Now you need to run the following command to enable three modules in order to use PHP-FPM, regardless of whether mod_php is installed on your server.

sudo a2enmod mpm_event proxy_fcgi setenvif

The PHP-FPM configuration snippet is located at /etc/apache2/conf-available/.

run multiple php versions ubuntu

Let’s say you want to use PHP7.2 in your WordPress virtual host, then just copy everything in php7.2-fpm.conf and paste them in your WordPress virtual host configuration file between <VirtualHost> tags. So your virtual host config file looks like below.

switch php version ubuntu

Save and close the file. Restart Apache for the change to take effect.

sudo systemctl restart apache2

Now let’s say you want to use PHP7.1 in Mautic virtual host because Mautic isn’t compatible with PHP7.2, then just copy everything in php7.1-fpm.conf and paste them in your Mautic virtual host between <VirtualHost> tags and then restart Apache.

To test which PHP version is used by your virtual host, you can create a info.php file in your document root (let’s say /var/www/html).

sudo nano /var/www/html/info.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file. Then in your browser address bar, type in

your-domain.com/info.php

downgrade php 7 to 5.6 ubuntu

Switching PHP Version in Nginx Server Block

It’s very easy to switch PHP version in Nginx server block. As you probably know, Nginx runs PHP code via PHP-FPM, which listens on a Unix socket. The socket file is located in /run/php/ directory.

php-fpm change php version

As you can see from the screenshot, there are four PHP-FPM socket files on my server corresponding to different PHP versions. Your Nginx server block file typically has the following snippet to connect to PHP-FPM server.

location ~ \.php$ {
    fastcgi_pass unix:/run/php/php5.6-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
  }

nginx multiple php versions

The PHP version being used by Nginx is determined by the second line in the snippet. To use PHP7.2 in a particular Nginx server block, change that line to:

fastcgi_pass unix:/run/php/php7.2-fpm.sock;

To use PHP7.0 in Nginx server block, change that line to

fastcgi_pass unix:/run/php/php7.0-fpm.sock;

I’m sure you get the idea now. Save and close the file. Then reload Nginx for the changes to take effect.

sudo systemctl reload nginx

To test which PHP version is used by your server block, you can create a info.php file in your document root (let’s say /var/www/html).

sudo nano /var/www/html/info.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file. Then in your browser address bar, type in

your-domain.com/info.php

downgrade php 7 to 5.6 ubuntu

That’s it! I hope this tutorial helped you run multiple PHP versions on Ubuntu 16.04/Ubuntu 17.10 with Apache and Nginx.  As always, if you found this post useful, then subscribe to our free newsletter or follow us on Google+Twitter or like our Facebook page.

Rate this tutorial
[Total: 4 Average: 4.8]

4 Responses to “How to Install Multiple Versions of PHP on Ubuntu 16.04/17.10

  • Great tutorial, thanks!

    Small hint: If you don’t want to copy the complete code-block into every vhost, just use the Include directive:

    Include /etc/apache2/conf-available/php7.0-fpm.conf

    Just put this into the VirtualHost and you’re set 🙂

  • Thanks, this works fine. I’m a novice but got it working simple enough. Cheers.

  • Would the process be the same for 18.04?

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • If my answer helped you, please consider supporting this site. Thanks :)