How to Install LEMP on Ubuntu: Nginx, PHP-FPM, MySQL
Run on the server with one command (check the script contents below first):
curl -sSL https://cp.lv/scripts/ustanovit-lemp-ubuntu.sh | sudo bash…or with wget:
wget -qO- https://cp.lv/scripts/ustanovit-lemp-ubuntu.sh | sudo bashScript contents ustanovit-lemp-ubuntu.sh:
#!/usr/bin/env bash
set -e
export DEBIAN_FRONTEND=noninteractive
# 1. Update the system
sudo apt-get update
sudo apt-get upgrade -y
# 2. Nginx, MySQL, PHP-FPM and the extensions a CMS needs
sudo apt-get install -y nginx mysql-server php-fpm php-mysql \
php-cli php-curl php-gd php-mbstring php-xml php-zip
sudo systemctl enable --now nginx mysql
# 3. Site root and a test index.php
sudo mkdir -p /var/www/example.com
sudo chown -R www-data:www-data /var/www/example.com
echo "<?php phpinfo();" | sudo tee /var/www/example.com/index.php > /dev/null
# 4. server block (quoted 'EOF' leaves Nginx variables alone)
sudo tee /etc/nginx/sites-available/example.com > /dev/null << 'EOF'
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/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
EOF
# 5. Enable the site
sudo ln -sf /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
sudo nginx -t && sudo systemctl reload nginx
echo "LEMP is ready: open http://example.com"
LEMP is the combination of Linux, Nginx, MySQL and PHP — the standard environment for PHP sites (WordPress, Laravel, custom projects). Below is a ready sequence of commands for Ubuntu 22.04/24.04. The whole process takes 10–15 minutes.
What you need first
- A VPS with Ubuntu 22.04 or 24.04 and SSH access.
- A user with
sudorights (do not work as root permanently). - A domain whose A record points to the server IP (for a production site).
Start by updating the system:
sudo apt update && sudo apt upgrade -y
Step 1. Install Nginx
sudo apt install nginx -y
sudo systemctl enable --now nginx
Check the status and open the firewall ports:
sudo systemctl status nginx
sudo ufw allow 'Nginx Full'
sudo ufw allow OpenSSH
sudo ufw enable
Open the server IP in a browser — the “Welcome to nginx” page should appear.
Step 2. Install MySQL
sudo apt install mysql-server -y
sudo systemctl enable --now mysql
Run the interactive hardening script for the database server:
sudo mysql_secure_installation
Set the root password, remove anonymous users, disallow remote root login and drop the test database. Then create a database and a dedicated user for the site:
sudo mysql
CREATE DATABASE app_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 3. Install PHP-FPM
Nginx cannot process PHP itself — it passes requests to the PHP-FPM process over a socket.
sudo apt install php-fpm php-mysql -y
Check the version and the service name (on Ubuntu 24.04 it is php8.3-fpm):
php -v
systemctl status php8.3-fpm
A useful set of extensions for a CMS:
sudo apt install php-cli php-curl php-gd php-mbstring php-xml php-zip -y
Step 4. Configure the server block
Create the site root and a test file:
sudo mkdir -p /var/www/example.com
sudo chown -R www-data:www-data /var/www/example.com
echo "<?php phpinfo();" | sudo tee /var/www/example.com/index.php
Open the config /etc/nginx/sites-available/example.com in an editor:
sudo nano /etc/nginx/sites-available/example.com
File contents:
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/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Or write the whole config with a single command — copy it and paste it into the console (the quotes around 'EOF' matter, so that the Nginx variables $uri stay as they are):
sudo tee /etc/nginx/sites-available/example.com > /dev/null << 'EOF'
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/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
EOF
Enable the site and reload Nginx:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Step 5. Test PHP
Open http://example.com — you will see the phpinfo() page with the PHP version and the loaded modules. Make sure mysqli or pdo_mysql is in the list. After the check, delete the test file: it exposes your server configuration.
sudo rm /var/www/example.com/index.php
Quick component check
| Component | Check command | Expected |
|---|---|---|
| Nginx | sudo nginx -t |
syntax is ok |
| PHP-FPM | systemctl is-active php8.3-fpm |
active |
| MySQL | mysqladmin -u root -p status |
Uptime: … |
| PHP socket | ls /run/php/ |
php8.3-fpm.sock |
FAQ
Why does the site output PHP code as plain text?
Nginx is not passing the request to PHP-FPM. Check the location ~ \.php$ block and the socket path in fastcgi_pass.
What causes a 502 Bad Gateway error?
Usually PHP-FPM is not running or the wrong socket is set. Run systemctl status php8.3-fpm and compare the .sock file name with the config.
How do I find the PHP version on my Ubuntu?
Run php -v. Ubuntu 22.04 ships PHP 8.1 by default, Ubuntu 24.04 ships PHP 8.3. The service and socket names depend on the version.
Can I use MariaDB instead of MySQL?
Yes. MariaDB is compatible and installs with apt install mariadb-server. The client commands and SQL are identical.
LEMP runs well even on a small server, but for production traffic pick an NVMe disk and enough RAM. Ready-made plans are on the VPS hosting page, and for busy sites there is WordPress VPS.