What LEMP Is and When to Pick It

LEMP stands for Linux, Nginx (pronounced engine-x, hence the E), MySQL, and PHP. Compared to the older LAMP stack, LEMP swaps Apache for Nginx, which handles static files and concurrent connections more efficiently on a typical VPS. It's the standard choice for WordPress, Laravel, Drupal, and most modern PHP applications. This walkthrough sets up LEMP on Ubuntu 22.04 or 24.04 LTS with sensible defaults for production.

Prepare the Server

Log in as a sudo-capable user and apply all available updates before installing anything else. An out-of-date kernel or libc can break newer packages.

sudo apt update && sudo apt upgrade -y
sudo apt install ufw curl gnupg2 ca-certificates lsb-release -y

Open firewall ports for SSH, HTTP, and HTTPS. The OpenSSH profile is essential — enabling UFW without it will lock you out.

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status

Install Nginx

Ubuntu ships a stable Nginx build in the default archive. For the newest mainline version, add the official Nginx repository instead. For most production sites the distribution package is fine.

sudo apt install nginx -y
sudo systemctl enable --now nginx
curl -I http://localhost

A 200 OK response confirms Nginx is serving its welcome page. Configuration lives in /etc/nginx/: nginx.conf sets global directives, sites-available/ holds per-site configs, and sites-enabled/ contains symlinks to the active sites.

Install MySQL

MySQL 8 is the default on Ubuntu 22.04 and 24.04. Install it and run the security script to set a root password, remove anonymous accounts, and disable remote root login.

sudo apt install mysql-server -y
sudo systemctl enable --now mysql
sudo mysql_secure_installation

Answer yes to removing test databases and reloading privileges. For each web application, create a dedicated user and database:

sudo mysql
CREATE DATABASE appdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'StrongPasswordHere';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

If you prefer MariaDB, install mariadb-server instead — the commands above are identical.

Install PHP-FPM

PHP-FPM (FastCGI Process Manager) is how Nginx executes PHP code. Ubuntu 24.04 ships PHP 8.3 by default; 22.04 ships 8.1. Install the FPM package plus the extensions most CMS platforms require.

sudo apt install php-fpm php-mysql php-cli php-curl php-gd \
    php-mbstring php-xml php-zip php-bcmath php-intl -y

Verify the version and confirm the FPM socket path:

php -v
ls /run/php/

You should see something like php8.3-fpm.sock. Use that exact path in the Nginx config below.

Create a PHP-Enabled Server Block

Nginx needs an explicit server block to pass .php requests to FPM. Create /etc/nginx/sites-available/example.com:

server {
    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;
    }
}

Enable the site and disable the default:

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

Test PHP with a quick phpinfo page:

sudo mkdir -p /var/www/example.com
echo "<?php phpinfo();" | sudo tee /var/www/example.com/info.php
sudo chown -R www-data:www-data /var/www/example.com

Visit http://example.com/info.php — you should see the PHP info page. Delete it immediately afterward; it exposes configuration details.

Tune PHP-FPM for Your VPS

Default FPM settings are conservative. For a 2 GB VPS running a typical PHP application, edit /etc/php/8.3/fpm/pool.d/www.conf:

pm = dynamic
pm.max_children = 20
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 500

Calculate pm.max_children by dividing available RAM by the memory footprint of one PHP worker (typically 30–60 MB). Reload with sudo systemctl reload php8.3-fpm.

Enable HTTPS

Point your domain at the VPS and install Certbot to issue a free Let's Encrypt certificate:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com

Certbot edits the server block to listen on 443 and sets up a renewal timer. For wildcard or EV certificates, browse our SSL options or follow the step-by-step SSL install guide.

LEMP Stack Summary

ComponentService NameConfig Path
Nginxnginx/etc/nginx/
MySQLmysql/etc/mysql/mysql.conf.d/
PHP-FPMphp8.3-fpm/etc/php/8.3/fpm/
Firewallufw/etc/ufw/

Next steps: harden SSH and limit sudo with our security hardening guide, and schedule daily backups of /var/www and MySQL dumps.

Need a production-ready LEMP host? MassiveGRID's Cloud VPS gives you NVMe storage, root access, and four global data centers for fast PHP hosting. Contact our team to pick the right plan.

Published by MassiveGRID, your trusted Linux VPS hosting partner. Explore our Cloud VPS plans for root-access Ubuntu hosting.