Linux VPS

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.

IntermediateLinux server3 min read

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

Bash
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):

Bash
ls /run/php/

2. Secure MariaDB and create the database

Bash
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

Bash
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

Bash
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:

Bash
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.

Tip: If you upload large media or themes, raise upload_max_filesize and post_max_size in /etc/php/<version>/fpm/php.ini to match client_max_body_size, then run sudo systemctl restart php8.3-fpm (use your version).

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 MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.