This blog assumes that you have NGINX already installed on your system. If you have not already installed NGINX to your server install it by following the steps given in the blog here: How to Install Nginx on DigitalOcean – Lemon-Squeeze

Step 1: Install MySQL:

Next, you need to install a database. WordPress works with both MySQL and MariaDB. We decided to use the classic MySQL even though they both fared equally as well in the MariaB vs. MySQL comparison.

sudo apt install mysql-server

Again, you can test if the installation has worked by checking the status:

sudo systemctl status mysql

The database is now installed, but it still needs to be configured. To do this, first log in:

sudo mysql -u root -p

It will ask for your Server root password.

Expected Output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.41-0ubuntu0.24.10.1 (Ubuntu)

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

After Logging in to MySQL, you can now create a new database for your WordPress installation:

CREATE DATABASE WordPress CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

Expected Output:

Query OK, 1 row affected (0.01 sec)

Now create a new user and password to access this database and assign the required rights to this username. You’re free to choose the username and password:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'your-password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'username'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 2: Install PHP

The last step before you install WordPress is to install the PHP scripting language. For this you need only one command, which then automatically installs the latest PHP version:

sudo apt install php-fpm

During the installation process, you will also see which version is installed on your system. With this information, you can then verify that PHP is working correctly. In my case, version 8.3 was installed. If you already have a newer version, you must adjust the command accordingly:

sudo systemctl status php8.3-fpm

In order for PHP to work with the MySQL database, install the following extension:

sudo apt-get install php-mysql

With this command, you’ve installed the LEMP stack on your system. Just like with a LAMP server the letters L, M and P stand for Linux, MySQL (or MariaDB) and PHP. While LAMP uses an Apache server, LEMP uses the NGINX web server, pronounced like “EngineX”.

Step 3: Install WordPress

Now you can install WordPress. This can also be done directly via the Ubuntu terminal. First, however, create a folder so that you can install the content management system afterwards. It is recommended to name the folder with the domain name. This way it makes it easier to keep several websites apart later on. So create the appropriate folder and then change to this one:

sudo mkdir -p /var/www/html/example.com
cd /var/www/html/example.com

Now it’s time to download the latest version from the official WordPress site and unzip the file:

wget https://wordpress.org/latest.tar.gz

After the download is complete as follows:

# sudo wget https://wordpress.org/latest.tar.gz
--2025-02-23 13:23:30--  https://wordpress.org/latest.tar.gz
Resolving wordpress.org (wordpress.org)... 192.000.000.1
Connecting to wordpress.org (wordpress.org)|192.000.000.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26780969 (26M) [application/octet-stream]
Saving to: ‘latest.tar.gz’

latest.tar.gz                                100%[=============================================================================================>]  25.54M  8.26MB/s    in 3.1s

2025-02-23 13:23:34 (8.26 MB/s) - ‘latest.tar.gz’ saved [26780969/26780969]

Extract the files from the downloaded folder:

sudo tar -xvzf latest.tar.gz

Remove the uneeded file:

sudo rm latest.tar.gz

Move the extracted data:

sudo mv wordpress/* ./

After moving you should get something like this:

# ls
index.php    readme.html  wp-activate.php  wp-blog-header.php    wp-config-sample.php  wp-cron.php  wp-links-opml.php  wp-login.php  wp-settings.php  wp-trackback.php
license.txt  wordpress    wp-admin         wp-comments-post.php  wp-content            wp-includes  wp-load.php        wp-mail.php   wp-signup.php    xmlrpc.php

Since the web server needs to make changes to the folder, you must give NGINX the appropriate authorization:

sudo chown -R nginx: /var/www/html/example.com/

Step 4: Customize the WordPress configuration file

You need to configure WordPress so that the CMS can work with your LEMP server. To do this, go to the WordPress directory and create the sample configuration file wp-config.php. Then open the file:

cd /var/www/html/example.com
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php

Adjust the file, which you can do by changing the following lines in the document:

/** The name of the database for WordPress */
define( 'DB_NAME', 'Your database name' );
/** Database username */
define( 'DB_USER', 'The created username' );
/** Database password */
define( 'DB_PASSWORD', 'The password you set' );
/** Database hostname */
define( 'DB_HOST', 'localhost' );

Step 5: Set NGINX

Now you need to configure NGINX for WordPress. To do this, create a new configuration file in the NGINX file folder:

sudo nano /etc/nginx/conf.d/example.com.conf

Enter the following code in the empty document:

server {
    listen 80;
    root /var/www/html/example.com;
    index  index.php index.html index.htm;
    server_name  wordpress.example.com;
    client_max_body_size 500M;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}

Make sure that you enter the correct path to your WordPress document at the beginning of the file. After that you can check the source code.

sudo nginx -t

Expected Output

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

You should get an indication that the syntax is ok and the text was successful. Finally, restart the server to make sure that all changes can take effect.

sudo systemctl restart nginx

Step 6: Log into the WordPress dashboard

Now you have everything installed and you can start designing your WordPress website. To do this, launch a browser and access your domain. In this tutorial, we have set WordPress under example.com”. So you would need to visit the appropriate domain, which is where you will be greeted with the first page of the setup wizard.

Note:
I faced an issue where I was unable to see my wordpress installation site as and it was pointing to my main domain rather than the subdomain that was present on the IP address so a quick fix for this problem and it might work for you in case your problem is caused by DNS cache:
Run ipconfig /flushdns in your command prompt to clear the cache and your problem should probably be fixed.

2 thoughts on “How to install WordPress with NGINX:

  1. Fantastic website you have here but I was curious if you knew of any user discussion forums that cover the same topics discussed here?
    I’d really like to be a part of group where I can get responses from other experienced individuals that share the same interest.
    If you have any recommendations, please let me know. Bless you!

    1. Hi,
      Thanks a lot for the kind words! I’m actually pretty new to all this myself, so I don’t really know of many forums yet. But if I come across any good ones, I’ll definitely let you know!
      Appreciate you reaching out, and feel free to hit me up anytime if you want to chat more.
      Cheers,
      Ujjwal Desu

Leave a Reply

Your email address will not be published. Required fields are marked *