Skip to main content
To install a LEMP stack on Ubuntu 24.04, run sudo apt install nginx mariadb-server php-fpm php-mysql, secure the database with sudo mysql_secure_installation, then add a server block that passes .php files to unix:/run/php/php8.3-fpm.sock. Test with sudo nginx -t, reload Nginx and open a PHP test page. On Debian 13 the socket is php8.4-fpm.sock.

Before you start

  • LEMP means Linux, Engine-x (Nginx), MariaDB or MySQL, and PHP. It runs most PHP apps, such as WordPress.
  • Use a user with sudo. If you are root, leave out sudo.
  • Allow ports 80 and 443 in your firewall, as in install Nginx.
  • PHP version by release: Ubuntu 22.04 has 8.1, Ubuntu 24.04 has 8.3, Debian 12 has 8.2, Debian 13 has 8.4, AlmaLinux / Rocky 10 has 8.3. Need another version? See multiple PHP versions.

How to install Nginx, MariaDB and PHP

All three services start automatically. Check your PHP version, which is part of the socket name:

How to secure MariaDB

Answer the prompts. Removing anonymous users, disallowing remote root login and removing the test database are the safe choices. More detail is in secure MySQL and MariaDB.

How to connect Nginx to PHP-FPM

PHP-FPM (FastCGI Process Manager) runs PHP as its own service. Nginx hands it every .php request through a socket file.
1

Create the site folder

2

Create the server block

Create /etc/nginx/sites-available/example.com:
Change php8.3 to match php -v (for example php8.4 on Debian 13). Then enable it:
3

Test and reload Nginx

How to check it worked

Create a small test file:
Request it through Nginx on the server:
Expected output on Ubuntu 24.04:
fpm-fcgi proves Nginx passed the request to PHP-FPM. The patch number may be newer on your server. Delete the file afterwards:
Never leave phpinfo() or test files on a live site. They show paths and settings that help attackers.

Frequently asked questions

I get “502 Bad Gateway”. What is wrong?

Nginx cannot reach PHP-FPM. Usually the socket name in fastcgi_pass does not match the installed PHP version, or php-fpm is not running. See fix 502 Bad Gateway.

My browser downloads the .php file instead of running it. Why?

The location ~ \.php$ block is missing or the site you edited is not enabled. Run sudo nginx -T | grep -n fastcgi_pass to see what Nginx actually loaded.

Should I use MariaDB or MySQL?

Both work for WordPress and most PHP apps. Ubuntu and Debian ship MariaDB as mariadb-server. See install MySQL or MariaDB.

How do I add HTTPS?

Run Certbot once your domain points to the server. See free SSL with Certbot.

VPS and dedicated server guides

Every server guide in one place.

Install WordPress

Put WordPress on your LEMP stack.

Create a database and user

One database per app.

Increase PHP upload size

Raise upload and memory limits.

Install a LAMP stack

The same with Apache.

Checked against the official Ubuntu Server PHP documentation, Red Hat Enterprise Linux 9 PHP guide and Debian 13 release notes on 4 November 2026; Ubuntu commands tested on Ubuntu 24.04 LTS (Nginx 1.24.0, MariaDB 10.11, PHP 8.3.6). Commands can differ on other distributions or releases.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026