Monday, October 3, 2022

How To Install PHP 7.4 on Debian 11

In this guide, we intend to teach you How To Install PHP 7.4 on Debian 11.

PHP is an open-source server-side scripting language that many devs use for web development. It is also a general-purpose language that you can use to make lots of projects, including Graphical User Interfaces (GUIs).

Steps To Install PHP 7.4 on Debian 11

Before you start to set up PHP 7.4 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.

Install PHP 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 the 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 7.4 with Apache and Nginx options.

Installing PHP 7.4 on Debian 11

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

Installing PHP 7.4 with Apache module

To install PHP 7.4 as an Apache module, run the following command:

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

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

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 php7.4-fpm
Output
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor pr>
     Active: active (running) since Thu 2022-09-01 04:12:35 EDT; 1min 11s ago
       Docs: man:php-fpm7.4(8)
    Process: 28557 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /ru>
   Main PID: 28554 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
      Tasks: 3 (limit: 2340)
     Memory: 8.6M
        CPU: 74ms
     CGroup: /system.slice/php7.4-fpm.service
             ├─28554 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─28555 php-fpm: pool www
             └─28556 php-fpm: pool www
...

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

php --version
Output
PHP 7.4.30 (cli) (built: Jun 27 2022 08:14:10) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies

Installing PHP 7.4 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 704 and PHP 7.4-FPM, run the command below:

sudo apt install php7.4 php7.4-fpm php7.4-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 php7.4-fpm
Output
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor pr>
     Active: active (running) since Thu 2022-09-01 04:17:34 EDT; 25s ago
       Docs: man:php-fpm7.4(8)
    Process: 33513 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /ru>
   Main PID: 33510 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
      Tasks: 3 (limit: 2340)
     Memory: 8.6M
        CPU: 64ms
     CGroup: /system.slice/php7.4-fpm.service
             ├─33510 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─33511 php-fpm: pool www
             └─33512 php-fpm: pool www
...

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 {
 location ~ .php$ {
   include snippets/fastcgi-php.conf;
   fastcgi_pass unix:/run/php/php7.4-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 7.4 is installed on your sever by checking its version:

php --version
Output
PHP 7.4.30 (cli) (built: Jun 27 2022 08:14:10) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies

Conclusion

At this point, you learn to Install PHP 7.4 on Debian 11 with Apache and Nginx modules.

Hope you enjoy it.

You may be interested in these articles:

Install Gradle Build Tool on Debian 11

How To Install MySQL 8.0 on Debian 11

Install and Use 7-Zip on Debian 11

How To Set up PiVPN on Debian 11

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles