All posts

Blog

How to Install a LEMP Stack on Ubuntu (Nginx, MySQL, PHP)

Illustrated cover for the Developers guide "How to Install a LEMP Stack on Ubuntu (Nginx, MySQL, PHP)"

LEMP is the stack behind a huge share of PHP websites: Linux as the operating system, Engine-X (Nginx) as the web server, MySQL or MariaDB as the database, and PHP to run the application code. If you have a fresh Ubuntu VPS and want to host WordPress, Laravel, or your own PHP project, this is where you start.

This tutorial shows how to install a LEMP stack on Ubuntu using the packages from Ubuntu's own repositories. The commands work on current LTS releases such as Ubuntu 22.04 and 24.04. Package versions differ between releases, so wherever a version number appears, check what your system actually installed.

Before you begin

You need:

  • An Ubuntu server you can reach over SSH.
  • A non-root user with sudo privileges (log in with SSH keys if you can).
  • Optionally, a domain name pointed at the server's IP with an A record.

Start by updating the package index and installed packages:

sudo apt update && sudo apt upgrade -y

Step 1: Install Nginx

sudo apt install nginx -y

Ubuntu starts and enables Nginx automatically. Check it:

systemctl status nginx

Open the firewall

If you use UFW, allow SSH first (so you do not lock yourself out) and then web traffic:

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

Nginx Full opens ports 80 and 443. Visit http://your.server.ip in a browser and you should see the default "Welcome to nginx!" page.

Step 2: Install MySQL (or MariaDB)

sudo apt install mysql-server -y

If you prefer MariaDB, a community-developed fork that is largely compatible with MySQL, install mariadb-server instead. Pick one, not both.

Secure the installation

Run the bundled security script:

sudo mysql_secure_installation

It walks you through removing anonymous users, disabling remote root login, and dropping the test database. Answer yes to those. On Ubuntu, the MySQL root account uses socket authentication by default, which means you log in with sudo mysql rather than a password. That is fine, and arguably safer, for administration.

Create a database and user for your app

Never point an application at the root database account. Create a dedicated one:

sudo mysql
CREATE DATABASE myapp DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'use-a-long-random-password';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Store the password in your app's config or environment file, not in your shell history notes.

Step 3: Install PHP-FPM

Nginx does not execute PHP itself. It hands PHP requests to PHP-FPM (FastCGI Process Manager). Install it with the MySQL extension and a few commonly needed modules:

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

Find out which version you got:

php -v
ls /run/php/

You will see a socket named something like php8.3-fpm.sock. Note the exact filename; you need it in the next step.

Step 4: Create an Nginx server block

Create a directory for your site and give your user ownership:

sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com

Create a new server block:

sudo nano /etc/nginx/sites-available/example.com

Paste the following, replacing the domain and the PHP socket path with yours:

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/ /index.php?$query_string;
    }

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

    location ~ /\.(?!well-known) {
        deny all;
    }
}

A few notes:

  • try_files ... /index.php?$query_string sends "pretty URLs" to your front controller, which WordPress and Laravel both expect.
  • The snippets/fastcgi-php.conf file ships with Ubuntu's Nginx package and sets sensible FastCGI defaults.
  • The last block blocks access to hidden files like .env and .git, while still allowing .well-known for certificate validation.

Enable the site, test the configuration, and reload:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

If you are not using the default site, you can remove its symlink with sudo rm /etc/nginx/sites-enabled/default.

Step 5: Test PHP and the database connection

Create a quick test file:

nano /var/www/example.com/info.php
<?php
phpinfo();

Visit http://example.com/info.php. A page full of PHP configuration details means Nginx and PHP-FPM are talking. Delete this file right after testing, because it reveals a lot about your server:

rm /var/www/example.com/info.php

To confirm database access from PHP, you can create a tiny script that connects with mysqli or PDO using the user you created, load it once, then delete it too.

Step 6: Add HTTPS

Once your domain resolves to the server, install a free Let's Encrypt certificate with Certbot's Nginx plugin:

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

Certbot edits your server block to add the certificate and can set up the HTTP-to-HTTPS redirect for you. Renewal is handled automatically by a systemd timer.

Basic tuning worth doing

  • PHP upload limits: edit /etc/php/<version>/fpm/php.ini and raise upload_max_filesize and post_max_size if your app needs larger uploads. Then set a matching client_max_body_size in Nginx. Restart PHP-FPM with sudo systemctl restart php8.3-fpm (adjust the version).
  • OPcache: it is enabled by default in Ubuntu's PHP packages and gives a large boost to PHP performance. Leave it on.
  • PHP-FPM pool size: on a small VPS, check pm.max_children in /etc/php/<version>/fpm/pool.d/www.conf so PHP cannot spawn more workers than your RAM can hold.

Troubleshooting

  • 502 Bad Gateway: usually the socket path in fastcgi_pass does not match the real socket, or PHP-FPM is not running. Check ls /run/php/ and systemctl status php8.3-fpm.
  • PHP files download instead of running: the location ~ \.php$ block is missing or not matching.
  • 404 on pretty URLs: the try_files line is missing or wrong.
  • Logs: /var/log/nginx/error.log is the first place to look.

Wrap-up

You now have a working LEMP stack on Ubuntu: Nginx serving requests, PHP-FPM executing code, MySQL storing data, and HTTPS on top. From here you can drop in WordPress, deploy a Laravel app, or upload your own project. Keep the system updated with sudo apt update && sudo apt upgrade, take regular database backups, and you have a solid foundation to build on.