How to Install PHP 8.1 on Debian 11 Bullseye

PHP 8.1 is a significant update of the PHP language that will be “officially” released on November 25, 2021. This is a standard upgrade going forward from the existing PHP 8.0 release with the new PHP 8.1 is bringing enums, fibers, never return type, final class constants, intersection types, read-only properties amongst the long list of new features and changes.

In the following tutorial, you will learn how to import the Ondřej Surý Repository and install PHP 8.1 on your Debian 11 Bullseye system.

Prerequisites

  • Recommended OS: Debian 11 Bullseye
  • User account: A user account with sudo or root access.

Update Operating System

Update your Debian operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade -y

The tutorial will be using the sudo command and assuming you have sudo status.

To verify sudo status on your account:

sudo whoami

Example output showing sudo status:

[joshua@debian~]$ sudo whoami
root

To set up an existing or new sudo account, visit our tutorial on Adding a User to Sudoers on Debian.

To use the root account, use the following command with the root password to log in.

su

Install Required Dependencies

You will need to have the following packages installed for this tutorial. Execute the following command to install:

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

Note, if unsure, run the command regardless; it will not harm your system.

Advertisement

Import Ondřej Surý PHP Repository

Import & Install GPG key:

The first step is to import and install the GPG key before adding the repository.

In your terminal, use the following command.

sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

Import & Install Repository:

With the GPG key sorted, it is time to add the Ondřej Surý repository as follows:

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

Next, refresh your APT repository list to reflect the changes.

sudo apt update

After running the update command, you may notice some packages require updating, make sure to do this before continuing.

sudo apt ugprade

Install PHP 8.1 with Apache Option

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

Install Apache Module

To install PHP 8 as an Apache module, enter the following command.

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

Example output:

How to Install PHP 8.1 on Debian 11 Bullseye

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

sudo systemctl restart apache2

Install Apache with PHP-FPM

PHP-FPM (an acronym of FastCGI Process Manager) is a hugely popular alternative PHP (Hypertext Processor) FastCGI implementation.

To install PHP-FPM with the following commands.

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

Lastly, restart Apache.

sudo systemctl restart apache2

Verify that PHP-FPM is working:

sudo systemctl status php8.1-fpm

Example output:

How to Install PHP 8.1 on Debian 11 Bullseye

As a reminder to see what version of PHP 8.1 is installed on your system, use the following command.

php --version

Example output:

How to Install PHP 8.1 on Debian 11 Bullseye
Advertisement

Install PHP 8.1 with Nginx Option

Nginx does not contain native PHP processing like some other web servers like Apache. You will need to install PHP-FPM “fastCGI process manager” to handle the PHP files.

First, check for updates on your system and install PHP-FPM, natively installing the PHP packages required.

In your terminal, use the following command to install PHP 8.1 and PHP 8.1-FPM.

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

Once installed, the PHP-FPM service is automatically started, and you can check the status to make sure it’s running ok.

sudo systemctl status php8.1-fpm

Example output:

How to Install PHP 8.1 on Debian 11 Bullseye

You will need to edit your Nginx server block and add the example below for Nginx to process the PHP files.

Below, example for all server blocks that process PHP files that need the location ~ .php$ added.

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

Test Nginx to make sure you have no errors with the adjustments made with the code above; enter the following.

sudo nginx -t

Example output:

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

Restart Nginx service for installation to be complete.

sudo systemctl restart nginx

As a reminder to see what version of PHP 8.1 is installed on your system, use the following command.

php --version

Example output:

How to Install PHP 8.1 on Debian 11 Bullseye

Comments and Conclusion

In the tutorial, you have learned how to install PHP 8.1 and configure how to use it with Apache and Nginx. PHP 8.1 is exciting. However, at the current moment, it is still coming out of beta and not considered stable, such as 8.0 or the old stable 7.4, so beware you may find that many of your favorite software like WordPress or Plugins/Themes for CMS software may conflict until developers can update.

Do some research, prepare, and have PHP 7.4 or 8.0 installed and ready to replace if anything goes wrong when making the switch. The stable versions such as 8.0 are still actively developed, and packages are pushed simultaneously along with the 8.1 packages.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x