How To Set up PHP 8.1 on Debian 11

In this article, we want to teach you How To Set up PHP 8.1 on Debian 11.

PHP is a recursive acronym for “PHP: Hypertext Preprocessor”. It is a server-side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites.

In this guide, you will learn to install the latest release of PHP on Debian 11.

How To Set up PHP 8.1 on Debian 11

Before you start to set up PHP on Debian 11, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Debian 11.

Now follow the steps below to complete this guide.

Set up Dependencies on Debian 11

First, you need to update and upgrade your local package index with the following command:

sudo apt update && sudo apt upgrade -y

Then, you need to install the required packages on your server with the command below:

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

At this point, you need to add the Sury PHP repository on Debian 11.

This is a home for packaging various software into Debian and Ubuntu. The most notable package under DEB.SURY.ORG is the PHP packaging.

To add Sury PHP repo, you can use the following command:

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

Update and upgrade your APT repository list again:

sudo apt update && sudo apt upgrade

Now let’s see how to install PHP 8.1 with Apache and Nginx option on Debian 11.

Install PHP 8.1 on Debian 11

If you run an Apache HTTP server, you can run PHP as an Apache module or PHP-FPM.

Set up PHP 8.1 with Apache module

To install PHP 8.1 as an Apache module, run the following command on Debian 11:

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

When your installation is completed, restart Apache to apply the changes:

sudo systemctl restart apache2

To install PHP-FPM, you can use the following command:

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

Note: By default, PHP-FPM is not enabled for Apache. You must enable it by the following command:

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

Then, restart Apache again:

sudo systemctl restart apache2

Verify your PHP-FPM service is active and running on your server with the command below:

sudo systemctl status php8.1-fpm
Output
php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor pr>
Active: active (running) since Sun 2022-03-13 03:43:44 EDT; 2min 4s ago
Docs: man:php-fpm8.1(8)
Process: 22035 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /ru>
Main PID: 22032 (php-fpm8.1)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
Tasks: 3 (limit: 2340)
Memory: 8.9M
...

Also, you can verify that PHP 8.1 is installed on your Debian 11 by checking its version:

php --version
Output
PHP 8.1.3 (cli) (built: Feb 23 2022 16:07:16) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.3, Copyright (c) Zend Technologies
with Zend OPcache v8.1.3, Copyright (c), by Zend Technologies

Set up PHP 8.1 with Nginx module

As you know, Nginx does not contain native PHP processing like some other web servers like Apache. You will need to install PHP-FPM to handle the PHP files.

To install PHP 8.1 and PHP 8.1-FPM, run the command below:

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

PHP-FPM will start automatically when your installation is completed.

To verify that it is active and running on your server, run the command below:

sudo systemctl status php8.1-fpm
Output
php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor pr>
Active: active (running) since Sun 2022-03-13 03:43:44 EDT; 2min 4s ago
Docs: man:php-fpm8.1(8)
Process: 22035 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /ru>
Main PID: 22032 (php-fpm8.1)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
Tasks: 3 (limit: 2340)
...

At this point, you need to edit your Nginx server block and add the example below for Nginx to process the PHP files on Debian 11.

This is an example for all server blocks that process PHP files that need the location ~ .php$ added.

Output
server {
# … some other code
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}

Now you can check that you have no error syntax for Nginx:

sudo nginx -t
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Also, you can verify that PHP 8.1 is installed on your Debian 11 by checking its version:

php --version
Output
PHP 8.1.3 (cli) (built: Feb 23 2022 16:07:16) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.3, Copyright (c) Zend Technologies
with Zend OPcache v8.1.3, Copyright (c), by Zend Technologies

Conclusion

At this point, you learn to Set up PHP 8.1 with Apache and Nginx options on Debian 11.

Hope you enjoy it.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular