, , , , ,

How to Install WordPress on a LEMP Stack with NGINX

How to Install WordPress on a LEMP Stack with NGINX

If you’ve ever wondered why so many fast WordPress sites run on NGINX instead of Apache, the best way to find out is to build one yourself. A LEMP stack gives you everything WordPress needs, and once you’ve set one up, you’ll understand exactly what’s happening every time someone loads a page.

LEMP stands for Linux, Engine-X (the way NGINX is pronounced), MariaDB or MySQL, and PHP. It’s the NGINX counterpart to the more familiar LAMP stack, with one important difference in how PHP is handled, which we’ll get to shortly.

This guide assumes you’re running Ubuntu Server or another Ubuntu-based distribution, such as Linux Mint. A few details may vary slightly depending on your setup.

Before you start: this post picks up where my NGINX guide leaves off. If you haven’t installed NGINX yet, start here first: How to Use AI to Build an NGINX Server on Mac, Linux, and Windows. Come back once you can see the NGINX welcome page in your browser.

How the Pieces Fit Together

Here’s the key difference from Apache. Apache can run PHP inside its own process. NGINX can’t, so it hands PHP requests off to a separate service called PHP-FPM (FastCGI Process Manager).

A request to your site works like this:

  1. A visitor requests a page, and NGINX receives it.
  2. If the request is for an image, stylesheet, or other static file, NGINX serves it directly.
  3. If it’s a PHP request, NGINX passes it to PHP-FPM, which runs WordPress.
  4. WordPress asks MariaDB for the posts, settings, and user data it needs.
  5. The finished page travels back through NGINX to the visitor.

This division of labor is a big part of why NGINX is known for handling traffic efficiently. It also means PHP-FPM is the one new piece you need to understand.

Step 1: Check Your Starting Point

Confirm that NGINX is installed and running:

bash

sudo systemctl status nginx

Then refresh your package list and apply any pending updates:

bash

sudo apt update
sudo apt upgrade

Step 2: Install PHP-FPM and MariaDB

Install the database server, PHP-FPM, and the PHP extensions WordPress relies on:

bash

sudo apt install mariadb-server php-fpm php-mysql \
php-curl php-gd php-mbstring php-xml php-zip \
php-intl php-bcmath php-imagick curl tar

Notice there’s no Apache package and no libapache2-mod-php. NGINX talks to PHP-FPM instead.

Make sure both services start now and again after a reboot. The PHP-FPM service name includes your PHP version, so check what was installed:

bash

ls /run/php/

You’ll see a socket file such as php8.3-fpm.sock. Write that filename down, because you’ll need it in Step 5. The version number in the service name matches the socket, so for PHP 8.3 you’d run:

bash

sudo systemctl enable --now mariadb
sudo systemctl enable --now php8.3-fpm

Adjust 8.3 to match whatever version your server installed.

Step 3: Set Up the WordPress Database

WordPress needs its own database and its own database user. Avoid giving it an administrator account, since a compromised site shouldn’t be able to touch anything else on your server.

Open the MariaDB shell:

bash

sudo mariadb

Then run the following, swapping in your own strong password:

sql

CREATE DATABASE wordpress
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci;

CREATE USER 'wpuser'@'localhost'
    IDENTIFIED BY 'CHOOSE_A_STRONG_PASSWORD';

GRANT ALL PRIVILEGES ON wordpress.*
    TO 'wpuser'@'localhost';

FLUSH PRIVILEGES;
EXIT;

Keep the database name, username, and password handy. A password manager is a good place to store them, and they should never end up in a public repository or a blog post.

Step 4: Download WordPress

I prefer pulling WordPress straight from WordPress.org so I’m always working with the current official release rather than a possibly outdated distro package.

bash

cd /tmp
curl -fLO https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz

Create a home for the site and copy the files over:

bash

sudo mkdir -p /var/www/wordpress
sudo cp -a /tmp/wordpress/. /var/www/wordpress/

Next, hand ownership to the web user and set sensible permissions:

bash

sudo chown -R www-data:www-data /var/www/wordpress
sudo find /var/www/wordpress -type d -exec chmod 755 {} \;
sudo find /var/www/wordpress -type f -exec chmod 644 {} \;

This setup is fine for getting started and learning. On a production server, you’ll want to tighten it so the web user can’t rewrite WordPress core files. That does mean planning for how updates and uploads will work.

Step 5: Write the NGINX Server Block

NGINX calls its site configurations server blocks, which is the equivalent of Apache’s virtual hosts. Create one for WordPress:

bash

sudo nano /etc/nginx/sites-available/wordpress

Paste in this configuration, using your own domain and the PHP socket name you noted earlier:

nginx

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

    server_name example.com www.example.com;

    root /var/www/wordpress;
    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 ~ /\.ht {
        deny all;
    }

    access_log /var/log/nginx/wordpress-access.log;
    error_log  /var/log/nginx/wordpress-error.log;
}

A few lines deserve a closer look:

  • try_files is what makes pretty permalinks like /about-us/ work. NGINX checks for a real file first, then a directory, and finally sends the request to WordPress’s index.php. There’s no .htaccess file in this setup, so this one line does that job.
  • fastcgi_pass is the handoff to PHP-FPM. If this socket path doesn’t match what’s in /run/php/, you’ll get a 502 error.
  • client_max_body_size raises the upload limit. Without it, NGINX rejects anything over 1 MB, which isn’t much when you’re uploading images. You may also need to raise upload_max_filesize and post_max_size in your PHP configuration to match.
  • The .ht rule blocks access to Apache-style config files, in case any ever land in your WordPress directory.

Enable the site by linking it into sites-enabled:

bash

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/

If this server is dedicated to WordPress, remove the default site so it doesn’t compete with yours:

bash

sudo rm /etc/nginx/sites-enabled/default

Now test the configuration before applying it:

bash

sudo nginx -t

A healthy result reports that the syntax is okay and the test is successful. Then reload:

bash

sudo systemctl reload nginx

Step 6: Configure wp-config.php

WordPress ships with a sample configuration file. Copy it into place:

bash

cd /var/www/wordpress
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php

Update the database section with the details from Step 3:

php

define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wpuser' );
define( 'DB_PASSWORD', 'CHOOSE_A_STRONG_PASSWORD' );
define( 'DB_HOST', 'localhost' );

You’ll also see a block of authentication keys and salts, which WordPress uses to protect login sessions. Generate a unique set from the official generator at https://api.wordpress.org/secret-key/1.1/salt/ and replace the placeholder lines.

This file holds your database credentials, so treat it like a secret. Keep it out of version control and don’t expose it publicly.

Step 7: Run the WordPress Installer

Open your domain in a browser, or use the server’s IP address if you’re testing on a local network. The WordPress setup screen should appear.

Choose your language, name your site, and create an administrator account. Skip admin as the username and use a long, unique password.

When it’s done, you can log in at /wp-admin.

If you see a blank page, a download prompt instead of a web page, or a 502 error, jump to the troubleshooting section below. Those almost always trace back to the PHP-FPM connection.

Step 8: Add HTTPS

Don’t leave a public site running on plain HTTP. Certbot can issue a free certificate and update your NGINX configuration for you, provided your domain points at the server and ports 80 and 443 are reachable. Choose NGINX and your operating system on the official Certbot site and follow the current instructions there, since the exact steps change over time.

While you’re hardening things, work through this checklist:

  • Keep Ubuntu, NGINX, PHP, MariaDB, WordPress, and all plugins and themes updated.
  • Open only the firewall ports you actually need.
  • Schedule regular backups of both the site files and the database.
  • Install themes and plugins only from sources you trust.

A site that loads isn’t necessarily a site that’s secure, so take the time to verify before you rely on it.

Troubleshooting

Check the basics first, and resist the urge to change several things at once.

NGINX won’t reload or start. Run sudo nginx -t. It points to the exact file and line causing the problem.

502 Bad Gateway. NGINX can’t reach PHP-FPM. Confirm the service is running (sudo systemctl status php8.3-fpm) and that the socket path in your server block matches the file in /run/php/.

Browser downloads the PHP file instead of displaying the page. The PHP location block isn’t being matched. Recheck the location ~ \.php$ section for typos.

“Error establishing a database connection.” Verify the credentials in wp-config.php and make sure MariaDB is running.

403 Forbidden. Check file ownership and permissions on /var/www/wordpress, then read the error log: sudo tail -n 50 /var/log/nginx/wordpress-error.log

Permalinks return 404. Make sure the try_files line is present in your location / block, then reload NGINX.

Uploads fail with “413 Request Entity Too Large.” Increase client_max_body_size in your server block and reload.

If you’re using AI to help troubleshoot, paste the specific error message, describe what you’ve already tried, and scrub any passwords or private details from your logs first. Asking it to explain the cause before suggesting fixes will help you learn more along the way.

Wrapping Up

You now have a working LEMP stack: NGINX handling requests, PHP-FPM running WordPress, and MariaDB storing everything behind it. The real payoff is that you understand how each layer connects, so when something breaks, you know where to look.

From here, you can pick a theme, install your plugins, set up backups, and start tuning performance.