How To Install Multiple PHP Version on Ubuntu 18.04 LTS

Install Multiple PHP Version on Ubuntu 18.04 LTS

In this tutorial, we will show you how to install Multiple PHP Version on Ubuntu 18.04 LTS. For those of you who didn’t know, PHP (recursive acronym for PHP: Hypertext Preprocessor) is an open-source, popular general-purpose scripting language that is widely-used and best suited for developing websites and web-based applications. It is a server-side scripting language that can be embedded in HTML.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo’ to the commands to get root privileges. I will show you through the step by step installation of Multiple PHP Version a Ubuntu 18.04 (Bionic Beaver) server.

Install Multiple PHP Version on Ubuntu 18.04 LTS Bionic Beaver

Step 1. First, make sure that all your system packages are up-to-date by running these following apt-get commands in the terminal.

sudo apt-get update
sudo apt-get upgrade
sudo apt install apache2 libapache2-mod-fcgid

Step 2. Installing PHP.

First, add the PPA to your system:

sudo apt install python-software-properties
sudo add-apt-repository ppa:ondrej/php

For this tutorial, we are using PHP 5.6 and PHP 7.2 to configure with Apache webserver. To use the multiple PHP versions, we will use PHP FPM:

apt update
sudo apt install php5.6 php5.6-fpm
sudo apt install php7.2 php7.2-fpm

After installation, check status PHP services are running:

systemctl status php5.6-fpm
systemctl status php7.2-fpm

Step 3. Apache Web Server Configuration.

First, create two directories on your server:

mkdir /var/www/php56
mkdir /var/www/php72

Next, create a test file named info.php with the content below. Save the file, then browse to it to see if PHP is working:

echo "<?php phpinfo(); ?>" > /var/www/php56/index.php
echo "<?php phpinfo(); ?>" > /var/www/php72/index.php

Let’s start the creation of VirtualHost and edit in your favorite text editor:

nano /etc/apache2/sites-available/php56.example.com.conf

Add the following content:

<VirtualHost *:80>
ServerName php56.example.com
DocumentRoot /var/www/php56
<Directory /var/www/php56>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<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>

Then, create a second VirtualHost configuration file to work with PHP 7.2:

nano /etc/apache2/sites-available/php72.example.com.conf

Add the following content:

<VirtualHost *:80>
ServerName php72.example.com
DocumentRoot /var/www/php72
<Directory /var/www/php72>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"
</FilesMatch>
</VirtualHost>

Create a symbolic link of config files to this directory or use the below command:

sudo a2ensite php56.example.com
sudo a2ensite php72.example.com
sudo a2enmod actions fastcgi alias proxy_fcgi

After making all the changes restart Apache to reload new settings changes:

systemctl restart apache2

Step 4. Test Multiple PHP Version.

Try to access it at php56.example.com shows the version PHP 5.6 and php72.example.com is showing PHP 7.2. If the PHP info page is rendered in your browser then everything looks good and you are ready to proceed further.

Congratulations! You have successfully installed Multiple PHP Version. Thanks for using this tutorial for installing Multiple PHP on Ubuntu 18.04 LTS (Bionic Beaver) system. For additional help or useful information, we recommend you to check the official PHP website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!