Home Knowledge base Skyline Cloud How to Install a LEMP Stack (Nginx, MySQL, PHP) on Ubuntu 24.04 KNOWLEDGE BASE

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

A clear, step-by-step guide to installing and configuring a production-ready LEMP stack — Nginx, MySQL, and PHP-FPM — on Ubuntu 24.04 LTS, with a tested server block and a PHP test page.

Introduction

A LEMP stack is a group of open-source software used to serve dynamic web applications. The name is an acronym for Linux, ENginx (pronounced "Engine-X"), MySQL, and PHP. It powers a large share of the web, including WordPress, Laravel, and Magento sites.

In this tutorial you will install and configure a complete LEMP stack on Ubuntu 24.04 LTS: Nginx as the web server, MySQL as the database, and PHP-FPM to process dynamic content. By the end you will have a working server block serving a PHP page.

This guide works on any Ubuntu 24.04 server. If you are running it on a Skyline Cloud VPS or cloud server inside the Kingdom, you also get in-Kingdom data residency, PDPL/NCA alignment, and local Arabic support.

Prerequisites

Before you begin, you should have:

  • An Ubuntu 24.04 server with a non-root user that has sudo privileges.
  • A basic firewall (UFW) — covered below.
  • Optionally, a domain name pointed at your server's public IP via an A record.

If you do not have a server yet, you can create one in minutes on Skyline Cloud.

Step 1 — Updating the Package Index

Always start with an up-to-date package index so you install current versions:

sudo apt update
sudo apt upgrade -y

Step 2 — Installing Nginx

Install the Nginx web server:

sudo apt install nginx -y

If UFW is active, allow HTTP and HTTPS traffic. The Nginx Full profile opens both ports 80 and 443:

sudo ufw allow 'Nginx Full'
sudo ufw status

Verify Nginx is running:

systemctl status nginx

Visit http://your_server_ip in a browser — you should see the default Welcome to nginx! page.

Step 3 — Installing MySQL

Install the MySQL database server:

sudo apt install mysql-server -y

MySQL starts automatically. Next, run the security script to remove insecure defaults:

sudo mysql_secure_installation

You will be prompted to configure the VALIDATE PASSWORD component, remove anonymous users, disallow remote root login, and drop the test database. Answer Y to these hardening steps.

On a fresh Ubuntu install, the root MySQL user authenticates via the auth_socket plugin, so you connect without a password from the shell:

sudo mysql

It is best practice to create a dedicated application user rather than using root. From the MySQL prompt:

CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'a_strong_password';
CREATE DATABASE appdb;
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 4 — Installing PHP

Unlike Apache, Nginx has no built-in PHP processor. You install PHP-FPM (FastCGI Process Manager) and the MySQL extension:

sudo apt install php-fpm php-mysql -y

Ubuntu 24.04 ships PHP 8.3 by default. Confirm the version and FPM service name:

php -v
systemctl status php8.3-fpm

Note the socket path /run/php/php8.3-fpm.sock — you will reference it in the Nginx config.

Step 5 — Configuring Nginx to Use PHP-FPM

Create a web root and a server block for your site. Replace your_domain with your domain or server IP.

sudo mkdir -p /var/www/your_domain
sudo chown -R $USER:$USER /var/www/your_domain

Create the server block file:

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

Add the following configuration:

server {
    listen 80;
    server_name your_domain www.your_domain;
    root /var/www/your_domain;

    index index.php index.html index.htm;

    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;
    }
}

Enable the site by linking it into sites-enabled, then remove the default site so it does not conflict:

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
sudo unlink /etc/nginx/sites-enabled/default

Test the configuration for syntax errors, then reload:

sudo nginx -t
sudo systemctl reload nginx

Step 6 — Testing PHP Processing

Create a test PHP file in your web root:

nano /var/www/your_domain/info.php

Add this content:

<?php
phpinfo();

Visit http://your_domain/info.php in your browser. You should see a detailed PHP information page, confirming Nginx is correctly passing requests to PHP-FPM.

Remove the file immediately afterward — it exposes server details that attackers can use:

sudo rm /var/www/your_domain/info.php

Step 7 — Securing with HTTPS (Recommended)

For any public site you should serve traffic over HTTPS. If your domain points at the server, install Certbot and obtain a free Let's Encrypt certificate:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain -d www.your_domain

Certbot edits your server block, installs the certificate, and sets up automatic renewal. You can verify the renewal timer with systemctl status certbot.timer. For business sites that need an organisation-validated or wildcard certificate, see Skyline's managed SSL options.

Service Summary

Component Package Service Default Port / Socket
Web server nginx nginx 80 / 443
Database mysql-server mysql 3306 (localhost)
PHP processor php-fpm php8.3-fpm /run/php/php8.3-fpm.sock

Conclusion

You now have a working LEMP stack on Ubuntu 24.04 — Nginx serving requests, PHP-FPM processing dynamic code, and MySQL storing your data, secured behind UFW and HTTPS. From here you can deploy WordPress, a Laravel app, or any PHP project into /var/www/your_domain.

If you would rather not manage the operating system yourself, a managed platform or business email hosting can offload that work. To explore VPS sizing, pricing, and the wider product range, visit the VPS & cloud servers hub.

Ready to deploy? Spin up an in-Kingdom cloud server with Skyline Cloud and have your LEMP stack live in minutes.

SKYLINE Engineering

@skyline

The engineering team at SKYLINE Industrial Solutions. We publish field-tested guides drawn from real KSA and GCC deployments.

See author profile
SKYLINE engineering services

Need this implemented for you?

Reading is free — building it right takes a team. SKYLINE engineers ship Skyline Cloud for Aramco vendors, banks, hospitals and government agencies across Saudi Arabia. Talk to us before you start.

Aramco Approved Contractor ISO 9001 · ISO 27001 SAMA CSF aligned NCA ECC ready 247+ KSA clients

Comments

0 total · 0 threads
Be the first to leave a comment.