LEMP Stack on Ubuntu and Debian

LEMP Stack on Ubuntu and Debian

Setting up a PHP site on Ubuntu or Debian means installing the services that handle web requests and application data. A LEMP stack uses Nginx to pass PHP requests to PHP-FPM, while MySQL stores application data.

This article shows how to install a LEMP stack on Ubuntu or Debian and check its PHP handler.

TL;DR

A working LEMP stack requires Nginx to pass PHP requests to a reachable PHP-FPM listener.

  • Check the active PHP-FPM service before testing a request.
  • Use a site-specific server block and the distribution FastCGI parameters.
  • Create a restricted MySQL account for the application and confirm its connection.

What Is a LEMP Stack?

A LEMP stack combines Linux, Nginx, MySQL and PHP to serve dynamic websites. MySQL stores application data.

Nginx is commonly pronounced Engine-X. LEMP is distinct from a bundled local development suite because each component is managed as its own Linux service and package. The separation lets an administrator update or inspect the web server, PHP runtime and database through their respective configuration and service tools.

ComponentRoleWhat connects it
LinuxRuns the server and servicesUbuntu or Debian host
NginxAccepts HTTP requests and serves static filesServer block and FastCGI configuration
PHP-FPMExecutes PHP applicationsUnix socket or configured TCP listener
MySQLStores application recordsPHP application and MySQL driver

LAMP uses Apache in place of Nginx. The right choice depends on the server and application configuration you intend to maintain, not on the acronym alone. Choose one web server for a straightforward first setup, and use the LAMP installation guide if Apache better fits your existing configuration.

How to Install a LEMP Stack on Ubuntu or Debian

Use a server account that can run administrative commands. The stack setup assumes the host can reach its configured distribution repositories.

Step 1: Refresh the package index and install the services

APT uses its local package index to find current repository versions, so refresh it before installing. Confirm configured repositories are reachable. If APT proposes removing a service the server depends on, cancel and plan that migration separately, and consult the APT package manager guide for transaction details.

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

Ubuntu 24.04 noble package metadata resolves generic PHP packages to PHP 8.3. The simulated transaction proposed removing MariaDB packages, so review that change if MariaDB already serves an application.

Step 2: Start the services and allow web traffic

Enable the services so they start with the server, and allow inbound HTTP and HTTPS only if this host uses UFW. Starting them now exposes service errors before a reboot. A provider firewall or a separate network policy may also control access.

sudo systemctl enable --now nginx mysql
sudo systemctl enable --now php8.3-fpm
sudo ufw allow in "Nginx Full"

Confirm the matching FPM unit before enabling it. When administering UFW remotely, confirm SSH remains allowed. The UFW setup guide covers firewall configuration.

Step 3: Find the PHP-FPM listener

List available PHP sockets after installing FPM.

ls -l /run/php/

A package-managed Ubuntu installation commonly uses a socket named for the PHP release, such as /run/php/php8.3-fpm.sock.

Step 4: Configure a server block for PHP

Configure the document root and PHP request handler in the site block. The root tells Nginx where site files live, while the index list controls which file it checks for a directory request. The server_name directive selects the hostname served by this block.

For a single local check, the default server can be enough, but a public site should use a deliberate hostname and document root. Separate site blocks keep their file roots and request handling distinct.

Ubuntu and Debian package installations include a default site file at /etc/nginx/sites-available/default. Back it up before editing, or create a separate server block for a named site.

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

Within the active server block, include index.php in the index list and add a PHP handler.

index index.php index.html index.htm;

location / {
    try_files $uri $uri/ =404;
}

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

The try_files rule serves an existing file or directory and returns 404 when neither exists.

Step 5: Validate Nginx and request a temporary PHP page

Check the configuration before reloading Nginx. Then create a short PHP file in the configured document root and request it from a browser using the server’s address.

sudo nginx -t
sudo systemctl reload nginx
printf '<?php echo "PHP handler works"; ?>\n' | sudo tee /var/www/html/health.php
curl http://127.0.0.1/health.php
sudo rm /var/www/html/health.php

Use your configured document root if it differs from /var/www/html. The response should contain PHP output rather than source code, and the temporary file should be removed after the check. A phpinfo page exposes server details, so do not leave one publicly reachable.

A local curl request only proves the local Nginx path works. To check access from a client, request the server address from that client after confirming the host and provider firewall rules allow web traffic.

Step 6: Secure MySQL and prepare application access

Run the installed MySQL hardening utility, then create an application database and grant its account access to that schema. Do not use the database administrator account in application configuration.

Database privileges are separate from Linux file permissions, so changing PHP file ownership will not repair a rejected SQL login, and the application configuration must use the matching database name and account.

sudo mysql_secure_installation
sudo mysql

At the MySQL prompt, substitute a unique database name, application account and long password. Store credentials in a protected configuration file outside source control.

This example binds access to localhost. A separate application server needs its own network path and access controls.

CREATE DATABASE site_db;
CREATE USER 'site_app'@'localhost' IDENTIFIED BY 'replace-with-a-long-unique-password';
GRANT ALL PRIVILEGES ON site_db.* TO 'site_app'@'localhost';
EXIT;

Applications can now connect locally with the database name and restricted account. Use the application’s own connection test to confirm credentials and schema access. The MySQL installation guide covers database-specific administration.

Check the PHP Handler and Fix Common Errors

Validate Nginx configuration before reloading it. I ran nginx -t without elevated privileges: Nginx reported that syntax was ok, then failed because it could not open /run/nginx.pid with permission denied. Run this administrative check on your server with the documented privilege level, then inspect the exact error if validation still fails.

SymptomLikely causeCheck or fix
502 Bad GatewayNginx cannot connect to PHP-FPMCheck FPM service state and compare fastcgi_pass with the listener path.
PHP source appears in the browserNginx is serving the file without sending it to FPMCheck the PHP location block, included FastCGI parameters and matching socket. Keep PHP files private until fixed.
A missing PHP pageWrong document root or requested file pathConfirm the server block’s root and that the file exists below it.
Database connection rejectedWrong account, password, database or PHP driverCheck the application’s connection settings and ensure php-mysql is installed.
Connection times out from another computerFirewall or network policy blocks web trafficCheck UFW and any separate provider firewall rule for ports 80 and 443.

A Server Fault report documents a missing PHP-FPM socket behind an older LEMP 502 error. Nginx error logs can identify the upstream path, while PHP-FPM service status shows whether the matching pool started. Compare both before changing socket modes, since broad permissions can expose the listener to local users.

After identifying the failing request, review its timestamp and URI alongside the error-log entry before changing any service configuration. If the browser displays PHP source, treat the page as exposed and disable public access until the handler works. A plain-text PHP response means the server did not execute the script, so do not put application credentials into a test file until the handler is confirmed.

For a web server that appears stopped, inspect its state with systemctl service checks. If you want a PHP application after the handler test, continue with the WordPress installation guide.

Conclusion

Deploy the application only after the temporary handler check succeeds.

For configuration details, consult Ubuntu’s Nginx server-block documentation, the Nginx FastCGI module reference, the PHP-FPM manual and the Ubuntu MySQL guide. For the next application step, use the linked WordPress guide or compare this setup with the LAMP guide if Apache is the better fit.

FAQ

These answers address the package and configuration choices that commonly come up when setting up LEMP.

What does LEMP stand for?

LEMP stands for Linux, Nginx (Engine-X), MySQL, and PHP. PHP-FPM processes PHP requests passed to it by Nginx.

Can I use MariaDB instead of MySQL?

Many applications can use MariaDB as a MySQL-compatible database, but check your application’s support and package transaction before replacing an existing database service.

How do I find the PHP-FPM socket path?

Inspect the PHP runtime sockets under /run/php/ and match the active listener to the fastcgi_pass value in the Nginx server block. The path depends on the installed PHP version and FPM configuration.

Why does Nginx return 502 for a PHP page?

A common cause is that Nginx cannot reach the PHP-FPM listener. Check that FPM is running and that fastcgi_pass uses the socket or address configured by the active FPM pool.