Install PHP with Apache and Nginx on Debian 12

In this guide, we want to teach you to Install PHP with Apache and Nginx Modules on Debian 12 Bookworm. PHP is an open-source, server-side programming language that you can use to create websites, applications, and more. Debian 12 ships with the latest release of PHP, PHP 8.2 and you can follow this guide to install it on your server using Apache and Nginx modules.

Install PHP with Apache and Nginx on Debian 12 Bookworm

To complete this guide, you must have access to your server as a non-root user with sudo privileges. To do this, you can visit this guide on Initial Server Setup with Debian 12 Bookworm.

Now follow the steps below to complete the PHP installation on Debian 12.

Step 1 – Update and Upgrade Debian 12 APT Repository

First of all, you should run the system update and upgrade by using the following commands:

# sudo apt update
# sudo apt upgrade -y

Step 2 – Install PHP with Apache Module on Debian 12

As we said, Debian 12 ships with PHP 8.2. To install PHP with the Apache module, you can run the following command in your terminal:

sudo apt install php libapache2-mod-php8.2 -y

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.2-fpm libapache2-mod-fcgid -y

Enable PHP-FPM for Apache on Debian 12

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

# sudo a2enmod proxy_fcgi setenvif 
# sudo a2enconf php8.2-fpm

Then, restart Apache again:

sudo systemctl restart apache2

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

sudo systemctl status php8.2-fpm
Output
● php8.2-fpm.service - The PHP 8.2 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.2-fpm.service; enabled; preset: e>
     Active: active (running) since Tue 2023-06-20 06:58:53 EDT; 38s ago
       Docs: man:php-fpm8.2(8)
    Process: 8349 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run>
   Main PID: 8346 (php-fpm8.2)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
      Tasks: 3 (limit: 4653)
     Memory: 9.3M
        CPU: 94ms
     CGroup: /system.slice/php8.2-fpm.service
...

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

php --version
Output
PHP 8.2.7 (cli) (built: Jun  9 2023 19:37:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies

Step 3 – Install PHP with Nginx Module on Debian 12

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

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

sudo apt install php php8.2-fpm php8.2-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.2-fpm
Output
● php8.2-fpm.service - The PHP 8.2 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.2-fpm.service; enabled; preset: en>
     Active: active (running) since Tue 2023-06-20 07:02:34 EDT; 4s ago
       Docs: man:php-fpm8.2(8)
    Process: 12188 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run>
   Main PID: 12185 (php-fpm8.2)
     Status: "Ready to handle connections"
      Tasks: 3 (limit: 4653)
     Memory: 9.3M
        CPU: 78ms
     CGroup: /system.slice/php8.2-fpm.service
...

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

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.2-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 is installed on your Debian 12 by checking its version:

php --version
Output
PHP 8.2.7 (cli) (built: Jun  9 2023 19:37:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies

For more information, you can visit the PHP Documentation page.

Conclusion

PHP is a widely-used general-purpose language that can be embedded into HTML. At this point, you have learned to Install PHP on Debian 12 Bookworm with Apache and Nginx Modules. Hope you enjoy it. Also, you may be interested in these articles:

How To Install Java with Apt on Debian 12 Bookworm

How To Install Grafana on Debian 12 Bookworm

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!