Installing and Configuring NGINX and PHP on Debian 12: A Step-by-Step Guide

Are you setting up a web server or migrating to Debian 12 (Bookworm) and looking to run PHP applications? NGINX, coupled with PHP-FPM, offers a robust, high-performance platform for delivering web content. This guide walks you through installing and configuring NGINX and PHP on Debian 12, ensuring your web server is secure, efficient, and ready to serve your web applications.

Why NGINX with PHP-FPM?

NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. Together, they make a powerful combination for hosting PHP applications.

Step 1: Update Your System

Before we start, it’s crucial to update your Debian system to ensure all your software packages are up to date. This can help avoid security vulnerabilities and compatibility issues.

sudo apt update
sudo apt upgrade

Step 2: Install NGINX

NGINX is available in the Debian repository, making its installation straightforward with the apt package manager.

sudo apt install nginx

Once installed, enable and start the NGINX service:

sudo systemctl enable nginx sudo systemctl start nginx

Step 3: Install PHP

Debian 12 comes with PHP 8.1, which includes many new features and performance improvements over previous versions. Install PHP along with PHP-FPM and the MySQL extension (if you’re using MySQL):

sudo apt install php-fpm php-mysql

You can install additional PHP extensions based on your requirements by appending their names to the install command, like php-xml, php-gd, and others.

Step 4: Configure NGINX to Use PHP-FPM

After installing PHP-FPM, you must configure NGINX to use it for processing PHP files. This involves editing or creating NGINX server block files, typically located in /etc/nginx/sites-available/.

Here’s a basic configuration snippet to process PHP files in NGINX:

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

This configuration tells NGINX to process files ending in .php using PHP-FPM.

After making changes, always check the NGINX configuration for syntax errors and reload NGINX:

sudo nginx -t sudo systemctl reload nginx

Step 5: Test PHP Processing

To ensure everything is set up correctly, create a info.php file in your web root (usually /var/www/html/) with the following content:

<?php phpinfo(); ?>

Access this file through your browser by navigating to http://your_server_ip/info.php. You should see a page displaying your PHP configuration details.

Do You Need to Restart PHP-FPM?

Yes, after modifying PHP-FPM’s configuration, it’s important to restart the service to apply the changes:

sudo systemctl restart php8.2-fpm

Conclusion

By following these steps, you’ve successfully installed and configured NGINX and PHP on Debian 12. This setup is optimized for performance and scalability, providing a solid foundation for your web applications. Remember, this guide covers a basic setup; depending on your specific needs, you might need to adjust firewall settings, install more PHP extensions, or configure additional NGINX server blocks. Happy hosting!

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply