Operating System Debian

How to Set Up Nginx, PHP-FPM and MariaDB on Debian 12

Install and connect Nginx, PHP-FPM and MariaDB on Debian 12 from the distribution packages, with a tested server block and a database for your application.

This guide installs Nginx, PHP-FPM and MariaDB on Debian 12 and connects them into a working stack for PHP applications: Nginx serves the site and hands PHP requests to PHP-FPM over a Unix socket, and MariaDB stores the data. Everything comes from the Debian repositories, so it is kept up to date with apt. Debian 12 ships Nginx 1.22, PHP 8.2 and MariaDB 10.11. The same steps work on Debian 13, which ships PHP 8.4 and MariaDB 11.8; only the PHP version in file paths changes.

Debian 8, which older versions of this guide targeted, is end-of-life and should be replaced rather than patched.

Requirements

  • A server running Debian 12 with a user that can run sudo. The examples use a user called deploy.
  • A domain name pointing at the server's IP address. The examples use example.com.
  • Ports 80 and 443 open if you run a firewall.

Update the system and install the packages

sudo apt update
sudo apt upgrade
sudo apt install nginx mariadb-server php-fpm php-mysql

php-fpm pulls in the PHP 8.2 FastCGI process manager, and php-mysql adds the mysqli and pdo_mysql extensions that PHP needs to talk to MariaDB. All three services start automatically. Check them:

systemctl status nginx mariadb php8.2-fpm --no-pager

Opening http://example.com/ in a browser now shows the default Nginx welcome page.

Secure MariaDB and create a database

Run the hardening script that comes with MariaDB:

sudo mariadb-secure-installation

On Debian the database root account authenticates through the Unix socket: only the system's root user (or sudo) can use it, and it needs no password. Press Enter when asked for the current root password, keep socket authentication, and answer yes to removing anonymous users, disallowing remote root logins, removing the test database and reloading the privilege tables.

Applications should not connect as root. Open a database shell:

sudo mariadb

Create a database and a user that is limited to it. Use your own names and a strong password:

CREATE DATABASE appdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'deploy'@'localhost' IDENTIFIED BY 'change-this-password';
GRANT ALL PRIVILEGES ON appdb.* TO 'deploy'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Configure PHP-FPM

The default pool in /etc/php/8.2/fpm/pool.d/www.conf runs as www-data and listens on /run/php/php8.2-fpm.sock, which is what Nginx on Debian expects, so it works without changes.

The one setting most sites need to adjust is the upload limit. Open the PHP-FPM php.ini:

sudo nano /etc/php/8.2/fpm/php.ini

Find and set these two values (post_max_size must be at least as large as upload_max_filesize):

upload_max_filesize = 12M
post_max_size = 12M

Restart PHP-FPM to apply the change:

sudo systemctl restart php8.2-fpm

Create the Nginx server block

Create the document root and give your user ownership of it:

sudo mkdir -p /var/www/example.com/public
sudo chown -R deploy:www-data /var/www/example.com

Create a configuration file for the site:

sudo nano /etc/nginx/sites-available/example.com
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    root /var/www/example.com/public;
    index index.php index.html;

    client_max_body_size 12m;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

    location ~ /\.(?!well-known) {
        deny all;
    }
}

What matters here:

  • try_files serves a file or directory if it exists and otherwise passes the request to index.php, which is what most PHP frameworks and CMSs expect.
  • snippets/fastcgi-php.conf is installed by Debian's Nginx package. It sets the FastCGI parameters and refuses requests for PHP files that do not exist.
  • fastcgi_pass must point at the PHP-FPM socket. On Debian 13 that is /run/php/php8.4-fpm.sock.
  • client_max_body_size matches the PHP upload limit set above. Nginx rejects larger request bodies with a 413 error before PHP sees them.
  • The last location blocks access to hidden files such as .env and .git, while leaving .well-known reachable for certificate validation.

Enable the site, disable the default one, test the configuration and reload Nginx:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx

Verify the stack

Create a test script that runs PHP and opens a database connection. Put in the database name, user and password you created earlier:

nano /var/www/example.com/public/test.php
<?php
$db = new mysqli('localhost', 'deploy', 'change-this-password', 'appdb');
echo 'PHP ' . PHP_VERSION . ' connected to MariaDB ' . $db->server_info;

Request it:

curl http://example.com/test.php

The output shows the PHP version followed by the MariaDB version. If PHP cannot connect, it reports the reason instead, usually a wrong password or database name. Delete the file when you are done, because it contains a password:

rm /var/www/example.com/public/test.php

Troubleshooting

  • 502 Bad Gateway: Nginx cannot reach PHP-FPM. Check that the service is running and that the socket path in fastcgi_pass matches a file in /run/php/.
  • The browser downloads the PHP file: the request did not match the PHP location. Make sure the site is enabled and the default site is removed, then reload Nginx.
  • 403 or "File not found": check the root path and that www-data can read the files.

The logs tell you which of these applies:

sudo tail -n 50 /var/log/nginx/error.log
sudo journalctl -u php8.2-fpm --no-pager -n 50

Next steps and maintenance

Security updates for all three components arrive through the normal Debian channel:

sudo apt update
sudo apt upgrade

Before putting a real application online, add a TLS certificate so the site is served over HTTPS, and install any further PHP extensions the application lists, for example:

sudo apt install php-curl php-gd php-mbstring php-xml php-zip
sudo systemctl restart php8.2-fpm

Frequently asked questions

Should I install Nginx from nginx.org instead of the Debian package?

Only if you need a newer Nginx than Debian ships. The nginx.org repository offers current stable and mainline packages for Debian 12 and 13, but they are laid out differently: there is no sites-available directory and no snippets/fastcgi-php.conf, sites go into /etc/nginx/conf.d/, and the worker processes run as the user nginx rather than www-data, so the PHP-FPM socket permissions have to be adjusted. The Debian package avoids that extra work.

Do I need to tune the number of Nginx worker processes?

No. Debian's nginx.conf sets worker_processes auto;, which starts one worker per CPU core. Older guides set the number by hand.

Is MariaDB reachable from the internet after this setup?

No. Debian's MariaDB package listens on 127.0.0.1 only, and the application connects locally. Leave it that way unless a separate application server really has to connect, and in that case restrict access by firewall and by user host.

How do I run a newer PHP version than Debian 12 provides?

Debian keeps PHP 8.2 for the life of the release and backports security fixes to it. If an application requires a newer PHP, the supported route is upgrading to Debian 13, which includes PHP 8.4.

Share story:

Continue reading other guides on How VPS.