HOW TO DEPLOY STATIC WEBSITES WITH NGINX ON CENTOS 8

Aug 31, 2020

For me personally, nginx is the webserver of choice for many applications. I am not going to go through why nginx is better or not than Apache as a web server, a quick search on the Internet should give you a good idea on which one is best for your needs.

Today we are going to focus on how to deploy a static website on a CentOS 8 server. So let’s jump straight into it.

To follow this tutorial, you will need:

STEP 1: POINTING THE DOMAIN NAME TO YOUR SERVER

To do this, login to your domain registrar provider website and under DNS settings add the below.

|----------+-------+-------------+-----------|
|   Type   |  Host |    Value    |    TTL    | 
|----------+-------+-------------+-----------|
| A Record |   @   | (IPAddress) | Automatic |    
|----------+-------+-------------+-----------|

STEP 2: MAKE SURE YOUR SERVER IS UP TO DATE

$ sudo yum check-update
$ sudo yum update

STEP 3: INSTALLING NGINX

$ sudo yum install nginx -y

We need to enable the nginx service to run on reboot:

$ sudo systemctl enable nginx 

To start the nginx service we do:

$ sudo systemctl start nginx

If you followed the previous tutorial on how to secure access to your server, we walked through how to setup basic firewall rules. Therefore we will have to add a HTTP rule for our Nginx webserver to be available.

$ sudo firewall-cmd --permanent --add-service=http 

We will reload the firewall service for the changes to take effect:

$ sudo firewall-cmd --reload  

CREATING THE WWW FOLDER ON THE SERVER AND ADDING THE STATIC WEBSITE

The /var/www folder where, usually, websites are located is not created by default on CentOS, so we will create it:

$ sudo mkdir /var/www  

I will use GitLab to clone the repository where the files of the static website are stored, but you could just use the scp command to copy the files to the server or upload them with FileZila.

Let’s cd into the directory and clone the repository.

$ cd /var/www  
$ git clone https://gitlab.com/claudiobrt/website  

If we do an ls we should a have folder called website and if we cd into it, all the static files for our website.

STEP 4: CREATING THE VIRTUAL HOST CONFIGURATION FILE

On CentOS the sites-available and sites-enabled folders where the Nginx configuration files for our website are located, are not automatically created when installing Nginx, so we will proceed with creating them. (this is not a necessary step, but most people, including myself, are used to having these folders for the configuration files)

$ sudo mkdir /etc/nginx/sites-available  
$ sudo mkdir /etc/nginx/sites-enabled  

For Nginx to use them, we will have to modify the nginx.conf:

$ sudo nano /etc/nginx/nginx.conf  

and add the following line inside the http block:

include /etc/nginx/sites-enabled/*;  

We can now create the configuration file with the following command:

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

and paste inside the file, the virtual host configs:

server {
  listen 80;
  listen [::]:80;  
  root /var/www/YourDomain.com;
  index index.html; 
  server_name YourDomain.com www.YourDomain.com;
    location / {
    try_files $uri $uri/ =404;
  }
}

As we told Nginx to fetch the configuration files from the /etc/nginx/sites-enabled directory, we will have to create a symbolic link for it in this directory.

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

This is pretty much done. Now we only need to test that the configuration is correct with:

$ sudo nginx -t  

If the output of this command is:

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

proceed with reloading the nginx webserver with:

$ sudo nginx -s reload  

Navigating to YourDomainName.com should get you the static website served by nginx.


:wq

Last updated 28 Sep. 2020. Built with Emacs 27.1 (Org mode 9.3.7).

DISCLAIMER