NewHalo Edge 3 is here: HTTP/3 everywhere and 42 edge locations.Learn more
Docs / LAMP and app hosting

Install a LEMP stack

LAMP and app hosting10 min readUbuntu 24.04 LTS

LEMP stands for Linux, nginx (pronounced "engine-x"), MariaDB and PHP. nginx is a lightweight web server that pairs well with PHP-FPM, the separate process manager that runs PHP. This guide builds that stack on a fresh Ubuntu 24.04 VPS.

Before you begin

  • A fresh VPS running Ubuntu 24.04 LTS with a public IP, for example 203.0.113.10.
  • SSH access as a user with sudo rights (this guide uses alex).
  • Nothing else listening on port 80. If you followed the LAMP guide on the same server, pick one stack, not both.

Step 1: Update the system

sudo apt update
sudo apt upgrade -y

Step 2: Install nginx and open the firewall

nginx is the web server. The Nginx Full firewall profile opens ports 80 (HTTP) and 443 (HTTPS). Open SSH first so the firewall does not block your own connection.

sudo apt install -y nginx
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)

Step 3: Install MariaDB

MariaDB stores your application's data. Install it and confirm it is running. You will harden it in the next guide.

sudo apt install -y mariadb-server
sudo systemctl status mariadb --no-pager

Step 4: Install PHP-FPM and extensions

php8.3-fpm runs PHP as a separate service that nginx talks to over a socket. The other packages are common extensions: mysql for the database, plus libraries for HTTP requests, images, multibyte text and archives.

sudo apt install -y php8.3-fpm php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip
sudo systemctl status php8.3-fpm --no-pager
ls /run/php/

The last command should list php8.3-fpm.sock. That socket is how nginx reaches PHP.

Step 5: Connect nginx to PHP

Ubuntu's default nginx site includes a PHP block that is commented out. Open the default site configuration:

sudo nano /etc/nginx/sites-available/default

Find the index line and put index.php first, so PHP's index file is used when a folder is requested:

    index index.php index.html index.htm;

Then find the commented PHP block and remove the # characters at the start of its lines, so it looks like this:

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

This block tells nginx to send any request ending in .php to PHP-FPM. Save the file, then test and reload:

sudo nginx -t
sudo systemctl reload nginx
nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 6: Check PHP is running

Create a test file in the web root. The default root on Ubuntu is /var/www/html.

sudo nano /var/www/html/info.php
<?php phpinfo(); ?>

Visit http://203.0.113.10/info.php in your browser. A page titled "PHP Version 8.3" means nginx and PHP-FPM are cooperating. Delete the file right away:

sudo rm /var/www/html/info.php
Careful: The phpinfo page exposes server details. Delete it as soon as you have checked it.

Troubleshooting

  • 502 Bad Gateway. nginx cannot reach PHP-FPM. Check that ls /run/php/ shows the socket and that php8.3-fpm is running. Confirm the socket path in fastcgi_pass matches the file name exactly.
  • The PHP file downloads or displays as text. The location ~ \.php$ block is still commented out, or the include line is missing. Re-check Step 5.
  • nginx -t fails. The error names the file and line. Common causes are a missing semicolon or a block that was uncommented partly but not fully.
  • Pages load on the IP address but not your domain. Your domain's DNS record does not point at this VPS yet, or the server block lacks a matching server_name.

Next steps

Harden MariaDB with Secure your MariaDB installation, then create an application database with Create a database and a dedicated user. When your application is ready, see Deploy a PHP application for the file layout and permissions that keep the app safe.

Something not working? Chat or email our support team and we will help you get unstuck.

Contact support