How to Install LEMP Stack on Ubuntu

The LEMP stack is a popular software bundle for hosting dynamic websites and applications, consisting of Linux as the operating system, Nginx as a high-performance web server, MySQL for database management, and PHP as the server-side scripting language. In this beginner-friendly guide, we’ll walk you step by step through how to install LEMP on Ubuntu 20.04, making it easy to follow even if you’re new to Linux or servers. Whether you’re setting up a LEMP server on Ubuntu 20.04 or planning to upgrade to LEMP Ubuntu 22.04 in the future, this tutorial will give you the right foundation.

Prerequisites

Before we start installing the Ubuntu LEMP stack, you’ll need:

  • An Ubuntu 20.04 server (works for Ubuntu 22.04 as well).
  • A non-root sudo user.
  • A firewall is enabled for security.

Steps to Install LEMP Stack on Ubuntu

Step 1: Installing Nginx Web Server

To serve web pages, we’ll install Nginx, a lightweight and fast web server:

sudo apt update
sudo apt install nginx

After installation, open your browser and visit your server’s IP. If you see the default Nginx welcome page, your web server is running successfully.

Step 2: Installing PHP

Nginx doesn’t process PHP by itself, so we’ll install PHP-FPM (FastCGI Process Manager) and php-mysql to connect PHP with MySQL:

sudo apt install php-fpm php-mysql

This ensures that PHP scripts can run and interact with the database.

Step 3: Installing MySQL Database

Now let’s install MySQL to store and manage your website data:

sudo apt install mysql-server

Secure your installation:

sudo mysql_secure_installation

Test the connection:

sudo mysql

install mysql

Exit MySQL with:

exit;

Pro tip: Always use strong passwords and create dedicated database users for each application.

Step 4: Configuring Nginx to Use PHP

Next, we’ll configure Nginx to process PHP files.

  1. Create a directory for your website:
sudo mkdir /var/www/your_domain
sudo chown -R $USER:$USER /var/www/your_domain
  1. Create a new Nginx server block:
sudo nano /etc/nginx/sites-available/your_domain
  1. Add this configuration:
server {
    listen 80;
    server_name your_domain www.your_domain;
    root /var/www/your_domain;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

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

    location ~ /\.htaccess {
        deny all;
    }
}
  1. Enable the new site and disable the default:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
sudo unlink /etc/nginx/sites-enabled/default
  1. Test configuration and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx

Now your LEMP server on Ubuntu is serving content from /var/www/your_domain.

Step 5: Testing PHP with Nginx

Create a test file to confirm PHP is working:

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

Insert:

<?php
phpinfo();

Visit http://your_domain/info.php in your browser. If you see PHP information details, your LEMP stack Ubuntu installation is working.

Don’t forget to remove the file afterward:

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

Step 6: Testing Database Connection with PHP

  1. Create a database and user:
CREATE DATABASE example_database;
CREATE USER 'example_user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL ON example_database.* TO 'example_user'@'%';
exit;
  1. Create a PHP script to connect and display data:
nano /var/www/your_domain/quote_list.php
<?php
$user = "example_user";
$password = "password";
$database = "example_database";
$table = "quote_list";

try {
    $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
    echo "<h2>Quotes</h2><ol>";
    foreach($db->query("SELECT content FROM $table") as $row) {
        echo "<li>" . $row['content'] . "</li>";
    }
    echo "</ol>";
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>
  1. Access it in your browser: http://your_domain/quote_list.php
    test database connection
    If it displays data, your LEMP Linux environment is functioning properly.

Conclusion

Congratulations! You have successfully installed the LEMP stack on Ubuntu 20.04.

Now you can:

  • Host PHP applications with Nginx
  • Manage data with MySQL
  • Run everything smoothly on your LEMP Ubuntu server

This setup is secure, efficient, and ready for production. If you’re planning to upgrade, the same steps apply when you install LEMP on Ubuntu 22.04.

Keywords:

lemp stack, lemp server, lemp ubuntu, lnmp stack, install lemp, install lemp ubuntu, install lemp ubuntu 22.04, install lemp on ubuntu 22.04, lemp linux, lemp stack ubuntu, lemp stack ubuntu 22.04, lemp ubuntu 22.04, ubuntu lemp stack, install lemp ubuntu 20.04

Outline