On this page
- Why choose Nginx with PHP-FPM?
- What should be ready before you start?
- How do you install Nginx, PHP-FPM and the database on Ubuntu or Debian?
- How do you install the stack on Rocky Linux or AlmaLinux?
- How do you write the Nginx server block?
- How do you create the database and its user?
- How do you open the firewall and add a certificate?
- How do you check that every layer works?
- How do you tune and maintain a LEMP server?
- Questions
Why choose Nginx with PHP-FPM?
Nginx handles many idle connections with very little memory and never runs PHP itself, so PHP workers are only busy while a script executes. That split suits small VPS plans: static files, images and keep-alive clients cost almost nothing, and the number of PHP-FPM children sets a hard ceiling on PHP memory.
The trade-off is that Nginx ignores .htaccess files. Rewrite rules that an app ships for Apache must be translated into the server block once, which for most modern frameworks is a single try_files line. If your software depends on many per-folder .htaccess rules, the LAMP stack may be the easier fit.
What should be ready before you start?
Have a sudo user, an updated system and DNS records for your domain pointing at the server.
# Ubuntu / Debian
sudo apt update && sudo apt -y upgrade
# Rocky / Alma
sudo dnf -y upgradeCreate the A record for example.net pointing to 198.51.100.24 and an AAAA record for 2001:db8:4f2a::24, as described in DNS records. Certificates later in this guide need those names to resolve.
How do you install Nginx, PHP-FPM and the database on Ubuntu or Debian?
Install the packages by name and avoid the plain php metapackage, which would drag Apache in.
sudo apt install -y nginx mariadb-server php-fpm php-mysql \
php-cli php-curl php-gd php-mbstring php-xml php-zip php-intl
sudo systemctl enable --now nginx mariadb
systemctl list-units --type=service | grep fpm
nginx -v
php -vThe PHP-FPM unit carries the version in its name, for example php8.3-fpm on Ubuntu 24.04, php8.2-fpm on Debian 12 and php8.4-fpm on Debian 13. Its socket lives at /run/php/php8.3-fpm.sock (with the matching number). On Ubuntu you may use mysql-server instead of mariadb-server; on Debian, MariaDB is the packaged MySQL-compatible server.
How do you install the stack on Rocky Linux or AlmaLinux?
Use AppStream for all three, choosing a PHP module stream first if 8.0 is too old for your app.
sudo dnf module reset -y php
sudo dnf module enable -y php:8.2
sudo dnf install -y nginx mariadb-server php-fpm php-mysqlnd \
php-cli php-gd php-mbstring php-xml php-intl php-opcache
sudo systemctl enable --now nginx mariadb php-fpmThe RHEL packages set PHP-FPM to run as apache. Change it so files written by PHP belong to Nginx:
sudo sed -i 's/^user = apache/user = nginx/; s/^group = apache/group = nginx/' /etc/php-fpm.d/www.conf
sudo chgrp -R nginx /var/lib/php/session
sudo systemctl restart php-fpm
ls -l /run/php-fpm/www.sockThe package also installs /etc/nginx/conf.d/php-fpm.conf, which defines an upstream named php-fpm pointing at /run/php-fpm/www.sock. You can use either the upstream name or the socket path in fastcgi_pass.
How do you write the Nginx server block?
A server block names the domain, sets the root, tries static files first and passes only real .php files to the socket.
sudo mkdir -p /var/www/example.net/public
sudo chown -R deploy:deploy /var/www/example.netSave as /etc/nginx/sites-available/example.net on Ubuntu or Debian, or as /etc/nginx/conf.d/example.net.conf on Rocky or Alma:
server {
listen 80;
listen [::]:80;
server_name example.net www.example.net;
root /var/www/example.net/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.3-fpm.sock; # Rocky/Alma: unix:/run/php-fpm/www.sock
}
location ~ /\.(?!well-known) {
deny all;
}
}# Ubuntu / Debian
sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginxOn Rocky or Alma just run sudo nginx -t && sudo systemctl reload nginx. The try_files $uri =404 line stops Nginx from handing PHP a path that does not exist, which closes an old path-info trick. For more server block patterns, see the Nginx guide.
How do you create the database and its user?
Harden the server once, then make one database and one local account per application.
sudo mariadb-secure-installation # with MySQL: sudo mysql_secure_installation
sudo mariadb
CREATE DATABASE blog CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'blog'@'localhost' IDENTIFIED BY 'Pick-Another-Strong-Pass-27';
GRANT ALL PRIVILEGES ON blog.* TO 'blog'@'localhost';
EXIT;With MySQL on Ubuntu, connect with sudo mysql instead. Both servers bind to localhost by default, so nothing extra needs closing. Finer privilege control is covered in MySQL users and privileges.
How do you open the firewall and add a certificate?
Allow web traffic, then let Certbot fetch a certificate and rewrite the server block for HTTPS.
# Ubuntu / Debian
sudo ufw allow OpenSSH && sudo ufw allow 'Nginx Full' && sudo ufw enable
sudo apt install -y certbot python3-certbot-nginx
# Rocky / Alma
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
sudo dnf install -y epel-release && sudo dnf install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.net -d www.example.net
sudo certbot renew --dry-runCertbot adds a listen 443 ssl block and a redirect from port 80. If you would rather let a web server handle certificates on its own, Caddy is an alternative front end that can also pass requests to PHP-FPM.
How do you check that every layer works?
Request a short PHP file through Nginx that reports PHP, the server API and the database version.
cat > /var/www/example.net/public/check.php <<'EOF'
<?php
$pdo = new PDO('mysql:host=localhost;dbname=blog', 'blog', 'Pick-Another-Strong-Pass-27');
echo PHP_VERSION, ' ', PHP_SAPI, ' ', $pdo->query('SELECT VERSION()')->fetchColumn(), PHP_EOL;
EOF
curl -s https://example.net/check.php
rm /var/www/example.net/public/check.phpThe output should show fpm-fcgi as the server API. A 502 Bad Gateway means Nginx cannot reach the socket: compare the path in fastcgi_pass with sudo ss -xlp | grep php and read /var/log/nginx/error.log. A File not found reply means SCRIPT_FILENAME points somewhere PHP cannot see, usually a wrong root.
How do you tune and maintain a LEMP server?
Set PHP-FPM worker limits to fit the RAM, keep packages current and back up the database.
- In the pool file (
/etc/php/8.3/fpm/pool.d/www.confor/etc/php-fpm.d/www.conf) setpm.max_childrento roughly free RAM divided by the memory of one worker; 50 to 80 MB per worker is common for CMS sites. - Raise
client_max_body_sizein Nginx together withupload_max_filesizein PHP when uploads fail with 413. - Patch with apt or dnf, and restart PHP-FPM after PHP updates.
- Dump databases nightly and keep copies off the server; see MySQL backup and restore.
Running several sites? Give each its own pool and Linux user as described in PHP-FPM pools.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about LEMP stack: Nginx, MySQL, PHP-FPM
Does Nginx read .htaccess files?
No. Move rewrite rules into the server block. Most PHP frameworks only need try_files $uri $uri/ /index.php?$query_string;.
Why do I get 502 Bad Gateway after an upgrade?
The PHP-FPM socket name changes with the PHP version on Debian and Ubuntu, for example from php8.2-fpm.sock to php8.3-fpm.sock. Update fastcgi_pass and reload Nginx.
Can I use MySQL instead of MariaDB?
Yes on Ubuntu and Rocky or Alma, where mysql-server is packaged. Debian ships MariaDB, which works with the same PHP drivers.
Should PHP-FPM listen on a socket or on 127.0.0.1:9000?
A Unix socket is slightly faster and cannot be reached from the network, so use it when Nginx and PHP share a server. TCP is useful only when PHP runs on another host or in a container.
How many sites can one LEMP server hold?
It depends on traffic, not on a fixed limit. Several low-traffic sites share a 2 GB server well; give each a separate pool so one busy site cannot starve the rest.