How to configure Nginx Server Block on CentOS 7

Server Blocks Nginx allows you to run more than one website on a single server. With Server Blocks, you can define the root of the site document (the directory that contains the website files), create separate security policies for each site, use a different SSL certificate for each site and much more.

In this tutorial, we will provide a step-by-step guide on how to set up Nginx server blocks (similar to Apache Virtual Hosts) on CentOS 7.

We recommend that you test every tutorial or guide present on the internet on a virtual machine (vmware or virtualbox) before deploying to a production server, so that you don’t mess up the OS when something goes wrong.

precondition

Make sure you meet the following prerequisites before continuing with this tutorial:

  • Get a domain name that points to your public server’s IP. In this tutorial we will be using example.com.
  • Nginx is installed on your CentOS system.
  • Log in as a user with sudo privileges.

In some documents, you will see Server Blocks It is referred to as Virtual host. The term virtual host is Apache.

Create the directory structure

Document root is the directory where website files for domain names are stored and served in response to requests. We can set the document root to anywhere we want but in this directory we will use the following directory structure:

/var/www/
├── example.com
│   └── public_html
├── example2.com
│   └── public_html
├── example3.com
│   └── public_html

We will create a separate directory for each domain we want to host in the directory /var/www. In this guide, we’ll create a directory public_html Which will be the document root directory for the domain which will keep the domain web site files.

Let’s start by creating a root directory for our domain example.com:

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

For testing purposes, we’ll create an index.html In the document root directory for the domain.

Open your text editor and create an experimental file index.html:

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

Copy the following code and paste it into the file:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Selamat Datang di Server CentOS</title>
  </head>
  <body>
    <h1>Sukses, Server CentOS Online!</h1>
  </body>
</html>

In this tutorial we run the command as sudo user and newly created files and folders are owned by the root user.

To avoid permission issues, we can change the ownership of the domain document root directory to the Nginx user (nginx):

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

Create a server cluster

The nginx server cluster configuration file should end in .conf And stored in the directory /etc/nginx/conf.d.

Open an editor of your choice and create a server cluster configuration file for example.com

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

You can name the configuration file as you like, but a domain name is recommended.

Copy the following code and paste it into the 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;
    }
}

Save the file and test your Nginx configuration for correct syntax:

sudo nginx -t

If there are no errors, then the result will look like this

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

Restart the Nginx service for the changes to take effect:

sudo systemctl restart nginx

Finally, to verify that the server block is working as expected, open it http://example.com In the browser of your choice, and you’ll see something like this:

CentOS Server

Conclusion

You learned how to configure a server cluster in Nginx to host multiple domains on a single CentOS server. You can repeat the steps we explained above and create additional server blocks for all of your domains.

If you want to secure your website with Let’s Encrypt’s free SSL certificate, you can check out the following guide: Secure Nginx with Let’s Encrypt on CentOS 7

.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *