PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. Different applications require various versions of PHP, so in this tutorial, we are going to install multiple versions of PHP on Ubuntu 16.04.

We will also show how to set the default version of PHP to be used on the Ubuntu system as when required. We will be installing PHP5.6, PHP7.0, PHP7.1 and PHP7.2 on a single Ubuntu 16.04 server.

Currently, the supported stable version of PHP in the Ubuntu software repositories is the PHP 7.x. You can confirm this by running the apt command below.

apt show php -a

Deploying your cloud server
If you have not already registered with Cloudwafer, you should begin by getting signed up. Take a moment to create an account after which you can easily deploy your own cloud servers.

Once you have signed up, log into your Cloudwafer Client Area with the password provided in your mail and deploy your Cloudwafer cloud server.

First, we'll be installing the default PHP version from the Ubuntu software repositories using the command below.

apt install php

To install multiple versions of PHP, the first step is to install the software-properties-common package.

apt install python-software-properties

Next, we need to add the PPA repository by Ondřej Surý which contains multiple versions of PHP. At the prompt, Press [ENTER] to continue.

add-apt-repository ppa:ondrej/php

Next, we need to update the system by using the command below:

apt-get update

We can now proceed to install different supported versions of PHP with the commands below:

Apache Web Server:

apt install php5.6   [PHP 5.6]
apt install php7.0   [PHP 7.0]
apt install php7.1   [PHP 7.1]
apt install php7.2   [PHP 7.2]

From the screenshot below, all four versions have been installed.

Nginx Web Server:

apt install php5.6-fpm   [PHP 5.6]
apt install php7.0-fpm   [PHP 7.0]
apt install php7.1-fpm   [PHP 7.1]
apt install php7.2-fpm   [PHP 7.2]

From the screenshot below, all four versions have been installed.

If you require additional PHP modules, install them by specifying the PHP version and use the auto-completion functionality (TAB button on the keyboard) to view all modules as follows.

You can check the default version of PHP version installed by typing the command below:

php -v

Configuring Default PHP Version in Ubuntu
To set the default PHP version to be used on the system, the update-alternatives command is required.

 Set Default PHP Version 5.6 
 update-alternatives --set php /usr/bin/php5.6

 Set Default PHP Version 7.0  
 update-alternatives --set php /usr/bin/php7.0

 Set Default PHP Version 7.1  
 update-alternatives --set php /usr/bin/php7.1

Set Default PHP Version 7.2 update-alternatives --set php /usr/bin/php7.2

After changing the default version, confirm the PHP version was changed correctly by checking the version using the command below:

php -v

After switching from one version to another, use the command below to locate the PHP configuration file:

php -i | grep "Loaded Configuration File"

To set the PHP version that will work with Apache web server, you need to first disable the current version with the a2dismod command, then enable the version you need using the a2enmod command and lastly, restart the Apache webserver.

a2dismod php7.2
a2enmod php5.6
systemctl restart apache2

We have successfully installed all the supported versions of PHP in Ubuntu and its derivatives.