How to install LEMP stack on Ubuntu 24.04

The LEMP (Linux, Nginx, MySQL/MariaDB, PHP) stack is a group of softwares that can be installed on your new virtual machine to serve dynamic web content. Here's a step-by-step tutorial on installing the stack on Ubuntu 24.04:

Step 1: Update Package Indexes

First, update your package indexes to ensure you have the latest versions of all packages:


    sudo apt update

Step 2: Install Nginx

Nginx is a high-performance web server. Install it using the following commands:


sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx

You can verify the installation by navigating to http://your_server_ip in your web browser. You should see the Nginx welcome page.

Step 3: Install MariaDB

MariaDB is a popular open-source database server. Install it with:


sudo apt install mariadb-server
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo systemctl status mariadb

Secure your MariaDB installation:


sudo mysql_secure_installation

Follow the prompts to set a root password and secure your database.

Step 4: Install PHP

PHP is a server-side scripting language used for web development. Install PHP and necessary modules:


sudo apt install php php-fpm php-mysql

Verify the installation:


php --version

You should see the PHP version installed on your system.

Step 5: Configure Nginx to Use PHP

Create a configuration file for your site:

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

Add the following configuration:


server {
    listen 80;
    server_name 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/php-fpm.sock;
    }
    
    location ~ /\.ht {
        deny all;
    }
}

Enable the configuration and restart Nginx:


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

Step 6: Test PHP

Create a test PHP file:

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

Add the following content:



Save and exit. Then, navigate to http://example.com/info.php in your web browser. You should see the PHP information page.