How to Set Up Nginx Server Blocks (Virtual Hosts) on CentOS 8

An Nginx server block is the equivalent of an Apache virtual host and makes it possible for you to host more than one domain or website on your server.

In this article, you will learn how to set up an Nginx server blocks (virtual hosts) on CentOS 8 and RHEL 8 Linux.

Prerequisites:

  • An A record for your domain. In simple terms, an A records refers to a DNS entry where the domain name is pointed to the Public IP of the server, in this case the Nginx web server. Throughout this guide , we will use the domain name crazytechgeek.info.
  • An Nginx Server or LEMP Stack installed on CentOS 8 or RHEL 8 instance.
  • A login user with Sudo privileges.

Let’s get started!

Step 1: Create an Nginx Document Root Directory

Right off the bat, you need to create a custom web root directory for the domain you want to host. For our case, we will create the directory as shown using the mkdir -p option to create all the necessary parent directories:

$ sudo mkdir -p /var/www/crazytechgeek.info/html

Thereafter assign the directory permissions using the $USER environment variable. As you do so, ensure that you are logged in as a regular user and not the root user.

$ sudo chown -R $USER:$USER /var/www/crazytechgeek.info/html

Next, assign the right directory permissions recursively as shown:

$ sudo chmod -R 755 /var/www/crazytechgeek.info/html

Step 2: Create a Sample Page for the Domain

Next, we are going to create an index.html file inside the custom web root directory that will be served by the domain once a request is made.

$ sudo vim /var/www/crazytechgeek.info/html/index.html

Inside the file, paste the following sample content.

<html>
    <head>
        <title>Welcome to your_domain!</title>
    </head>
    <body>
  <h1>Awesome! Your Nginx server block is working!</h1>
    </body>
</html>

Save and exit the configuration file.

Step 3: Create an Nginx Server Block in CentOS

For the Nginx web server to serve the content in the index.html file we created in step 2, we need to create a server block file with the appropriate directives. Therefore, we shall create a new server block at:

$ sudo vim /etc/nginx/conf.d/crazytechgeek.info.conf

Next, paste the configuration that appears below.

server {
        listen 80;
        listen [::]:80;

        root /var/www/crazytechgeek.info/html;
        index index.html index.htm index.nginx-debian.html;

        server_name crazytechgeek.info www.crazytechgeek.info;

        location / {
                try_files $uri $uri/ =404;
        }

		
    access_log /var/log/nginx/crazytechgeek.info.access.log;
    error_log /var/log/nginx/crazytechgeek.info.error.log;

}
Create Nginx Server Block in CentOS
Create Nginx Server Block in CentOS

When you are done, save the changes and exit the configuration file. To confirm that all Nginx configurations are sound and error-free, execute the command:

$ sudo nginx -t

The output below should be a confirmation that you are good to go!

Check Nginx Configuration File in CentOS
Check Nginx Configuration File in CentOS

Finally, restart your Nginx web server and confirm that it’s running as expected:

$ sudo systemctl restart nginx
$ sudo systemctl status nginx
Check Nginx Status in CentOS
Check Nginx Status in CentOS

Step 4: Testing the Nginx Server Block in CentOS

We are all done with the configurations. The only part remaining is to confirm if our server block is serving content in the web root directory defined earlier in the index.html file.

To do this, simply open your browser and go to your server’s domain as shown:

http://domain-name
Check Nginx Server Block
Check Nginx Server Block

As observed, our content is being served by the server block, a clear indication that all went well.

Step 5: Enable HTTPS on Domain Hosted on Nginx

You may consider encrypting your domain using Lets Encrypt SSL to add a layer of protection and secure traffic to and from the webserver.

$ sudo dnf install certbot python3-certbot-nginx
$ sudo certbot --nginx

To confirm that your domain is correctly configured on HTTPS, visit https://yourwebsite.com/ in your browser and look for the lock icon in the URL bar.

Conclusion

We have successfully set up a Nginx server block on CentOS 8 and RHEL 8. You can repeat the same for multiple domains using the same procedure.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
James Kiarie
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

2 Comments

Leave a Reply
  1. Just a quick Note:

    $ sudo systemctl restart nginx
    $ sudo systemctl status Nginx
    

    should be:

    $ sudo systemctl restart nginx
    $ sudo systemctl status nginx
    

    The Capitalised “Nginx” on line two caused an issue :)

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.