Categories
Server

Installing PHP 8.2, Nginx, and PHP-FPM on Debian 12: A Step-by-Step Guide

This guide will walk you through the installation and configuration of PHP 8.2, Nginx, and PHP-FPM on Debian 12. This setup is ideal for those running web applications that require modern PHP and the performance advantages of Nginx with PHP-FPM (FastCGI Process Manager).

Prerequisites

  • A server running Debian 12.
  • Root or a user with sudo privileges.
  • Basic familiarity with the command line.

Step 1: Update and Upgrade Your System

Before starting the installation process, it is essential to update your system’s package list and upgrade any outdated packages.

sudo apt update && sudo apt upgrade -y

This ensures your system is running the latest security patches and software updates.

Step 2: Install Nginx

Nginx is a powerful, high-performance web server and reverse proxy. To install Nginx on Debian 12, run the following command:

sudo apt install nginx -y

Once the installation is complete, enable and start the Nginx service:

sudo systemctl enable nginx
sudo systemctl start nginx

To verify that Nginx is running, you can check the service status:

sudo systemctl status nginx

Additionally, you can test Nginx by visiting your server’s IP address in a browser. You should see the default Nginx welcome page.


Step 3: Install PHP 8.2

Debian 12 does not have PHP 8.2 in its default repositories, so you’ll need to add an external repository to install it. The SURY repository is a popular and trusted source for the latest PHP versions.

First, add the necessary dependencies:

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

Then, import the SURY repository’s GPG key:

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

Now, add the PHP repository:

sudo add-apt-repository "deb https://packages.sury.org/php/ $(lsb_release -sc) main"

Update the package list to include the newly added repository:

sudo apt update

Finally, install PHP 8.2 along with commonly used extensions:

sudo apt install php8.2 php8.2-fpm php8.2-cli php8.2-mbstring php8.2-xml php8.2-mysql php8.2-curl php8.2-zip -y

Step 4: Configure PHP-FPM

PHP-FPM (FastCGI Process Manager) is responsible for handling PHP requests from Nginx. After installing PHP-FPM, you need to ensure that the service is enabled and running:

sudo systemctl enable php8.2-fpm
sudo systemctl start php8.2-fpm

To verify that PHP-FPM is running:

sudo systemctl status php8.2-fpm

Step 5: Configure Nginx to Use PHP-FPM

Now that PHP-FPM is installed and running, you need to configure Nginx to handle PHP requests via PHP-FPM.

First, edit the Nginx server block configuration file. For simplicity, we’ll modify the default server block, but you can create custom server blocks for specific websites or applications.

Open the default Nginx configuration file:

sudo nano /etc/nginx/sites-available/default

Locate the location ~ \.php$ { block and modify it as follows:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Make sure to adjust the root directive (/var/www/html) if your website files are located elsewhere. The fastcgi_pass directive should point to the correct PHP-FPM socket.

Save and close the file. Then, test the Nginx configuration for syntax errors:

sudo nginx -t

If no errors are reported, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 6: Verify PHP-FPM with Nginx

To verify that PHP is correctly configured with Nginx and PHP-FPM, create a simple PHP info file.

sudo nano /var/www/html/info.php

Add the following PHP code:

<?php
phpinfo();
?>

Save and close the file.

Now, visit http://your_server_ip/info.php in a browser. If everything is set up correctly, you will see a page displaying detailed PHP information.


Step 7: Secure Your Installation

After confirming that PHP is working, it’s important to remove or restrict access to the info.php file as it exposes detailed information about your server configuration:

sudo rm /var/www/html/info.php

Additionally, consider further securing your Nginx and PHP-FPM setup by:

  • Setting up firewalls (e.g., ufw) to allow only necessary traffic.
  • Using SSL certificates (e.g., via Let’s Encrypt) to enable HTTPS for your websites.
  • Configuring fail2ban to protect against brute-force attacks.

Conclusion

You’ve successfully installed PHP 8.2, Nginx, and PHP-FPM on Debian 12. This setup provides a robust foundation for running PHP-based web applications with modern, efficient software. By following this guide, you’ve ensured that your web server is configured with the latest PHP version and optimized for performance with Nginx and PHP-FPM.

You can now start deploying your applications or further fine-tune the configuration to meet your specific requirements.