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.

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.
- Prerequisites and Supported Releases
- Step 1: Installing and Configuring Nginx Web Server
- Step 2: Installing and Securing the MariaDB Database Engine
Interactive Builder for this Guide
Build your custom nginx syntax (How to Install LEMP Stack on Ubuntu 24.04)
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";
}
}server_nameBinds this virtual host to app.example.comreturn 301 https://...Redirects all plain HTTP requests permanently to TLS/HTTPSproxy_pass http://127.0.0.1:3000Forwards HTTP + WebSocket requests to local application serverX-Content-Type-Options / X-Frame-OptionsProtects users against MIME sniffing and clickjackingThe 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.
Complete step-by-step developer and system administrator tutorial on installing and securing the high-performance LEMP (Linux, Nginx, MariaDB/MySQL, PHP) web stack on Ubuntu 24.04 LTS (Noble Numbat). Explains installing Nginx, securing MariaDB with mariadb-secure-installation, configuring PHP 8.3 FPM fastCGI processing, authoring custom server blocks, and validating PHP execution.
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
sudoadministrative 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:
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.
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.
Related Ubuntu Tutorials and Documentation
Deepen your Linux system administration skills with our related production-tested guides:
- How to Install and Secure MariaDB on Ubuntu 24.04 — In-depth guide on tuning MariaDB performance and user privileges.
- How to Upgrade PHP 8.2 to 8.3 in Ubuntu — Manage multiple PHP runtimes and extension migrations.
- How to Set Up a Firewall with UFW on Ubuntu 24.04 — Configure UFW rules for HTTP (80) and HTTPS (443) web traffic.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
sudo apt install mysql-server instead of MariaDB without changing Nginx or PHP-FPM settings./var/www/html/, and custom site blocks are typically created under /var/www/domain.com/html/.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.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
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.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides
Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!