How to install WordPress on a Linux VPS with Nginx

By ONIE Engineering Team · Updated November 12, 2026 · 3 min read

This guide sets up WordPress on an Ubuntu 22.04 or 24.04 VPS with the LEMP stack: Linux, Nginx, MariaDB and PHP-FPM. A server with 2 vCPU and 2 GB RAM is a comfortable start; see how to choose VPS specs. You can deploy a cloud VPS in a few minutes and follow along. Replace example.com with your domain and point its DNS A record to the server IP first.

1. Update the server and open the firewall

sudo apt update && sudo apt upgrade -y
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

If you have not secured SSH yet, do that first with the SSH hardening guide.

2. Install Nginx, MariaDB and PHP

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

3. Create the database

sudo mysql
CREATE DATABASE wordpress DEFAULT 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;

4. Download WordPress

cd /tmp && curl -O https://wordpress.org/latest.tar.gz
tar xzf latest.tar.gz
sudo mv wordpress /var/www/example.com
sudo chown -R www-data:www-data /var/www/example.com
cd /var/www/example.com && sudo -u www-data cp wp-config-sample.php wp-config.php

Edit wp-config.php: set DB_NAME, DB_USER and DB_PASSWORD to the values above, and replace the authentication keys with fresh ones from the official generator at https://api.wordpress.org/secret-key/1.1/salt/.

5. Configure Nginx

Create /etc/nginx/sites-available/example.com:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.php;

    location / { try_files $uri $uri/ /index.php?$args; }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }
    location ~ /\.ht { deny all; }
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

If /run/php/php-fpm.sock does not exist, use the versioned socket, for example /run/php/php8.3-fpm.sock.

6. Add a free HTTPS certificate

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

Certbot installs a timer that renews the certificate automatically. Check it with sudo certbot renew --dry-run.

7. Finish in the browser and harden

Open https://example.com and complete the installer. Then:

  • Keep WordPress, themes and plugins updated and remove the ones you do not use.
  • Use strong, unique admin passwords and enable two-factor login.
  • Take a snapshot or backup before every major update.
  • Install a caching plugin to reduce load on a small VPS.

Frequently asked questions

Which PHP version should I use?

Use the version that ships with your Ubuntu release, as long as WordPress and your plugins support it. Check the official requirements page.

Can I host several sites on one VPS?

Yes. Create one Nginx server block, one database and one directory per site, and give the server enough RAM for them.

Why do I see a 502 Bad Gateway?

Nginx cannot reach PHP-FPM. Check that PHP-FPM is running and that the socket path in the Nginx file matches an existing socket.

Sources and further reading

More VPS guides