What LEMP Means
The E stands for Nginx, pronounced "engine-x". Nginx serves static files and passes PHP requests to PHP-FPM, a separate service; PHP talks to the database. Unlike Apache, Nginx does not run PHP itself, so the link between the two is a socket named in the server block. The steps below are for Ubuntu and Debian; the Red Hat family follows at the end.
Step 1: Install Nginx
sudo apt update
sudo apt install nginx
sudo ufw allow 'Nginx Full'
If ufw has no Nginx profile, allow 80/tcp and 443/tcp. If ufw is not active yet, allow your SSH port before you run sudo ufw enable. Details are in how to install Nginx.
Step 2: Install the Database
sudo apt install mariadb-server
sudo mariadb-secure-installation
Debian ships MariaDB as its MySQL-compatible server. On Ubuntu you can choose mysql-server instead and secure it with sudo mysql_secure_installation; see how to install MySQL on Ubuntu. Create a database and an account for the application:
sudo mariadb
CREATE DATABASE app;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON app.* TO 'app_user'@'localhost';
EXIT;
With MySQL, open the shell with sudo mysql instead. Both packages listen on 127.0.0.1 by default on Debian and Ubuntu. Confirm with sudo ss -tlnp | grep 3306, and never open port 3306 in the firewall.
Step 3: Install PHP-FPM
sudo apt install php-fpm php-mysql
systemctl status php8.3-fpm
Do not install the php metapackage on an Nginx server: it prefers the Apache module, so it installs Apache as well. The version in the service name is the one your release ships, for example 8.3 on Ubuntu 24.04, 8.4 on Debian 13, and 8.5 on Ubuntu 26.04. Extensions and php.ini are covered in how to install PHP on Ubuntu.
Step 4: A Server Block for PHP
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Save it as /etc/nginx/sites-available/example.com, create /var/www/example.com, and enable the site:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Check the socket name with ls /run/php/. Frameworks that route every address through index.php replace =404 with /index.php?$args. Reload rather than restart, so a failed test leaves the running server untouched. More in Nginx server blocks.
Step 5: Test PHP, Then Delete the Test
echo '<?php phpinfo();' | sudo tee /var/www/example.com/info.php
curl -s http://localhost/info.php -H "Host: example.com" | grep -m1 -o 'PHP Version [0-9.]*'
sudo rm /var/www/example.com/info.php
The page lists the PHP version and every setting. That is exactly why it must not stay: it tells an attacker how the server is built. Delete it as soon as the test works. A 502 at this step means Nginx cannot reach PHP-FPM; see 502 Bad Gateway in Nginx.
AlmaLinux and Rocky Linux
sudo dnf install nginx mariadb-server php-fpm php-mysqlnd
sudo systemctl enable --now nginx mariadb php-fpm
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
Put the server block in /etc/nginx/conf.d/example.com.conf. There is no snippets folder, so the PHP location reads:
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Run sudo restorecon -Rv /var/www/example.com so SELinux labels the files as web content. PHP-FPM runs as apache by default, and its socket admits Nginx through listen.acl_users; the pool file is /etc/php-fpm.d/www.conf. Secure the database with sudo mariadb-secure-installation. MariaDB's own default is to listen on all addresses, and this family does not change it: add bind-address = 127.0.0.1 under [mysqld] in /etc/my.cnf.d/mariadb-server.cnf, restart MariaDB, and confirm with ss.
Firewall and Next Steps
Open only 80 and 443, plus your SSH port. Then add a certificate with Certbot for Nginx.