Operating System Ubuntu

How to Install the LEMP Stack (Nginx, MariaDB, PHP) on Ubuntu 24.04 LTS

Install Nginx, MariaDB and PHP-FPM on Ubuntu 24.04 LTS, connect them in a server block for your domain and verify PHP and the database.

A LEMP stack is Linux, Nginx (pronounced "engine-x", hence the E), a MySQL-compatible database and PHP. This guide installs Nginx, MariaDB and PHP-FPM on Ubuntu 24.04 LTS from the standard Ubuntu repositories and connects them, so that you finish with a server block for your domain that executes PHP and a database ready for an application.

Ubuntu 18.04, which older tutorials covered, is past its standard support period. The same steps apply to Ubuntu 22.04 and Debian 12; only the PHP version number in service and socket names differs, and the guide shows how to check it.

Requirements

  • A server running Ubuntu 24.04 LTS.
  • A user with sudo rights (the examples use deploy).
  • A domain name such as example.com pointing to the server (for example 203.0.113.10). You can test by IP address without one.
  • No other web server listening on port 80. If Apache is installed, stop and disable it first.

Step 1: Install Nginx

sudo apt update
sudo apt install -y nginx

The package starts Nginx and enables it at boot. Confirm it is running:

sudo systemctl status nginx

If the UFW firewall is in use, allow SSH and both web ports. The Nginx Full profile opens 80 and 443:

sudo ufw allow OpenSSH
sudo ufw allow "Nginx Full"
sudo ufw status

Open http://203.0.113.10/ (your own address) in a browser; the "Welcome to nginx!" page should appear.

Step 2: Install and secure MariaDB

Ubuntu 24.04 packages MariaDB 10.11, a long-term support release, so there is no need to add an external repository:

sudo apt install -y mariadb-server

Run the hardening script:

sudo mariadb-secure-installation

Press Enter for the current root password. MariaDB authenticates the root account through the local Unix socket by default, so you can keep that and skip setting a root password. Answer Y to removing anonymous users, disallowing remote root logins, removing the test database and reloading privileges. The script's older name, mysql_secure_installation, still works.

Open a database shell as root:

sudo mariadb

Create a database and an application user with its own password:

CREATE DATABASE appdb CHARACTER SET utf8mb4;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'change-this-password';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
EXIT;

Step 3: Install PHP-FPM

Nginx does not run PHP itself; it passes requests to the PHP FastCGI Process Manager. Install it with the MySQL/MariaDB driver:

sudo apt install -y php-fpm php-mysql

On Ubuntu 24.04 this installs PHP 8.3. Check which socket PHP-FPM created, because you need its exact name in the Nginx configuration:

ls /run/php/

You should see php8.3-fpm.sock (Debian 12 shows php8.2-fpm.sock). The service carries the same version number:

sudo systemctl status php8.3-fpm

Applications usually need more extensions. A common set:

sudo apt install -y php-curl php-gd php-mbstring php-xml php-zip php-intl
sudo systemctl restart php8.3-fpm

Step 4: Configure an Nginx server block for PHP

Create a document root for the site:

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

Create the server block file:

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

Add the following, adjusting the domain and, if needed, the socket name:

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/ =404;
    }

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

    location ~ /\.ht {
        deny all;
    }
}

The snippets/fastcgi-php.conf file ships with the Ubuntu Nginx package. It sets the FastCGI parameters and returns 404 for PHP paths that do not exist as files instead of passing them to PHP. Many applications (WordPress, Laravel) need try_files $uri $uri/ /index.php?$args; in the location / block instead of =404; follow your application's documentation.

Enable the site, test the syntax 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 testing by IP address without a domain, also remove the default site so that your server block answers: sudo rm /etc/nginx/sites-enabled/default, then reload again.

Step 5: Test PHP and the database connection

Create a PHP info page:

echo '<?php phpinfo();' > /var/www/example.com/info.php

Open http://example.com/info.php. The page should report "FPM/FastCGI" as the Server API and list mysqli and pdo_mysql. To confirm that the database account works, connect with it from the shell:

mariadb -u appuser -p appdb -e "SELECT VERSION();"

Delete the info page once you have checked it:

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

Maintenance and troubleshooting

  • 502 Bad Gateway: Nginx cannot reach PHP-FPM. Check that the service is running and that the fastcgi_pass path matches the file in /run/php/ exactly.
  • PHP files download instead of running: the request is not hitting your location ~ \.php$ block. Check that the site is linked in sites-enabled and that server_name matches the host you requested.
  • Logs: Nginx writes to /var/log/nginx/access.log and error.log; PHP-FPM logs to /var/log/php8.3-fpm.log.
  • PHP settings: edit /etc/php/8.3/fpm/php.ini (upload limits, memory limit) and restart php8.3-fpm. The command-line PHP uses a separate file, /etc/php/8.3/cli/php.ini.
  • Updates: sudo apt update && sudo apt upgrade keeps all three components patched. After a release upgrade that changes the PHP version, update the socket name in every server block.

Reference: Ubuntu Server: configure nginx and the Nginx FastCGI module documentation.

Frequently asked questions

Should I add the MariaDB or a third-party PHP repository to get newer versions?

Only if your application requires a version Ubuntu does not ship. The distribution packages receive security updates through apt for the life of the release; external repositories add a second update source you have to track and can complicate release upgrades.

Do I still need to set cgi.fix_pathinfo=0 in php.ini?

Not with the configuration above. That setting was a workaround for Nginx passing non-existent paths to PHP. The fastcgi-php.conf snippet checks that the script file exists before handing the request over, which closes the same hole.

Can I use MySQL instead of MariaDB?

Yes. Install mysql-server instead of mariadb-server; the two cannot be installed together. The php-mysql package and the Nginx configuration stay the same.

How do I serve the site over HTTPS?

Install Certbot with its Nginx plugin and request a Let's Encrypt certificate for your domain. Certbot adds the listen 443 ssl configuration to the server block and sets up automatic renewal. The firewall rule from Step 1 already allows port 443.

Share story:

Continue reading other guides on How VPS.