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 and PHP7.2 on a single Debian 9 Server.

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

sudo 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 quickly deploy your cloud servers.

Once you have signed up, log into your Cloudwafer Client Area and deploy your Cloudwafer cloud server.

Updating System Packages
It is always recommended that you update the system to the latest packages before beginning any major installations. Issue the command below:

sudo apt-get update && sudo apt-get upgrade

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

sudo apt install php

The first step is to install the software-properties-common package.

sudo apt install software-properties-common

Next, issue the commands below to add the PHP PPA on your Debian system.

wget -q -O- | sudo apt-key add -
echo "deb stretch main" | sudo tee /etc/apt/sources.list.d/php.list
sudo apt update

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

Apache Web Server:

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

PHP 5.6

PHP 7.2

Nginx Web Server:

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



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 
 sudo update-alternatives --set php /usr/bin/php5.6

 Set Default PHP Version 7.2  
 sudo 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. Next, enable the version you need using the a2enmod command. Lastly, restart the Apache webserver.

a2dismod php5.6
a2enmod php5.6
systemctl restart apache2