Skip to main content
Tutorials

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

Linux, Nginx, MySQL, and PHP — the foundation for hosting dynamic sites on your own VPS, installed the right way.

VS Volt Serv Team 8 min read

Key Takeaways

  • LEMP = Linux, Nginx (the "E" is for engine-x), MySQL/MariaDB, and PHP.
  • Nginx passes PHP requests to PHP-FPM; the database stores your app data.
  • Lock the database down and add SSL before you go to production.

A LEMP stack is the modern foundation for hosting dynamic websites — WordPress, Laravel apps, custom PHP, you name it. It stands for Linux, Engine-x (Nginx), MySQL/MariaDB, and PHP. This guide gets you from a fresh Ubuntu 24.04 server to a working stack. You will want a Linux VPS with root access and a domain pointed at it (see how to point a domain to a VPS).

1. Update the server

Always start fresh: sudo apt update && sudo apt upgrade -y.

2. Install Nginx

sudo apt install nginx -y, then sudo systemctl enable --now nginx. Visit your server's IP in a browser and you should see the Nginx welcome page. If not, make sure ports 80/443 are open — you can check from outside with our port checker.

3. Install the database

Install MariaDB (a drop-in MySQL replacement) with sudo apt install mariadb-server -y, then run sudo mysql_secure_installation and answer the prompts to set a root password, remove anonymous users, and disable remote root login. This step matters — an open database is a common cause of breaches.

4. Install PHP-FPM

Nginx does not run PHP itself; it hands PHP requests to PHP-FPM. Install it with sudo apt install php-fpm php-mysql -y, plus any extensions your app needs (e.g. php-curl php-gd php-mbstring).

5. Tell Nginx to use PHP

Create a server block for your site in /etc/nginx/sites-available/, set the root to your web directory, add index.php to the index list, and add a location ~ \.php$ block that passes requests to the PHP-FPM socket. Enable the site, test the config with sudo nginx -t, and reload with sudo systemctl reload nginx.

6. Test it

Drop a file with <?php phpinfo(); ?> into your web root and load it in a browser. If you see the PHP info page, the whole stack is talking to itself. Delete that file afterward — it exposes server details.

7. Go to production

Before real traffic, add a free Let's Encrypt SSL certificate with certbot and verify it with our SSL checker. Then harden the server with our security checklist.

What's next

With LEMP running you can install almost any PHP app. The obvious next step is installing WordPress. Curious why we chose Nginx? Read Nginx vs Apache.

Share this article
VS

Volt Serv Team

Hosting specialists at Volt Serv. We publish practical guides to help you choose, deploy, and secure your servers.

Power Your Project with Volt Serv

VPS hosting from $4.99/mo with full root access, NVMe SSD storage, free DDoS protection, and a 99.99% uptime guarantee.