Install WordPress on a LEMP stack (Nginx, MariaDB, PHP)

Build a LEMP stack on Ubuntu or Debian, create the database, configure wp-config.php and Nginx for PHP-FPM, then finish the WordPress installer over HTTPS.

  • Intermediate
  • Linux VPS
  • 3 min read

By RunRacks Team · Published

This guide installs WordPress on Ubuntu 24.04, 22.04 or Debian 12 using Nginx, MariaDB and PHP-FPM. We use example.com as the domain; its A record should already point at your server.

1. Install the packages

Terminal
sudo apt update
sudo apt install -y nginx mariadb-server php-fpm php-mysql php-curl php-gd php-intl php-mbstring php-xml php-zip

Find the PHP-FPM socket name, because it includes the PHP version (8.3 on Ubuntu 24.04, 8.1 on 22.04, 8.2 on Debian 12):

Terminal
ls /run/php/

2. Secure MariaDB and create the database

Terminal
sudo mariadb-secure-installation

Then open the database shell with sudo mariadb and run, choosing your own strong password:

SQL
CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'use-a-long-random-password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

3. Download WordPress

Terminal
cd /tmp
curl -LO https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
sudo mkdir -p /var/www/example.com
sudo cp -a wordpress/. /var/www/example.com/
sudo chown -R www-data:www-data /var/www/example.com

4. Configure wp-config.php

Terminal
cd /var/www/example.com
sudo -u www-data cp wp-config-sample.php wp-config.php
sudo -u www-data nano wp-config.php

Set DB_NAME, DB_USER and DB_PASSWORD to the values from step 2. Replace the block of put your unique phrase here keys with fresh ones from https://api.wordpress.org/secret-key/1.1/salt/.

5. Configure Nginx

Create /etc/nginx/sites-available/example.com. Change the socket in fastcgi_pass to match what ls /run/php/ showed:

NGINX
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.php;
    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;
    }
}

Enable it and reload:

Terminal
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

6. Add HTTPS, then run the installer

Get a certificate first, so your admin password is never sent over plain HTTP. Follow Secure Nginx with Let's Encrypt, then open https://example.com and complete the WordPress setup screens.

Troubleshooting

  • 502 Bad Gateway: the fastcgi_pass socket name is wrong or PHP-FPM is stopped. Compare it with ls /run/php/ and check systemctl status php8.3-fpm.
  • "Error establishing a database connection": the database name, user or password in wp-config.php does not match step 2.
  • Permalinks give 404: the try_files line is missing from location /.
  • WordPress asks for FTP details to install plugins: the files are not owned by www-data. Re-run the chown from step 3.

Next steps

Was this guide helpful?

Still stuck?

Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.

Linux VPS

Deploy a Node.js app with PM2 behind Nginx

Install a Node.js LTS release, keep your app running with PM2 across crashes and reboots, and serve it through Nginx with HTTPS.

  • Intermediate
  • Linux VPS
  • 3 min read

Linux VPS

Create an SFTP-only user limited to one folder

Give someone file access over SFTP without a shell, locked inside one folder. Configure internal-sftp with a Match block, set the chroot ownership right and test with WinSCP.

  • Advanced
  • Linux VPS
  • 5 min read