Ubuntu LEMP Installation
Server Management

Installing Nginx, MariaDB and PHP (LEMP) on Ubuntu

Install Nginx, MariaDB and PHP-FPM (LEMP) on an Ubuntu 22.04/24.04 VDS. Server block configuration and PHP testing, step by step.

  • MerkezHost Technical Team
  • 3 min read
  • Level: Intermediate
  • Updated:

The LEMP stack consists of Linux, Engine-X (Nginx), MariaDB/MySQL and PHP, and is one of the most popular ways to host PHP-based web applications. In this guide we'll install LEMP step by step on an Ubuntu 22.04/24.04 VDS server.

Requirements

  • A VDS server running Ubuntu 22.04 or 24.04.
  • A user with sudo privileges.
  • A domain pointed to the server (optional but recommended).

Step 1: Update the system

sudo apt update && sudo apt -y upgrade

Step 2: Install Nginx

sudo apt install -y nginx
sudo systemctl enable --now nginx

Allow web traffic in the firewall:

sudo ufw allow 'Nginx Full'

When you open your server's IP address in a browser, you should see the Nginx welcome page.

Step 3: Install MariaDB

sudo apt install -y mariadb-server
sudo systemctl enable --now mariadb
sudo mysql_secure_installation

In the security wizard, confirm removing anonymous users, disabling remote root login and deleting the test database.

Create a database for your application

sudo mariadb
CREATE DATABASE uygulama CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'uygulama_kullanici'@'localhost' IDENTIFIED BY 'GucluBirSifre!';
GRANT ALL PRIVILEGES ON uygulama.* TO 'uygulama_kullanici'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 4: Install PHP-FPM

Nginx runs PHP files through PHP-FPM. Install PHP and common extensions:

sudo apt install -y php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip php-intl
php -v

Note the version in the command output (for example 8.3); we'll use the socket file for this version in the Nginx configuration.

Step 5: Configure the Nginx server block

Create a directory for your site:

sudo mkdir -p /var/www/alanadiniz.com
sudo chown -R $USER:www-data /var/www/alanadiniz.com

Open a new configuration file:

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

Add the following content (adjust the php8.3-fpm.sock part to match your PHP version):

server {
    listen 80;
    server_name alanadiniz.com www.alanadiniz.com;
    root /var/www/alanadiniz.com;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

    location ~ /\.(?!well-known) {
        deny all;
    }
}

Enable the site, test the configuration and reload Nginx:

sudo ln -s /etc/nginx/sites-available/alanadiniz.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 6: Test PHP

echo "<?php phpinfo();" | sudo tee /var/www/alanadiniz.com/info.php

Open http://alanadiniz.com/info.php in your browser. If you see the PHP information page, the installation was successful.

Warning

The info.php file reveals sensitive information about your server. Be sure to delete it after testing: sudo rm /var/www/alanadiniz.com/info.php

Step 7: Add SSL

Finally, move your site to HTTPS. For step-by-step instructions, see our Let's Encrypt SSL installation guide.

Conclusion

Your LEMP server is ready to host WordPress, Laravel or any PHP application. For high-performance infrastructure, take a look at our NVMe SSD VDS servers.

Frequently Asked Questions

What is the difference between LEMP and LAMP?

LAMP uses the Apache web server, while LEMP uses Nginx. Nginx consumes less memory under high numbers of concurrent connections.

Which PHP version should I use?

The default PHP version in the Ubuntu repositories receives security updates and is sufficient for most projects. Choose the latest version your application supports.

  • #nginx
  • #mariadb
  • #php
  • #lemp
  • #ubuntu
  • #web server
Share:
Read More

Related Guides