• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Set Up Nginx Server Blocks on CentOS 8

Written by Admin, Updated On May 10, 2020
centos, nginx
How to Set Up Nginx Server Blocks on CentOS 8

Nginx Server Blocks allows you to run multiple websites on a single server. It makes simple and easy to manage configurations of each site independently. We can set separate security policy and use different SSL certificates and much more. In this guide, we will show how to set up Nginx Server Blocks on a CentOS 8 server.

Nginx Server Blocks are similar to Virtual Hosts in Apache.

Prerequisites#

  • A CentOS 8 server with a non-root user with sudo privileges.
  • A domain name should pointing to your server IP address.
  • Nginx should installed and configured, as shown in How to Install Nginx Web Server on CentOS 8 system.

Create the Directory Structure#

First, we will design a directory structure to store the site data to serve to visitors.

The top level directory is considered as DocumentRoot directory. We can set the document root to any location that you want but it’s best practice to set in directory structure. So we will store all at /var/www.

/var/www/
├── example1.com
│   └── public_html
├── example2.com
│   └── public_html

Above, we are creating separate directory for each domain under /var/www directory. Within this directory, we’ll create a public_html directory as domain document root directory to store website data.

sudo mkdir -p /var/www/example.com/public_html

Create a index.html file within the domain document root directory for testing purpose. By default, This page will display when visitors visit your website.

You can create a new index.html file using your favorite text editor type:

sudo nano /var/www/example.com/public_html/index.html

After that, add the below lines into it:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Welcome!!</title>
  </head>
  <body>
    <h1>Great! example.com set up completed!</h1>
  </body>
</html>

In this tutorial, all commands run as sudo user and newly created files and directories are owned by the root user. So we will change ownership of document root directories to avoid permission issue later for regular user. Therefore, regular user can modify files in our web directories without any problems.

sudo chown -R nginx: /var/www/example.com

Creating a Server Block#

Nginx server block configuration files must end with .conf extension. Those files should store in /etc/nginx/conf.d directory.

Create a new file for example.com using your choice text editor by typing :

sudo nano /etc/nginx/conf.d/example.com.conf

Now, add the following lines in to this file:

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

    root /var/www/example.com/public_html;

    index index.html;

    server_name example.com www.example.com;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

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

You can give any names to your configuration file but it’s best practice to give file name same as domain name.

Next, Save the file and test the Nginx configuration for correct syntax:

sudo nginx -t

You will get following output if there is no error.

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

You must restart the Nginx service to take effect. Execute below command:

sudo systemctl restart nginx

Finally, you can verify by opening your http://example.com to your web browser and it should show you as following :

nginx server block welcome page

Conclusion#

In this guide, you have learned how to create and set up Nginx server blocks to host multiple website on a single CentOS 8 server. You can repeat the steps to create additional server blocks for multiple domains.

Don’t hesitate to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install Yarn on Ubuntu 20.04 LTS
Next Article   How to Install Ruby on Ubuntu 20.04 LTS

Related Posts

  • How to Install WordPress with Nginx on Debian 11

    How to Install WordPress with Nginx on Debian 11

    March 22, 2023
  • How to Install LEMP Stack on Ubuntu 22.04

    How to Install LEMP Stack on Ubuntu 22.04

    March 18, 2023
  • How to Install Set Up Nginx Server Blocks on Ubuntu 22.04

    How to Set Up Nginx Server Blocks on Ubuntu 22.04

    March 4, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook