How to Install PHP 7.4 on Debian 11 Bullseye

PHP 7.4 is a significant update of the PHP language that was “officially” released on November 28, 2019. This is a standard upgrade from now on from the existing PHP 7.3 release to PHP 7.4, which is the last version in the 7 PHP series that brings in arrow functions for cleaner one-liners, preloading for improved performance, typed properties in classes, improved type variances, spread operator in arrays and much more.

In the following tutorial, you will learn how to install PHP 7.4 on Debian 11 Bullseye by importing the Ondřej Surý repository, the maintainer for PHP on Debian, and installing, upgrading, or removing howto instructions.

Update Debian

Before proceeding, run a system update to ensure all your packages are up-to-date to avoid any conflicts during the installation.

sudo apt update && sudo apt upgrade

Install Required Packages

The following dependencies will need to be installed to install PHP 7.4 successfully. Most of these packages would already be present on your system, but running the command can help ensure they are installed.

sudo apt install software-properties-common apt-transport-https curl -y

Import Ondřej Surý PHP Repository

To begin with, import the PHP repository by Ondrej, who has been a PHP maintainer for Debian for over a decade. This safe repository contains a much more updated version of 7.4 than what Debian contains.

Import the PPA using the following command.

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

Once done, it is good to refresh your APT repositories as the PPA may bring additional upgrades to existing dependencies.

sudo apt update

After importing the PPA and running an update, you should see a few packages that need updating; run an upgrade now.

sudo apt upgrade

Install PHP 7.4 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 7.4 as an Apache module, enter the following command.

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

Once installation is complete, restart your Apache server to load the new PHP module.

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 php7.4-fpm libapache2-mod-fcgid php7.4-cli

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 php7.4-fpm

Lastly, restart Apache.

sudo systemctl restart apache2

Verify that PHP-FPM is working:

systemctl status php7.4-fpm

Example output:

How to Install PHP 7.4 on Debian 11 Bullseye

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

php --version

Example output:

How to Install PHP 7.4 on Debian 11 Bullseye

Install PHP 7.4 with Nginx Option

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

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 7.4 and PHP 7.4-FPM.

sudo apt install php7.4 php7.4-fpm php7.4-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 php7.4-fpm

Example output:

How to Install PHP 7.4 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 is an example of all server blocks that process PHP files that need the location ~ .php$ added.

server {
 location ~ .php$ {
   include snippets/fastcgi-php.conf;
   fastcgi_pass unix:/run/php/php7.4-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 7.4 is installed on your system, use the following command.

php --version

Example output:

How to Install PHP 7.4 on Debian 11 Bullseye

Comments and Conclusion

7.4 is considered old stable, with PHP 8.0 reaching the maturity of being stable; however, for non-developers and users installing PHP to run CMS systems such as WordPress often you will find developers still are updating to the PHP 8.0/8.1 series, so using 7.4 is a solid option and still performs well even though its a generation behind now.

PHP 7.4 end of life will be reached on 28th November 2022, migrating to PHP 8.0.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!