How to Install WordPress on a Subdomain with Nginx

  • Last Created On Nov 20, 2024
  • 33
0 0

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.

Prerequisites

Before you begin, be sure to:

  • Deploy a Dekopon Stack One-Click LEMP server, or install a new Ubuntu 20.04 server and a LEMP stack.
  • Create, and point a subdomain to the server.
  • Access the server as a non-root user with sudo privileges.

In this article, the subdomain wp.example.com 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.

mysql> CREATE DATABASE wp;

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.

mysql> FLUSH PRIVILEGES

Exit the MySQL console.

mysql> EXIT

Install WordPress

Create a new WordPress files directory.

$ sudo mkdir /var/www/wp.example.com

Download the latest WordPress release file.

$ wget http://wordpress.org/latest.tar.gz

Extract files from the archive.

$ tar -xzf latest.tar.gz

Move extracted files to the WordPress directory.

$ sudo mv wordpress/* /var/www/wp.example.com

Grant Nginx read and write privileges to the directory.

$ sudo chown -R www-data:www-data /var/www/wp.example.com

Configure Nginx

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

$ sudo touch /etc/nginx/sites-available/wp.example.com.conf

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

$ sudo nano /etc/nginx/sites-available/wp.example.com.conf

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/wp.example.com;
        index index.php index.html index.htm;

        server_name wp.example.com;
    
        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/wp.example.com.conf   /etc/nginx/sites-enabled/wp.example.com.conf

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

Security

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 wp.example.com -d www.wp.example.com

Edit the Nginx server block file.

$ sudo nano /etc/nginx/sites-available/wp.example.com.conf

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 https://wp.example.com$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.

http://wp.example.com
  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:

  • How to Set Up Redis® Caching for WordPress
  • Top 7 Reasons Why You Should Move From Shared Hosting to a Dekopon Stack Cloud Server
  • How to Install WordPress on LEMP with Ubuntu or Debian
  • Compile and Install Nginx With the PageSpeed Module on Debian 8
  • Redirect HTTP Requests to HTTPS on Nginx
  • Install Let's Encrypt SSL on Ubuntu with Apache or Nginx
Views: 33

Recent Articles

  • How to use Sudo on a Dekopon Stack Cloud...
    44
  • CentOS
    Setup HTTP Authentication With Nginx on...
    63
  • CentOS
    Plesk: Upgrade MariaDB From Version 5.50...
    61
  • CentOS
    Installing CloudLinux on DirectAdmin wit...
    78
  • CentOS
    Installing Pritunl on CentOS 7
    84

Popular Articles

  • Troubleshooting WordPress Database Error...
    248
  • How To Migrate WordPress To Dekopon Sta...
    170
  • Troubleshooting the WordPress White Scre...
    133
  • Upgrade One-Click WordPress without Down...
    123
  • CentOS
    How to Install PufferPanel (free Minecra...
    113