Easy Guide: Set Up a Web Server with Nginx, PHP 8.2, and MariaDB on Debian 12

Automated English translation of the original Spanish article. Code examples keep their original identifiers.

Want to set up your own web server on Debian 12 with Nginx, PHP, and MariaDB? In this guide, I will show you how to do it step by step in a simple, practical way. It is designed for anyone starting out with Linux servers who needs a basic environment to test ideas or experiment before taking the leap to something more serious, like a production server. I will also share a few tips to enhance performance and security. Let’s get to work!

Key Concepts

  • Web Server (Nginx): Listens for browser requests and serves web pages, whether static files or dynamic content.
  • Database (MariaDB): Stores and organizes data, such as application users or blog posts.
  • PHP-FPM (PHP 8.2): The process manager that enables Nginx to execute PHP code. We will use version 8.2, which ships natively with Debian 12, to build pages tailored to your needs.
  • HTTP and HTTPS: HTTP transfers data across the web. HTTPS does the same, but encrypted, ensuring no one can intercept your data.

With these concepts clear, you’re ready to proceed!

Step 1: Keep Your System Up to Date

First, make sure your Debian 12 installation is completely updated. Open your terminal and run:

sudo apt update
sudo apt upgrade -y

update refreshes the package index, while upgrade installs the latest versions of your currently installed packages. The -y flag automatically answers “yes” to prompts to make the process faster.

Step 2: Install Nginx, Your Web Server

Nginx is lightweight, fast, and ideal to get started. Install it by running:

sudo apt install nginx -y

Once the installation completes, verify that it works: open your web browser and navigate to your server’s IP address (your VPS or local machine IP). If you see “Welcome to Nginx!”, you are on the right track!

Step 3: Add MariaDB for Your Data

MariaDB is an excellent choice for storing your application data. Install it with:

sudo apt install mariadb-server -y

Next, secure the installation by running:

sudo mysql_secure_installation

It will prompt you to set a root password, remove anonymous users, disable remote root login, and drop the test database. Answer “yes” (Y) to almost every prompt; this is a quick and essential step.

Create a Database and a User

Prepare a database for your applications. Log in to MariaDB:

sudo mariadb

Then execute these SQL commands (customize the names and passwords as needed):

CREATE DATABASE mi_base_de_datos;
CREATE USER 'mi_usuario'@'localhost' IDENTIFIED BY 'mi_contraseña';
GRANT ALL PRIVILEGES ON mi_base_de_datos.* TO 'mi_usuario'@'localhost';
FLUSH PRIVILEGES;
EXIT;

This creates a database (mi_base_de_datos) and a dedicated user (mi_usuario) with access granted only to that database. Ready to use!

Step 4: Connect Nginx with PHP 8.2

To handle dynamic pages, install PHP-FPM 8.2 and the MariaDB extension:

sudo apt install php8.2-fpm php8.2-mysql -y

Now, configure Nginx to process PHP files. Open the default configuration file:

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

Locate (or add) the location ~ \.php$ block and ensure it looks like this, pointing to the PHP 8.2 socket:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    # Asegúrate que esta línea apunta al socket de tu versión de PHP
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    # Añade estas líneas si no existen dentro del bloque location
    # include fastcgi_params;
    # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Note: Sometimes the include fastcgi_params; and fastcgi_param SCRIPT_FILENAME... directives are required if they are not already included in snippets/fastcgi-php.conf.

Save the file (Ctrl+O, Enter, Ctrl+X in Nano) and restart both services:

sudo systemctl restart nginx
sudo systemctl restart php8.2-fpm

This configures Nginx to forward PHP requests to PHP-FPM 8.2 via a Unix domain socket, an internal communication channel connecting them.

Step 5: Test That Everything Works

Let’s verify that everything is running properly. Create a test file:

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

Paste the following code:

<?php
phpinfo();
?>

Save and exit. In your browser, visit http://<your-server-ip>/info.php. If a page appears displaying PHP configuration details (look for “PHP Version 8.2.x”), everything is working perfectly!

Warning: This file exposes sensitive server configuration details. Remove it with sudo rm /var/www/html/info.php once you finish testing, especially if this server will be exposed publicly.

Step 6: Secure Your Server

Your server is ready, but keep it protected with these recommendations:

  • Firewall: Use ufw to restrict incoming traffic. For example, allow only SSH (port 22), HTTP (port 80), and HTTPS (port 443):
sudo ufw allow 22
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
  • Updates: Regularly run sudo apt update && sudo apt upgrade -y to keep packages patched.
  • Passwords: Use strong, unique passwords for root, regular users, and databases.
  • HTTPS: Implementing encryption is essential (explained below).
  • Fail2ban: Install it using sudo apt install fail2ban -y to mitigate brute-force attacks. Its default setup provides immediate protection.

Virtual Hosts in Nginx

Want to host multiple websites (like a blog and an online store) on the same machine? Use virtual hosts (server blocks) to isolate them. Create a file in /etc/nginx/sites-available/ (for example, mysite.conf), configure your server block (including the PHP block if needed), and enable it with a symbolic link:

sudo ln -s /etc/nginx/sites-available/misitio.conf /etc/nginx/sites-enabled/

Verify that the configuration syntax is valid with sudo nginx -t and reload Nginx:

sudo systemctl restart nginx

Additional Tips to Optimize Your Server

Tune PHP-FPM

Optimize PHP-FPM for improved performance. Edit /etc/php/8.2/fpm/pool.d/www.conf and adjust the process manager settings:

  • pm = dynamic (or static for a fixed process count, or ondemand to conserve RAM).
  • pm.max_children = 5 (Note: This is a low default, suitable for small VPS instances with limited RAM. Increase it according to your available memory if you expect more traffic).
  • pm.start_servers = 2 (used only with dynamic)
  • pm.min_spare_servers = 1 (used only with dynamic)
  • pm.max_spare_servers = 3 (used only with dynamic)

Restart PHP-FPM to apply the changes:

sudo systemctl restart php8.2-fpm

Add HTTPS with Let’s Encrypt

To secure your site with an SSL/TLS certificate, install Certbot and its Nginx plugin:

sudo apt install certbot python3-certbot-nginx -y

Prerequisite: You need a registered domain name (such as yourdomain.com) with a DNS A record pointing to your server’s public IP address.

Obtain and install a certificate (replace your_domain with your actual domain):

sudo certbot --nginx -d tu_dominio

Follow the interactive prompts (choose the option to redirect HTTP traffic to HTTPS). Test the automatic renewal process:

sudo certbot renew --dry-run

If the dry run succeeds, your website traffic will be encrypted and secure.

And You’re Done!

Setting up a LEMP stack with Nginx, PHP 8.2, and MariaDB on Debian 12 is straightforward when approached step by step. You now have a solid environment to run PHP applications or learn how Linux server stacks operate. Apply the necessary hardening and tuning measures, and you will have a dependable foundation for your experiments or future projects.