LEMP (Linux, Engine-X/Nginx, MySQL or MariaDB, PHP) is the most popular stack for PHP applications on a VPS: WordPress, Laravel, Drupal, Nextcloud and countless custom apps. This guide installs and connects all four parts on Ubuntu 24.04, ready for a real site.

Prefer Apache? See install a LAMP stack. Not sure which? See LAMP vs LEMP.

Before you start

1. Install Nginx

sudo apt update
sudo apt install -y nginx
sudo ufw allow 'Nginx Full'
systemctl status nginx

Visit http://your-server-ip to see the Nginx welcome page. See install Nginx on Ubuntu.

2. Install MariaDB (or MySQL)

sudo apt install -y mariadb-server
sudo mysql_secure_installation

Answer: set or keep root authentication via unix_socket, remove anonymous users, disallow remote root login, remove the test database, and reload privileges.

Create a database and user for your site:

sudo mariadb
CREATE DATABASE example CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'example'@'localhost' IDENTIFIED BY 'a-strong-password';
GRANT ALL PRIVILEGES ON example.* TO 'example'@'localhost';
FLUSH PRIVILEGES;

See MySQL users and privileges. To use MySQL 8 instead: sudo apt install mysql-server.

3. Install PHP-FPM

Ubuntu 24.04 ships PHP 8.3:

sudo apt install -y php8.3-fpm php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring \
  php8.3-xml php8.3-zip php8.3-intl php8.3-bcmath php8.3-opcache
systemctl status php8.3-fpm

Add other extensions your app needs. See PHP extensions and PHP versions on Ubuntu.

4. Configure an Nginx server block

sudo mkdir -p /var/www/example.com/public
sudo chown -R $USER:www-data /var/www/example.com
# /etc/nginx/sites-available/example.com
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/public;
    index index.php index.html;

    client_max_body_size 64M;

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

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

    location ~ /\.(?!well-known) { deny all; }
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

See Nginx server blocks and Nginx with PHP-FPM.

5. Test PHP and the database connection

cat > /var/www/example.com/public/info.php <<'EOF'
<?php
$db = new mysqli('localhost', 'example', 'a-strong-password', 'example');
echo $db->connect_error ? 'DB error: ' . $db->connect_error : 'DB OK<br>';
phpinfo();
EOF

Open http://example.com/info.php. You should see “DB OK” and the PHP info page. Delete the file afterwards: rm /var/www/example.com/public/info.php.

6. Add HTTPS

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

Certbot edits the server block, and sets up automatic renewal. See Let’s Encrypt with Certbot.

7. Basic tuning

Next steps

Install your application: WordPress on Ubuntu with Nginx or deploy Laravel with Nginx. Set up backups for the database and files. See mysqldump.

Quick reference

Component Package / path
Nginx nginx, /etc/nginx/sites-available/
Database mariadb-server (or mysql-server)
PHP php8.3-fpm, socket /run/php/php8.3-fpm.sock
Web root /var/www/example.com/public
HTTPS certbot --nginx

Related: Ubuntu VPS and WordPress VPS hosting.

  • LEMP
  • Nginx
  • Ubuntu