How to Install WordPress on a Subdomain with Nginx

Updated on March 28, 2022
How to Install WordPress on a Subdomain with Nginx header image

WordPress is a popular self-hosted Content Management System (CMS) commonly used to power blogs and general-purpose websites. Building a fast and reliable WordPress website requires a stable web server like Nginx to serve all necessary files requested by the website visitors on a specific domain.

With Nginx set up as the web server and MySQL or MariaDB as the database server, this article explains how to install WordPress on a subdomain and configure it to work well with the underlying infrastructure.


Before you begin, be sure to:

In this article, the subdomain appears for demonstration purposes, be sure to replace all occurrences with your actual subdomain.

Setup the WordPress Database

Log in to the MySQL database server.

$ mysql -u root -p

Create a new database.


Create a new database user secured with a strong password.

mysql> CREATE USER `wpuser`@`localhost` IDENTIFIED BY ‘Strong-Password’;

Grant full privileges to the user in the WordPress database.

mysql> GRANT ALL PRIVILEGES ON wp.* TO 'wpuser’@’localhost';

Refresh MySQL privileges.


Exit the MySQL console.

mysql> EXIT

Install WordPress

Create a new WordPress files directory.

$ sudo mkdir /var/www/

Download the latest WordPress release file.

$ wget

Extract files from the archive.

$ tar -xzf latest.tar.gz

Move extracted files to the WordPress directory.

$ sudo mv wordpress/* /var/www/

Grant Nginx read and write privileges to the directory.

$ sudo chown -R www-data:www-data /var/www/

Configure Nginx

Create a new Nginx Server Block in the /etc/nginx/sites-available directory.

$ sudo touch /etc/nginx/sites-available/

Using a text editor of your choice, edit the server block file.

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

Copy and paste the following configuration lines to the file.

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=WP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

server {
        listen 80;
        root /var/www/;
        index index.php index.html index.htm;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
                expires max;

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;

location / {
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php?q=$uri&$args;

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
                # Enable FastCGI Caching
                fastcgi_cache WP;
                fastcgi_cache_valid 200 60m;

Save and close the file.

The configuration file includes FastCGI Caching and PHP settings for the WordPress website.

Test Nginx for configuration errors.

$ sudo nginx -t

Activate the Nginx server block by linking it to the /etc/nginx/sites-enabled directory.

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

Restart Nginx for changes to take effect.

$ sudo systemctl nginx restart

Start PHP-FPM to serve WordPress PHP files.

$ sudo systemctl php7.4-fpm start


Allow the HTTP and HTTPS ports through the firewall.

On Debian-based systems with UFW:

$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp

On RHEL-based systems with FirewallD:

$ sudo firewall-cmd --permanent --zone=public --add-service=http.
$ sudo firewall-cmd --permanent --zone=public --add-service=https.

Install Certbot to secure the WordPress site with HTTPS.

On Debian-based systems:

$ sudo apt install certbot python3-certbot-nginx

On RHEL-based systems:

$ sudo yum install certbot python3-certbot-nginx

Use Certbot to request a free Let's Encrypt SSL Certificate.

$ sudo certbot -d -d

Edit the Nginx server block file.

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

Add the following lines in the server{ section to redirect all HTTP requests to HTTPS and also restrict access to critical website files such as .php and .htaccess.

# Redirect all HTTP requests to HTTPS

return 301$request_uri;

# Deny access to hidden files such as .htaccess

location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;

# Deny access to any PHP files In the WordPress uploads directory

location /wp-content/uploads/ {
        location ~ \.php$ {
                deny all;

Configure WordPress

Start a new web browser session on your local computer and visit your configured subdomain.
  1. Click Get Started to start the WordPress setup process.
  2. Enter the Database information created earlier.
  3. Enter the WordPress website title, and set up a new administrator account.
  4. Finish setup and login to the WordPress website.
  5. From the WordPress administrator dashboard, navigate to Plugins and click Add new Plugin.
  6. In the plugin search field, enter the keyword Nginx Helper, then download and install it on the WordPress website.
  7. Locate the Nginx Helper plugin from the WordPress Settings drop-down, and toggle through the available options to fine-tune your WordPress website with Nginx.
  8. As a bare minimum, enable Cache Purge to automatically clear FastCGI cache each time new changes appear on the website.

Next Steps

You have successfully installed WordPress on a subdomain with Nginx, for further information on how to use WordPress and Nginx, refer to the following resources: