Website Vidya
DeploymentStep-by-Step Guide

How to Install LEMP Stack on Ubuntu 24.04

Learn how to install lemp stack on ubuntu 24.04 with Nginx web server, MariaDB database, PHP 8.3 FPM, UFW firewall, and server block configurations.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
How to Install LEMP Stack on Ubuntu 24.04
AI Executive SummaryCORE CIS AI

5 min read

Learn how to install lemp stack on ubuntu 24.04 with Nginx web server, MariaDB database, PHP 8.3 FPM, UFW firewall, and server block configurations.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Installing and Configuring Nginx Web Server
  • Step 2: Installing and Securing the MariaDB Database Engine
Grounded in full engineering runbookCORE CIS Ai Engine

Interactive Builder for this Guide

Build your custom nginx syntax (How to Install LEMP Stack on Ubuntu 24.04)

Generated Config Output
server {
    listen 80;
    listen [::]:80;
    server_name app.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name app.example.com;

    ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript application/xml;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
Production Nginx reverse proxy -> 127.0.0.1:3000 for app.example.com.
Flag-by-Flag Breakdown
server_nameBinds this virtual host to app.example.com
return 301 https://...Redirects all plain HTTP requests permanently to TLS/HTTPS
proxy_pass http://127.0.0.1:3000Forwards HTTP + WebSocket requests to local application server
X-Content-Type-Options / X-Frame-OptionsProtects users against MIME sniffing and clickjacking

The LEMP stack—comprising Linux (operating system), Nginx (pronounced 'Engine-X', high-performance web server), MariaDB or MySQL (relational database), and PHP (server-side scripting language)—is the premier open-source architecture for hosting high-concurrency web applications, APIs, and content management systems.

Compared to traditional LAMP stacks using Apache, Nginx's asynchronous event-driven architecture handles thousands of concurrent connections with exceptionally low memory usage, making LEMP the preferred choice for performance-focused sites.

Ubuntu 24.04 LTS (Noble Numbat) serves as a rock-solid foundation for LEMP, shipping natively with Nginx 1.24+, MariaDB 10.11+ (or MySQL 8.0), and PHP 8.3.

In this tutorial, we walk through how to install lemp stack on ubuntu from bare server configuration to rendering dynamic PHP applications with SSL readiness.

Advertisement

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

  • A server running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
  • An unprivileged user account with sudo administrative privileges.
  • A registered domain name pointing to your server's public IP address (recommended).
  • UFW firewall installed and active.

Step 1: Installing and Configuring Nginx Web Server

Nginx acts as the entrypoint for incoming HTTP and HTTPS traffic, serving static assets directly and proxying dynamic requests to PHP-FPM.

First, update your local APT repository index, then install the nginx package:

Advertisement

Allow web traffic through UFW and verify that Nginx is running:

# Update package lists and install Nginx:
sudo apt update
sudo apt install -y nginx

# Allow HTTP and HTTPS traffic through UFW firewall:
sudo ufw allow 'Nginx Full'

# Verify Nginx service status:
sudo systemctl status nginx
# Expected output confirms: Active: active (running)

Step 2: Installing and Securing the MariaDB Database Engine

MariaDB is a high-performance, drop-in open-source replacement for MySQL developed by MySQL's original creators.

Install the mariadb-server package using apt:

Execute the automated security script to set a database root password and remove insecure default privileges:

# Install MariaDB server:
sudo apt install -y mariadb-server

# Run interactive security hardening script:
sudo mariadb-secure-installation
# Recommendations during prompts:
# - Switch to unix_socket authentication? [Y]
# - Change root password? [Y] -> Set strong password
# - Remove anonymous users? [Y]
# - Disallow root login remotely? [Y]
# - Remove test database and access to it? [Y]
# - Reload privilege tables now? [Y]

Step 3: Installing PHP 8.3 and the FPM Processor

Unlike Apache, which can embed PHP directly as a module, Nginx requires an external fastCGI process manager (php-fpm) to execute PHP scripts.

Advertisement

Ubuntu 24.04 provides PHP 8.3 by default.

Install php8.3-fpm along with the php-mysql database bridge extension and common web libraries:

# Install PHP-FPM and essential database/web modules:
sudo apt install -y php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip

# Verify PHP-FPM service status:
sudo systemctl status php8.3-fpm
# Expected output confirms: Active: active (running)

Step 4: Configuring an Nginx Server Block for PHP Processing

Best practice dictates creating a dedicated server block configuration for your domain rather than editing the default configuration.

Create the web root directory at /var/www/example.com/html and assign ownership to the web user (www-data):

Create the server block configuration file in /etc/nginx/sites-available/example.com:

# Create website document root:
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:www-data /var/www/example.com/html

# /etc/nginx/sites-available/example.com
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

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

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

Enabling the Server Block and Reloading Nginx

Enable the server block by creating a symbolic link in /etc/nginx/sites-enabled/:

Validate configuration syntax and reload Nginx:

# Create symlink in sites-enabled:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

# Test Nginx syntax configuration:
sudo nginx -t
# Output confirms: syntax is ok, test is successful

# Reload Nginx service:
sudo systemctl reload nginx

Step 5: Testing PHP Processing in Nginx

Verify that Nginx communicates correctly with PHP-FPM by creating a diagnostic info.php file in your document root:

Query the page via curl or your web browser to confirm PHP execution:

# Create info.php test file:
echo '<?php phpinfo(); ?>' > /var/www/example.com/html/info.php

# Test HTTP response via curl:
curl -I http://example.com/info.php
# Expected output confirms: HTTP/1.1 200 OK

# For security, remove info.php after testing:
rm -f /var/www/example.com/html/info.php

Step 6: Securing the LEMP Stack with Let's Encrypt SSL

With Nginx, MariaDB, and PHP-FPM operating harmoniously, secure your website with free, automated TLS encryption from Let's Encrypt.

Install Certbot along with its official Nginx automation plugin using apt:

Run the automated certificate generator to configure HTTPS redirection and test renewal:

# Install Certbot and Python Nginx plugin:
sudo apt install -y certbot python3-certbot-nginx

# Request and install SSL certificate automatically:
sudo certbot --nginx -d example.com -d www.example.com

# Test automatic renewal timer:
sudo certbot renew --dry-run

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

502 Bad Gateway error when loading PHP scripts in Nginx

Cause: The PHP-FPM service is stopped or the fastcgi_pass socket path does not match the active PHP version.

Resolution: Verify PHP-FPM is running (sudo systemctl status php8.3-fpm) and ensure the socket in /etc/nginx/sites-available/ is unix:/run/php/php8.3-fpm.sock.

sudo systemctl restart php8.3-fpm && sudo systemctl reload nginx

PHP files downloaded as plain text instead of executing in browser

Cause: Missing location ~ \.php$ block in Nginx server block configuration.

Resolution: Add the fastcgi configuration block to your Nginx configuration and reload Nginx.

sudo nginx -t && sudo systemctl reload nginx

MariaDB access denied for user 'root'@'localhost'

Cause: MariaDB uses the unix_socket authentication plugin by default on Ubuntu.

Resolution: Log in with administrative sudo privileges: sudo mariadb -u root.

sudo mariadb -u root

Security Best Practices and Hardening

Never leave phpinfo() files accessible on public production servers; they disclose internal module paths, environment secrets, and operating system versions.

Set cgi.fix_pathinfo = 0 in /etc/php/8.3/fpm/php.ini to prevent malicious script injection vulnerabilities.

Obtain a free Let's Encrypt TLS certificate using Certbot: sudo apt install -y certbot python3-certbot-nginx && sudo certbot --nginx -d example.com.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
LEMP uses Nginx as the web server, which uses an event-driven non-blocking I/O architecture that handles high traffic with minimal memory overhead. LAMP uses Apache, which uses process- or thread-based request handling that consumes more RAM per concurrent visitor.
Yes. MariaDB and MySQL share the same protocol, ports (3306), and SQL syntax. You can install standard MySQL using sudo apt install mysql-server instead of MariaDB without changing Nginx or PHP-FPM settings.
Nginx's default installation web root is located at /var/www/html/, and custom site blocks are typically created under /var/www/domain.com/html/.
Install Certbot and the Nginx plugin with sudo apt install certbot python3-certbot-nginx, then run sudo certbot --nginx -d yourdomain.com to automatically issue and configure Let's Encrypt TLS certificates.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Uncategorized•

How to Redirect HTTP to HTTPS in Nginx

If you’re running a website today and it’s still accessible over plain HTTP, you’re already behind. Modern browsers flag non-HTTPS sites as “Not Secure,” search engines favor encrypted websites, and users are far more cautious about where t...

RR
Rajesh Rai
Read More
Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout