LochStudios  /  Help Centre  /  VPS & Linux  /  Install a LEMP Stack (Nginx, MariaDB, PHP-FPM)

Install a LEMP Stack (Nginx, MariaDB, PHP-FPM)

Set up the complete LEMP stack (Linux, Nginx, MariaDB, PHP) to run dynamic web applications.

Updated

LEMP is a popular stack for running dynamic web applications: Linux, ENginx (web server), MariaDB (database), and PHP (server-side language). This guide installs all four components.

Step 1: Update your package manager

sudo apt update

Step 2: Install Nginx

sudo apt install -y nginx

Start and enable it:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 3: Install MariaDB

sudo apt install -y mariadb-server

Start and enable it:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Run the secure installation script:

sudo mariadb-secure-installation

Follow the prompts:
- Press Enter for no root password (it uses Unix socket auth)
- Type Y to disable anonymous users
- Type Y to disable root remote login
- Type Y to remove test databases
- Type Y to reload privilege tables

Step 4: Install PHP and PHP-FPM

sudo apt install -y php-fpm php-mysql php-mbstring php-xml php-curl

The exact version depends on your Ubuntu/Debian release (e.g., php8.3-fpm on Ubuntu 24.04).

Start and enable PHP-FPM:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Check which PHP-FPM socket you have:

ls -la /run/php/

You should see php*-fpm.sock (e.g., php8.3-fpm.sock).

Step 5: Configure Nginx to use PHP-FPM

Edit your Nginx server block (replace yourdomain.com and the socket path):

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

Use this configuration:

server {
    listen 80;
    listen [::]:80;

    server_name yourdomain.com www.yourdomain.com;

    root /var/www/yourdomain.com/html;
    index index.php index.html index.htm;

    access_log /var/www/yourdomain.com/logs/access.log;
    error_log /var/www/yourdomain.com/logs/error.log;

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

(Replace php8.3-fpm.sock with the socket you found in Step 4.)

Step 6: Test and reload Nginx

Check the configuration:

sudo nginx -t

Reload Nginx:

sudo systemctl reload nginx

Step 7: Create your website directory

sudo mkdir -p /var/www/yourdomain.com/html
sudo mkdir -p /var/www/yourdomain.com/logs
sudo chown -R www-data:www-data /var/www/yourdomain.com/html

Step 8: Test PHP

Create a test PHP file:

sudo bash -c 'cat > /var/www/yourdomain.com/html/info.php << "EOF"
<?php
phpinfo();
?>
EOF'

Visit http://yourdomain.com/info.php in your browser. You should see the PHP information page.

Delete this file after testing (it's a security risk):

sudo rm /var/www/yourdomain.com/html/info.php

Step 9: Create a database and user

Connect to MariaDB:

sudo mariadb

Create a database (replace mydatabase and myuser with your values):

CREATE DATABASE mydatabase;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Your application can now connect with username myuser, password mypassword, and database mydatabase.

Common commands

Restart all services:

sudo systemctl restart nginx mariadb php-fpm

Check MariaDB status:

sudo systemctl status mariadb

View MariaDB logs:

sudo journalctl -u mariadb -n 20

Tips

  • Use HTTPS (Let's Encrypt) for production sites
  • Disable the info.php test file and keep your WordPress/Laravel versions updated
  • Set up automated database backups
  • Monitor disk space: df -h
  • Monitor memory: free -h

Was this article helpful?

← Back to VPS & Linux