How to Install PHP 8.2, 8.1, 8.0, or 7.4 on Debian 11 or 10


PHP is a programming language executed on the server and is mainly used for creating websites and web applications. It can be easily incorporated into a Debian server to create dynamic websites and content management systems (CMS). PHP is also a popular language for web developers as it can be integrated with various databases and frameworks to create powerful, dynamic web applications. Some key features of PHP include:

  • Server-side execution: The PHP code runs on the server, and the output is sent to the client’s browser as HTML.
  • Cross-platform compatibility: PHP is compatible with multiple operating systems, including Windows, Linux, and macOS.
  • Integration with databases: PHP can connect to and interact with databases such as MySQL, PostgreSQL, and Oracle.
  • Built-in functions: PHP includes many built-in functions that can be used for tasks such as string manipulation, file manipulation, and more.
  • Extensive community: Vast and active community of developers and users who actively contribute to developing and improving PHP and its ecosystem.
  • Open-source: PHP is open-source software and can be freely used, modified, and distributed.

In this tutorial, you will learn how to add the Ondřej Surý Repository and install the latest version of PHP 8.2, 8.1, 8.0, or 7.4 on your Debian 11 Bullseye system using the command line terminal.

Step 1: Update Debian System Packages

Ensure all existing packages on your Debian operating system are up to date by updating them with the following command.

sudo apt update

Before continuing, you can preview the updates that will be made to your system by using the command for listing upgrades.

apt list --upgradable

If updates are available, use the given command to initiate the upgrade process.

sudo apt upgrade

Step 2: Import Ondřej Surý PHP Repository

Install Required Dependencies

To follow this tutorial, specific packages need to be installed. Please run the following command to install them.

sudo apt-get install ca-certificates apt-transport-https software-properties-common curl lsb-release -y

The following step is to import and install the GPG key and repository using an automated script, which can be done by running the curl command. In the terminal, execute the following command.

curl -sSL https://packages.sury.org/php/README.txt | sudo bash -x

Refresh your APT repository list to reflect the changes made in the previous step.

sudo apt update

After running the update command, specific packages may need updating, be sure to update them before proceeding.

sudo apt upgrade

Step 2: Install PHP

Option 1: Install PHP with Apache Option

You can run PHP as an Apache module or use PHP-FPM with the Apache HTTP server. To make it easier to manage and keep clean, it is best to install only the version of PHP that you require. You can execute the following command to install PHP as an Apache module.

To install PHP 8.2 module, run the following command:

sudo apt install php8.2 libapache2-mod-php8.2

To install PHP 8.1 module, run the following command:

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

To install PHP 8.0 module, run the following command:

sudo apt install php8.0 libapache2-mod-php8.0

To install PHP 7.4 module, run the following command:

sudo apt install php7.4 libapache2-mod-php7.4

Once installation is complete, restart your Apache server for the new PHP module to be loaded.

sudo systemctl restart apache2

Option 2: Install Apache with PHP-FPM

PHP-FPM (FastCGI Process Manager) is a widely used alternative to the standard PHP (Hypertext Processor) FastCGI implementation.

Select the command to correspond to the version of PHP, PHP-FPM with Apache you want to install.

See also
How to Install Linux Kernel 6.1 on Debian 11 or 10

To install PHP, and PHP-FPM 8.2 with Apache, use the following command.

sudo apt install php8.2-fpm libapache2-mod-fcgid

To install PHP, and PHP-FPM 8.1 with Apache, use the following command.

sudo apt install php8.1-fpm libapache2-mod-fcgid

To install PHP, and PHP-FPM 8.0 with Apache, use the following command.

sudo apt install php8.0-fpm libapache2-mod-fcgid

To install PHP, and PHP-FPM 7.4 with Apache, use the following command.

sudo apt install php7.4-fpm libapache2-mod-fcgid

Please note that PHP-FPM is not enabled for Apache by default. You must enable it using the following command.

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php{version}-fpm

It’s important to note that you must replace the {version} section in the above command with your desired PHP version. For example, if you want to use PHP 8.2, the command should be as follows.

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php8.2-fpm

Finally, restart the Apache web server.

sudo systemctl restart apache2

Option 3: Install PHP support for Nginx

Install PHP & PHP-FPM

Unlike other web servers like Apache, Nginx does not have built-in support for processing PHP files. To handle PHP files with Nginx, you must install PHP-FPM (FastCGI Process Manager). Use the following terminal command to install PHP and PHP-FPM for Nginx.

To install PHP 8.2 for Nginx support, use the following command.

sudo apt install php8.2 php8.2-fpm php8.2-cli -y

To install PHP 8.1 for Nginx support, use the following command.

sudo apt install php8.1 php8.1-fpm php8.1-cli -y

To install PHP 8.0 for Nginx support, use the following command.

sudo apt install php8.0 php8.0-fpm php8.0-cli -y

To install PHP 7.4 for Nginx support, use the following command.

sudo apt install php7.4 php7.4-fpm php7.4-cli -y

After installation, the PHP-FPM service will automatically start running. If this fails to occur, use the following command.

sudo systemctl start php{version}-fpm

Configure Nginx Server Block for PHP-FPM

For Nginx to process PHP files, you will need to edit your Nginx server block and add the following example. This should be added to all server blocks that handle PHP files, specifically the “location ~ .php$.”

location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php{version}-fpm.sock;
}

Replace {version} with your desired PHP version.

To ensure that there are no errors with the adjustments made to your Nginx configuration, you can test it using the following command.

sudo nginx -t

It will check the syntax of the configuration files and tell you if there are any issues.

Example output if successful:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To finalize the installation, it is necessary to restart the Nginx service.

sudo systemctl restart nginx

As a reminder, you can use the following command to check the PHP version currently installed on your system.

php --version

Example output with PHP 8.2 installed:

Step 3: Create a PHP Test File

Here are the steps to create a PHP test file on a Debian Linux terminal.

Use the following command to create a new file called “test.php” in the desired directory.

touch test.php

Use the following command to open the file in a text editor, such as nano:

nano test.php

In the text editor, enter the following PHP code.

<?php
   echo "PHP is working!";
?>

Save the file by pressing CTRL + O then exit the editor by pressing CTRL + X.

Run the test file by using the following command.

php test.php

You should see the message “PHP is working!” printed on the terminal, which indicates that PHP is working correctly on your system.

Note: You can use any other editor like vim, gedit, etc.

Conclusion

In conclusion, installing PHP on a Debian system is a relatively straightforward process. By following the steps outlined in this guide, you should be able to install and configure PHP on your Debian system successfully. Additionally, you should also be able to verify that PHP is working correctly by running a simple test script. With PHP installed, you can run a wide range of dynamic web applications and scripts on your Debian system.

4 thoughts on “How to Install PHP 8.2, 8.1, 8.0, or 7.4 on Debian 11 or 10”

Leave a Comment

Share to...