Install php 8.1 debian 10

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 10 Buster desktop or server.

Table of Contents

1

  • Update Debian System
  • Install Required Dependencies
  • Import Ondřej Surý PHP Repository
  • Install PHP 8.1 with Apache Option
    • Install Apache Module
    • Install Apache with PHP-FPM
  • Install PHP 8.1 with Nginx Option
  • Comments and Conclusion

Update Debian System

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

sudo apt update && sudo apt upgrade -y

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 wget curl lsb-release -y

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

Import Ondřej Surý PHP Repository

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

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

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 upgrade

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 -y

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 -y

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 should be automatically started, if not run the following command.

sudo systemctl enable php8.1-fpm --now

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

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.