How to Switch between PHP Versions on an Ubuntu 22.04 VPS or Dedicated Server

Introduction

PHP is an open source general-purpose scripting language that is widely used and best suited for developing websites and web applications. It is a server-side scripting language that can be embedded into HTML.

Currently, there are three supported versions of PHP i.e  Versions 7.0, 7.2, 7.8, 8.0 and 8.1. As a modern-day web developer who works on different frameworks, packages need to have the multiple versions of PHP installed on your VPS.

During your development, not all your projects may support the latest version (V. 8.1) resulting you to downgrade to a much earlier version. In this tutorial, we will illustrate how to switch between the different versions of PHP from 8.1 to 7.2 to enable you to run your projects smoothly.

Prerequisites

  • Ubuntu 22.04 operating system
  • Web server (in this demo, we use Apache web server, you can learn how to install on our tutorial – How To Install Apache, MariaDB, and PHP (LAMP stack) in Ubuntu.
  • A non-root user with sudo privileges.
  • Have 2 different versions of PHP installed on your VPS

Steps

Upgrade your system packages

$ sudo apt upgrade
Note
Special Note: This tutorial assumes you have already installed PHP 7.0, 7.2 and 8.1, but in case you do not have any two PHP versions installed on your computer, we will be working with versions 7.2 and 8.1. You can use the following commands  below to install them:
$ sudo apt install software-properties-common apt-transport-https –y

$ sudo add-apt-repository ppa:ondrej/php –y

Update the system repositories:

$ sudo apt update

 

$ sudo apt install php8.1 libapache2-mod-php8.1

$ sudo apt install php7.2

Run the command to check the set default version of PHP

$ php –v

By default, Ubuntu will set the latest, stable version of PHP as the default i.e. version 8.1.To switch to the alternative version (7.2)

$ sudo a2dismod php8.1

$ sudo a2enmod php7.2

$ sudo service apache2 restart

$ sudo update-alternatives --set php /usr/bin/php7.2

You can also use the following command to switch from one Php version to another installed php version:

$ sudo update-alternatives --config php

Enter the choice ‘number’ that you want to set as default php version.

$ php --version
Note
Special Note: If you are using Nginx server, you can restart it by using the following command:
$ sudo systemctl restart nginx

You can now check the current version of PHP again:

$ php -v

You will notice the version has changed from version 8.1 to 7.2. Of course, the opposite can be done by following the steps above and replacing the version of PHP in each step and voila! You are back to your previous version.

This process may seem repetitive and tedious, so to make it easy, you can encapsulate the entire process in a function. For example, we want to switch between 7.2 to 7.0 php version.

To do this, open your .bashrc file with your favorite editor, in this case, we use nano.

$ sudo nano ~/.bashrc

Scroll to the last line and type in the following code:

changephpversion () {

local IS_PHP7=`php -version|grep "PHP 7.2"`

if [[ -z $IS_PHP7 ]] ; then

echo "Switching... Please wait"

sudo a2dismod php7.0;

sudo a2enmod php7.2;

sudo service apache2 restart

sudo update-alternatives --set php /usr/bin/php7.2

else

echo "Switching... Please wait"

sudo a2dismod php7.2;

sudo a2enmod php7.0;

sudo service apache2 restart

sudo update-alternatives --set php /usr/bin/php7.0

fi

}

Exit your editor while saving changes and incorporate the new changes by typing below command:

$ source ~/.bashrc

Now when you type ‘changephpversion’ on your terminal, the function will automatically switch to the alternative version.

Conclusion

You can try out this tutorial with different PHP versions. Very simple indeed! Your query to the best web hosting can end by clicking on this link.

PART ONE: How to Install Zabbix on an Ubuntu 18.04 VPS or Dedicated Server

This tutorial is compiled to help you install Zabbix; a powerful server monitori
4 min read
David Malcom
David Malcom
Author

How to install Zabbix on Ubuntu 18.04 [PART TWO]

This tutorial is compiled to help you install Zabbix; a powerful server monitori
5 min read
David Malcom
David Malcom
Author

How to Install the LEMP (Linux, Nginx, MySQL, PHP) Stack on an Ubuntu 18.04 VPS or Dedicated Server

This tutorial walks you through the process of installing Nginx, MySQL, and PHP
4 min read
Mark Armistead
Mark Armistead
Author

How To Install Apache, MySQL & PHP on an Ubuntu 18.04 VPS or Dedicated Server

This article shows users how to setup PHP, Apache web server and MySQL database
4 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top