Photo by Christopher Gower on Unsplash

Downloading WordPress

mkdir workspace && cd workspace
curl -LO https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
mkdir example
cp -rf workspace/wordpress/* example
cp workspace/example/wp-config-sample.php workspace/example/wp-config.php

Locate your WordPress directory, mine is /home/ajay/workspace/example. Change the ownership & permission of that directory by executing the following command from the workspace directory.

sudo chown -R www-data:www-data example/*
sudo chmod 777 example/*

Installing MySQL and Creating database & DB user

You need to make sure you have MySQL installed in your system. If you don't then install it via the following command

sudo apt-get update
sudo apt-get install mysql-server

Now create a database and its user by the following command

sudo mysql
CREATE DATABASE wordpress;
GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
EXIT;

Setting up the WordPress Configuration File

Now, open the WordPress configuration file:

sudo nano workspace/example/wp-config.php

The file will look like this,

define('DB_NAME', 'wordpress'); # replace it with your database name

/** MySQL database username */
define('DB_USER', 'wordpressuser'); # replace it with your db user name

/** MySQL database password */
define('DB_PASSWORD', 'password'); # # replace it with your db password

Save the file and exit by typing ctrl+X and then y.

Configuring Nginx

To setup, Nginx you first have to install Nginx on your system. So you can install it via following on ubuntu

sudo apt-get update
sudo apt-get install nginx

Now go to /etc/nginx/sites-available and create your website conf file, for example, I’m creating example.conf

server {
listen 80;
listen [::]:80;
root /home/ajay/workspace/example; # this is the root folder of wordpress
index index.php index.html index.htm;
server_name example.com www.example.com; # replace it with your website name
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ [^/]\.php(/|$) {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; # using php7.2
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Now check Nginx configuration via the following command

sudo nginx -t

If everything is okay then it will show you the following output

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

Now create a soft link so that your site will be enabled. To do that just type

sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/

Now restart the Nginx server via the following command

sudo systemctl restart nginx

Note: Add domain name in /etc/hosts .

Installing Additional PHP Extensions

One thing to note is we are using php7.2-fpm. So you need to make sure you have installed that in your system. If you don’t have it then install it via the following command

sudo apt install php7.2-cli php7.2-fpm php7.2-curl php7.2-gd php7.2-mysql php7.2-mbstring zip unzip

If this installed successfully then restart php7.2-fpm

sudo systemctl restart php7.2-fpm.service

To check everything is working fine, we check the status of the Nginx and php7.2-fpm

sudo systemctl status php7.2-fpm.service
sudo systemctl status nginx

Completing the Installation Through the Web Interface

In your web browser, navigate to your server’s domain name or public IP address (www.example.com). Now steps after that are self-explanatory.

Have fun!