VPS Series · Post 2 of 5

Installing WordPress on Ubuntu 24.04
with a LEMP Stack

After setting up the LEMP stack in Post 1, it's time to install WordPress. This covers creating a MySQL database, downloading WordPress, configuring Nginx server blocks, and fixing the Cloudflare SSL redirect loop that trips up most beginners.

1 Create MySQL Database

Log into MySQL and create a database and user for WordPress:

bash
mysql -u root -p

Then inside the MySQL shell:

sql
CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'SecurePass@2024!';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

2 Download & Place WordPress

bash
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xvf latest.tar.gz
mv wordpress /var/www/html/wordpress

# Set correct ownership
chown -R www-data:www-data /var/www/html/wordpress
chmod -R 755 /var/www/html/wordpress

3 Configure wp-config.php

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

Find and update these lines with your database credentials:

php
define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wpuser' );
define( 'DB_PASSWORD', 'SecurePass@2024!' );
define( 'DB_HOST', 'localhost' );

4 Create Nginx Server Block

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

Paste this configuration:

nginx
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/html/wordpress;
    index index.php index.html;

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

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

5 Enable the Site

bash
# Remove default site (important!)
rm /etc/nginx/sites-enabled/default

# Enable WordPress site
ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx

Removing the default site is critical. If it's still enabled, Nginx serves the default page instead of WordPress.

6 Fix Cloudflare SSL Redirect Loop

After pointing your domain and setting Cloudflare SSL to Full, you may get a redirect loop. Add this to wp-config.php above the "stop editing" line:

php
define('FORCE_SSL_ADMIN', true);
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
    $_SERVER['HTTPS'] = 'on';
}

7 Update WordPress URLs via MySQL

If you changed your domain and WordPress is looping, reset URLs directly in the database:

sql
USE wordpress;
UPDATE wp_options SET option_value = 'https://example.com'
  WHERE option_name = 'siteurl';
UPDATE wp_options SET option_value = 'https://example.com'
  WHERE option_name = 'home';
EXIT;

Visit https://example.com/wp-admin — you should see the WordPress installer or login screen. Done!