How to Install Multiple PHP on CentOS 7/8 and Ubuntu 20.04

Install Multiple PHP on CentOS and ubuntu

In this tutorial, we will learn how to Install PHP on CentOS 7 and Ubuntu with important extensions with the help of repository .

Introduction

PHP play a important role in LAMP application ( WordPress, Media wiki, Joomla etc). Now a days most of application require PHP 7 because PHP 7 load your web application faster and will consume less server’s resource like CPU and RAM

Important extensions used with PHP –

intl
mbstring
gd
xml
mysqli
imap
zip
curl
json
ldap
mysqlnd
opcache

PHP installation process is different according to operating system’s. So here we will take a look multiple PHP installation based on two popular OS.

  • CentOS 7
  • CentOS 8
  • Ubuntu 20.04

Install Epel Repository on CentOS 7

  • Login to your server using root access or sudo privileged user and run the below command that will install epel repository along with yum-utils and enable it.

Read Also: Understanding ln Command in Linux (Create Symbolic Links)

It’s a third party repository that will help to install further dependencies and yum-utils also help us to install yum related packages to enable or disable other repo’s.

yum install epel-release yum-utils -y
  • Now we need to download and install remi repository that includes multiple PHP version packages, so we will use yum command to install remi-release-7.rpm directly from its official source.
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y

Install / Enable REMI Repository on CentOS 8

  • Run the following command it will install the Remi-repository on CentOS 8.
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
  • Check enabled repositories
sudo yum repolist
  • Install yum-config-manager it will help you to enable and disable a repository through the command line
sudo dnf -y install dnf-utils

Install PHP with Extensions

  • Now as we have installed the Remi repo, we will Install PHP 5.6 version with important extension with these below command.
sudo yum install php56 php56-php php56-php-intl php56-php-mbstring php56-php-gd php56-php-xml php56-php-imap php56-php-zip php56-php-curl php56-php-json php56-php-ldap php56-php-mysqlnd php56-php-opcache php56-php-fpm -y
  • Similar command but for installation of PHP 7.0 version with important extension.
sudo yum install php70 php70-php php70-php-intl php70-php-mbstring php70-php-gd php70-php-xml php70-php-imap php70-php-zip php70-php-curl php70-php-json php70-php-ldap php70-php-mysqlnd php70-php-opcache php70-php-fpm -y
  • Now again Install PHP 7.1 version with important extension.
sudo yum install php71 php71-php php71-php-intl php71-php-mbstring php71-php-gd php71-php-xml php71-php-imap php71-php-zip php71-php-curl php71-php-json php71-php-ldap php71-php-mysqlnd php71-php-opcache php71-php-fpm -y
  • Install PHP 7.2 version with important extension.
sudo yum install php72 php72-php php72-php-intl php72-php-mbstring php72-php-gd php72-php-xml php72-php-imap php72-php-zip php72-php-curl php72-php-json php72-php-ldap php72-php-mysqlnd php72-php-opcache php72-php-fpm -y
  • For Install PHP 7.3 version with important extension.
sudo yum install php73 php73-php php73-php-intl php73-php-mbstring php73-php-gd php73-php-xml php73-php-imap php73-php-zip php73-php-curl php73-php-json php73-php-ldap php73-php-mysqlnd php73-php-opcache php73-php-fpm -y
  • Install PHP 7.4 version with important extension.
sudo yum install php74 php74-php php74-php-intl php74-php-mbstring php74-php-gd php74-php-xml php74-php-imap php74-php-zip php74-php-curl php74-php-json php74-php-ldap php74-php-mysqlnd php74-php-opcache php74-php-fpm -y
  • To check the default PHP version on the server run the following command.
php -v
  • If you want to change the default php version for a session, simple run the following command.
scl enable php73 bash
  • If you want to change the Php version permanently then run the following command.

Remove the default Php version

sudo dnf module reset php -y

Set the default php version

sudo dnf module enable php:remi-7.3 -y

Change the PHP version as per your requirement.

  • To check the installed PHP Modules or extension simply run the following command.
php -m

Installation on Ubuntu 20.04

Ubuntu 20.04 famous and used widely in as server’s or home computer. here above we have already installed the multiple php version for CentOS 7 it’s time to explain the steps for Ubuntu 20.04.

Let’s start by downloading and installing a third party repository as the same remi in CentOS but here it’s ppa repository.

so let’s get ready with server root access or sudo privileges user.

  • To download and install ppa repository using apt command, just simply run the following commands.
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php

First command will install the required package and install any further dependency packages that we might need in next command. The Second command is used to download the repository and enable that, in between of the second command you will get the following output:

# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
 More info: https://launchpad.net/~ondrej/+archive/ubuntu/php
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Press [ ENTER ]key to start the installation.

  • Now update the packages from newly installed repo if any package is available to update.
sudo apt update -y
  • So just start the installation of PHP 5.6 directly with important extension by running below command on terminal.
sudo apt install php5.6 php5.6-intl php5.6-mbstring php5.6-gd php5.6-xml php5.6-imap php5.6-zip php5.6-curl php5.6-json php5.6-ldap php5.6-mysqlnd php5.6-opcache php5.6-fpm -y
  • Now run this below command for installation of PHP 7.0 version with important extension.
sudo apt install php7.0 php7.0-intl php7.0-mbstring php7.0-gd php7.0-xml php7.0-imap php7.0-zip php7.0-curl php7.0-json php7.0-ldap php7.0-mysqlnd php7.0-opcache php7.0-fpm -y
  • Same for Installation PHP 7.1 version with important extension.
sudo apt install php7.1 php7.1-intl php7.1-mbstring php7.1-gd php7.1-xml php7.1-imap php7.1-zip php7.1-curl php7.1-json php7.1-ldap php7.1-mysqlnd php7.1-opcache php7.1-fpm -y
  • Now again install PHP 7.2 version with important extension.
sudo apt install php7.2 php7.2-intl php7.2-mbstring php7.2-gd php7.2-xml php7.2-imap php7.2-zip php7.2-curl php7.2-json php7.2-ldap php7.2-mysqlnd php7.2-opcache php7.2-fpm -y
  • Install PHP 7.3 version with important extension.
sudo apt install php7.3 php7.3-intl php7.3-mbstring php7.3-gd php7.3-xml php7.3-imap php7.3-zip php7.3-curl php7.3-json php7.3-ldap php7.3-mysqlnd php7.3-opcache php7.3-fpm -y
  • Install PHP 7.4 version with important extension.
sudo apt install php7.4 php7.4-intl php7.4-mbstring php7.4-gd php7.4-xml php7.4-imap php7.4-zip php7.4-curl php7.4-json php7.4-ldap php7.4-mysqlnd php7.4-opcache php7.4-fpm -y
  • To check the default PHP version on the server run the following command.
php -v
  • To check the installed PHP Modules or extension simply run the following command.
php -m

Switch between PHP versions

When we install multiple PHP versions on the server, sometimes we need to change the default PHP version.

To change the default php version, simply run the following command.

  • First disable the default PHP version. For example server have PHP 7.2 as default PHP version and I have to switch to PHP 7.4.
sudo a2dismod php7.2

Change the PHP version as per your requirement which you want to disable.

  • Now enable the required PHP version.
sudo a2enmod php7.4
  • Set the default PHP version.
sudo update-alternatives --set php /usr/bin/php7.4
  • To set the default PHP version globally, run the following command.
sudo update-alternatives --config php

Above you have seen the installation of multiple PHP versions on CentOS 7 and Ubuntu 20.04 both. It totally depends on you which PHP version you need to install, it’s not necessary that you install all the versions. If you require any specific version, just install them only and ignore the rest versions.

Conclusion

In the Linux operating system, Multiple PHP version installation and setup is a very easy task. As above, we have successfully achieved this on CentOS 7 and Ubuntu 20.04.

If you have any queries related to Install PHP on CentOS 7 and Ubuntu or any issues, let us know in the comments.