Thursday, July 17, 2025
Linux Tutorial

How to Install LEMP Stack on Ubuntu 20.04 – 22.04

LEMP stack (Linux, Nginx, MySQL, PHP) is a powerful alternative to LAMP, offering better performance for high-traffic websites. In this guide, we’ll walk you through setting up a LEMP stack on Ubuntu 20.04 step by step.

Prerequisites

Before starting, ensure you have:

  1. Ubuntu 20.04 server (with sudo access)
  2. Firewall enabled (ufw)
  3. Domain or server IP (for testing)

Step 1: Install Nginx (Web Server)

Nginx is a fast, lightweight web server. Install it with:

bash

sudo apt update

sudo apt install nginx -y

Allow Nginx Through Firewall

bash
sudo ufw allow 'Nginx HTTP'

sudo ufw status  # Verify the rule is active

Test Nginx Installation

Visit your server’s IP in a browser:

text
http://your_server_ip

You should see the Nginx welcome page.

Step 2: Install MySQL (Database)

MySQL stores your website data. Install it with:

bash
sudo apt install mysql-server -y

Secure MySQL Installation

Run the security script:

bash
sudo mysql_secure_installation

Follow prompts to:

  • Set a root password (recommended)
  • Remove anonymous users
  • Disable remote root login
  • Remove test databases

Test MySQL Login

bash
sudo mysql -u root -p

Type exit to quit.

Step 3: Install PHP (Processing)

PHP processes dynamic content. Install php-fpm (FastCGI) and MySQL support:

bash
sudo apt install php-fpm php-mysql -y

Verify PHP version:

bash
php -v

(Should show PHP 7.4+ on Ubuntu 20.04)

Step 4: Configure Nginx to Use PHP

1. Create a Website Directory

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

2. Create an Nginx Server Block

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

Paste this config (replace your_domain with your actual domain/IP):

nginx
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:/var/run/php/php7.4-fpm.sock;
    }

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

Save (Ctrl+X, Y, Enter).

3. Enable the Site & Test

bash
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
sudo nginx -t  # Check for errors
sudo systemctl reload nginx

Step 5: Test PHP Processing

Create a test PHP file:

bash
nano /var/www/your_domain/info.php

Add:

php
<?php phpinfo(); ?>

Visit in browser:

text
http://your_domain/info.php

You should see PHP configuration details.

Remove the file after testing (security risk):

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

Step 6 (Optional): Test MySQL Connection from PHP

1. Create a Test Database & User

bash
sudo mysql -u root -p

Run in MySQL:

sql
CREATE DATABASE example_db;
CREATE USER 'example_user'@'%' IDENTIFIED BY 'strong_password';
GRANT ALL ON example_db.* TO 'example_user'@'%';
FLUSH PRIVILEGES;
EXIT;

2. Create a PHP Script to Query MySQL

bash
nano /var/www/your_domain/test_db.php

Paste:

php
<?php
$user = "example_user";
$pass = "strong_password";
$db = "example_db";

try {
    $conn = new PDO("mysql:host=localhost;dbname=$db", $user, $pass);
    echo "<h2>Connected to MySQL successfully!</h2>";
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
?>

Visit:

text
http://your_domain/test_db.php

If successful, you’ll see a “Connected to MySQL” message.

Conclusion

You’ve successfully installed a LEMP stack on Ubuntu 20.04!

Nginx – Fast web server
MySQL – Database for dynamic content
PHP-FPM – Efficient PHP processing

Need help? Drop a comment below! A big thank you for exploring TechsBucket! Your visit means a lot to us, and we’re grateful for your time on our platform. If you have any feedback or suggestions, we’d love to hear them.

Leave a Response