UbuntuUbuntu Based

How To Install LEMP Stack on Ubuntu 24.04 LTS

Install LEMP Stack on Ubuntu 24.04

In this tutorial, we will show you how to install LEMP stack on Ubuntu 24.04 LTS. The LEMP stack is a powerful combination of open-source software that enables you to run dynamic websites and web applications. It consists of the Linux operating system, Nginx (pronounced “engine-x”) web server, MariaDB database server, and PHP programming language.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the LEMP stack on Ubuntu 24.04 (Noble Numbat). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies.
  • An Ubuntu 24.04 system with root access or a user with sudo privileges.

Install LEMP Stack on Ubuntu 24.04 LTS Noble Numbat

Step 1. Updating the Package Repository.

It’s also recommended to update your system’s package index and upgrade any available packages to their latest versions. You can do this by running the following commands:

sudo apt update
sudo apt upgrade

The sudo apt update command refreshes the package list, fetching information about the newest versions of packages and their dependencies. This step helps the package manager determine which packages need to be upgraded.

Step 2. Installing Nginx on Ubuntu.

Nginx is the first component we’ll install in our LEMP stack. It will handle incoming HTTP requests and serve web content to clients.

Open your terminal and run the following command to install Nginx:

sudo apt install nginx

Once the installation is complete, start the Nginx service and enable it to start automatically on system boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Adjust the firewall settings to allow Nginx traffic. If you have the UFW (Uncomplicated Firewall) enabled, run the following command:

sudo ufw allow 'Nginx Full'

Verify that Nginx is running correctly by visiting your server’s IP address or domain name in a web browser. You should see the default Nginx welcome page.

Step 3. Installing MariaDB Database Server.

Next, we’ll install MariaDB, the database management system that will store and manage data for your web applications.

Install MariaDB with the following command:

sudo apt install mariadb-server

After the installation is complete, run the mysql_secure_installation script to secure your MariaDB installation:

sudo mysql_secure_installation

This script will prompt you to set a root password for MariaDB, remove anonymous users, disable remote root login, remove the test database, and reload privilege tables. Follow the prompts and choose secure options.

To test your MariaDB installation, log in to the MariaDB shell:

sudo mysql -u root -p

Enter the root password you set during the mysql_secure_installation script. Once logged in, you can create a new database and user for your web application.

Step 4. Installing PHP.

PHP is the programming language that will power your dynamic web applications. We’ll install PHP 8.3 along with the necessary extensions for interacting with MariaDB and Nginx.

Install PHP and the required extensions with the following command:

sudo apt install php-fpm php-mysql php-curl php-gd php-json php-intl php-bcmath php-opcache php-apcu php-mbstring php-fileinfo php-xml php-soap php-tokenizer php-zip

Verify the PHP installation by checking the version:

php -v

Step 5. Configure Nginx to Use a PHP Processor.

Now that we have all the components installed, we need to configure Nginx to use PHP for processing dynamic content.

Create a new server block configuration file for your domain or website:

sudo nano /etc/nginx/sites-available/example.com

Paste the following configuration into the file, replacing example.com with your domain or server’s IP address:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com;
    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/php8.3-fpm.sock;
    }

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

Save the file exit the text editor and enable the server block by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Test the Nginx configuration for syntax errors:

sudo nginx -t

If no errors are reported, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 6. Testing PHP with Nginx.

To ensure that PHP is working correctly with Nginx, we’ll create a simple PHP info file. Now create a new file in your web root directory:

sudo nano /var/www/example.com/info.php

Add the following PHP code to the file:

<?php
phpinfo();
?>

Save the file and exit the text editor, the visit http://example.com/info.php in your web browser. You should see the PHP information page, confirming that PHP is working correctly with Nginx.

Step 7. Securing the LEMP Stack.

While the LEMP stack is now installed and functional, it’s recommended to take additional security measures to harden your server.

Set up UFW (Uncomplicated Firewall) rules to restrict access to your server:

sudo ufw allow OpenSSH
sudo ufw enable

Configure SSL/TLS encryption with Let’s Encrypt for secure communication:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

Enable automatic renewal of SSL certificates:

sudo systemctl status certbot.timer

Step 8. Additional Configurations and Optimizations

Depending on your specific requirements, you may want to consider the following additional configurations and optimizations:

  • Enabling Gzip compression in Nginx: Add the following lines to your Nginx configuration file to enable Gzip compression for improved performance:
gzip on;
gzip_types text/plain application/xml text/css text/javascript application/javascript;
  • Configuring caching with Nginx: Implement caching mechanisms to reduce server load and improve response times for static content.
  • Setting up a swap file: If your server has limited RAM, creating a swap file can help improve performance by allowing the system to use disk space as virtual memory.

Congratulations! You have successfully installed LEMP. Thanks for using this tutorial for installing the LEMP stack on the Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the Ubuntu website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button