How to Install Multiple Versions of PHP on Ubuntu 20.04, 18.04, 20.10

This tutorial shows you how to install multiple versions of PHP on Ubuntu and make 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 18.04 repository includes PHP7.2 and Ubuntu 20.04 repository includes PHP7.4. We will see how to install PHP7.3, PHP7.4, and PHP8.0 on a single Ubuntu server.

Install Multiple Versions of PHP on Ubuntu via PPA

The easiest way to install multiple versions of PHP is by using the PPA from Ondřej Surý, who is a Debian developer. To add this PPA, run the following commands in the 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 PHP8.0 on Ubuntu by executing the following command.

sudo apt install php8.0 php8.0-fpm

And install some common PHP8.0 extensions.

sudo apt install php8.0-mysql php8.0-mbstring php8.0-xml php8.0-gd php8.0-curl

You can view all available PHP7.4 extensions by typing in sudo apt install php8.0 and pressing Tab key twice.

ubuntu-install-multiple-php-versions

To install PHP7.4 on Ubuntu , run

sudo apt install php7.4 php7.4-fpm

Install some common PHP7.4 extensions.

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

You can install PHP7.3 in the same way. Simply replace the version number in these commands.

Hint: PHP5.6, PHP7.0 and PHP7.2 reached end-of-life, but you can still get security fixes from this PPA.

Switching PHP Version in Apache Virtual Host

By default, Apache uses one PHP version across all virtual hosts. If you want to use a 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.4 installed on my Ubuntu 20.04 server,

multiple-php-versions-apache-ubuntu-server-20.04

I disable it by running:

sudo a2dismod php7.4

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-server-20.04

Let’s say you want to use PHP8.0 in an Apache virtual host, then open the virtual host configuration file and add the following line in the <VirtualHost> tags.

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

Like this:

switch php version ubuntu 20.04 apache

Save and close the file. Then check syntax.

sudo apachectl -t

Restart Apache for the change to take effect.

sudo systemctl restart apache2

Now let’s say you want to use PHP7.3 in Mautic virtual host because Mautic isn’t compatible with PHP7.4, then add the following line in your Mautic virtual host between <VirtualHost> tags and then restart Apache.

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

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

ubuntu 20.04 install php8.0

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 ubuntu 20.04

As you can see from the screenshot, there are several PHP-FPM socket files (.sock) 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$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

nginx multiple php versions ubuntu 18.04

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

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

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

fastcgi_pass unix:/run/php/php7.3-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

ubuntu 20.04 install php8.0

How To Set the Default PHP Version for the Command Line

If you install a newer version of PHP on Ubuntu, then it will become the default version for the command line. I installed PHP8.0, but I still want PHP7.4 as the default version for the command line.

To set the default command line PHP version, run the following command.

sudo update-alternatives --config php

set the default command line PHP version ubuntu

To check the current version of command-line PHP, run

php --version

php --version ubuntu

Wrapping Up

That’s it! I hope this tutorial helped you run multiple PHP versions on Ubuntu 18.04, 20.04, 20.10 with Apache and Nginx. You may also want to set up the ModSecurity web application firewall to protect your PHP web applications from hacking. If you use Apache web server on Debian/Ubuntu, then read the following tutorial.

If you use Nginx web server on Debian/Ubuntu, then read the following tutorial:

As always, if you found this post useful, then subscribe to our free newsletter to get more useful tutorials 🙂

Rate this tutorial
[Total: 5 Average: 4.8]

4 Responses to “How to Install Multiple Versions of PHP on Ubuntu 20.04, 18.04, 20.10

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.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image.