LinuxTutorialsUbuntu

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 Versions on Ubuntu 18.04 LTS. For those of you who didn’t know, As a web developer or system administrator, you may often find yourself in a situation where you need to run multiple PHP versions on a single server. This requirement can arise due to various reasons, such as supporting legacy applications, testing new PHP features, or maintaining compatibility with different frameworks and libraries. Fortunately, Ubuntu 18.04 LTS provides a convenient way to install and manage multiple PHP versions using the PPA (Personal Package Archive) from Ondřej Surý.

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 the step-by-step installation of Multiple PHP versions on a Ubuntu 18.04 (Bionic Beaver) server.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Multiple PHP Versions on Ubuntu 18.04 LTS Bionic Beaver

Step 1. Before we begin, ensure that you have a running Ubuntu 18.04 LTS server and that your system’s package index is up-to-date. You can update the package index by running the following command:

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 the 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 the status of 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 Versions. 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 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!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button