LINUXMAKER, OpenSource, Tutorials

Install PHP (8.2, 7.x and 5.6) on Debian Linux 11

PHP, also referred to as "HyperText Processor", is the open source programming language used for web application development. It is a scripting language mainly used for frontend with HTML. It can be used to build e-commerce websites, manage databases and perform session monitoring.
It is available for all operating systems. The latest version of PHP is version 8 and this page discusses how to install PHP on Linux system Debian 11 (Bullseye).

Requirements

First, all the packages of the system are updated using the command mentioned below:

# apt update

Subsequently, after updating the packages, the dependencies required for the command below are now installed:

# apt install software-properties-common ca-certificates lsb-release apt-transport-https

 

1. Setting up the SURY repository

In the next step, the SURY repository is integrated into the system. SURY is a third-party Debian-based PHP repository that bundles the PHP software. This requires the following command to add the SURY repository

# sh -c 'echo "deb packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

In addition, the GPG key for the repository is required to verify it

wget -qO - packages.sury.org/php/apt.gpg | apt-key add -

2. Installation of the desired PHP version

After adding a new repository to the source list, the packages must be synchronized again:

# apt update

In the SURY repository PHP versions 8.2, 8.1, 8.0, 7.4, 7.3, 7.2. 7.1, 7.0 and PHP 5.6 included. The current latest stable version of PHP is 8.0, but many websites are still running on PHP 7. Or many PHP applications require older PHP versions due to lack of maintenance. For this purpose, any of the required PHP versions can be installed on the system. Depending on which versions - even several - the commands look like this.

# apt -y install php8.2

or

# apt -y install php7.1

or in the case of several versions

# apt -y install php5.6 php7.0 php7.2 php7.3 php7.4

Basically, you take whatever opportunity you need. For example, web space providers will need to offer multiple versions of PHP. The apt option "-y" ensures that the packages and additional dependencies are installed without asking. You can also omit this, but you will then be asked to confirm the installation.

3. Install PHP extensions

PHP alone would not be very functional without the numerous extensions. These can be installed in the same way.

apt -y install php7.1-mysql php7.1-zip php7.1-cli php7.1-mbstring php7.1-xml php7.1-common php7.1-curl

Of course, you have to do this in the same way for all installed PHP versions.

4. Changing PHP versions

This and the following step are the really interesting ones, because you want to be able to switch between the PHP versions without having to constantly reinstall. You can achieve that this way.

# update-alternatives --config php

There are 6 choices for the alternative php (which provide /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
   0 /usr/bin/php8.2  75  auto mode
   1 /usr/bin/php5.6  56  manual mode
   2 /usr/bin/php7.0  70  manual mode
*  3 /usr/bin/php7.1  71  manual mode
   4 /usr/bin/php7.2  72  manual mode
   5 /usr/bin/php7.3  73  manual mode
   6 /usr/bin/php7.4  74  manual mode
   7 /usr/bin/php8.2  75  manual mode
Press Enter to keep the current choice[*],
or enter the selection number:

The procedure is self-explanatory. The then freshly set PHP version can then be displayed as follows.

# php -v

PHP 7.1.33-52+0~20230409.76+debian11~1.gbp03e0c6 (cli) (built: Apr  9 2023 16:56:50) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
   with Zend OPcache v7.1.33-52+0~20230409.76+debian11~1.gbp03e0c6, Copyright (c) 1999-2018, by Zend Technologies

5. Switch between multiple PHP versions

Next to point 4, this is one of the most important steps. Both can also be recorded in a script so that you only have to execute one command instead of all the commands presented here.

So in order to switch from the discontinued PHP 7.1 to PHP 5.6, the command "update-alternatives --config php" must first be carried out as presented in order to switch to the standard PHP version.
So that the Apache server can also use this PHP version, these commands are important in this order.

# a2dismod php7.1
# a2enmod php5.6
# systemctl restart apache2

If you also want to switch to the Command Line Interface (CLI), then the following steps are still required.

# update-alternatives --set php /usr/bin/php5.6
# update-alternatives --set phar /usr/bin/phar5.6
# update-alternatives --set phar.phar /usr/bin/phar.phar5.6
# update-alternatives --set phpize /usr/bin/phpize5.6
# update-alternatives --set php-config /usr/bin/php-config5.6

Note - The phpize5.6 and php-config5.6 commands are available in the php5.6-dev package.

This is the same for all other PHP versions.