Home » PHP » How To Set Up Nginx Server Blocks on Debian 8 Linux

How To Set Up Nginx Server Blocks on Debian 8 Linux

Update on:
Sep 30, 2019

Nginx Server Block is use to configure and run more than one website or web application on a single server/system.

The Nginx Server Block helps us configure web document root (web directory to store website files), use different SSL certificates for each site, and create different or separate security policies.

This tutorial will show how to create an Nginx server block (like apache virtual host) in simple steps on Debian 8 Server.

Prerequisites

Before starting the tutorial, make sure you have the following prerequisites:

Having a domain name pointing to the server’s public IP. We will use testsite.com.

You have to install the Nginx server in your Debian machine, if not pre-installed using this guide.

You should log in to the server as a user with sudo privileges.

Create the Directory Structure

The document root is a directory on the server where we store the website files and access the domain name requests as a response. You can configure the document root directory at any location you want on the server.

We will use the following directory structure:

/var/www/
├── testsite.com
│   └── public_html
├── testsite2.com
│   └── public_html
├── testsite3.com
│   └── public_html

We are always creating a separate directory in the directory “/var/www/” on our server to host each domain. Within each of these directories, we will create a directory called “public” to store the domain website files.

Now we create the root directory for our domain testsite.com:

$ sudo mkdir -p /var/www/testsite.com/public_html

For testing the site root directory is working, we will create an “index.html” file inside the domain’s document root directory.

$ sudo vim /var/www/example.com/public_html/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to testsite.com</title>
  </head>
  <body>
    <h1>Success! testsite.com home page!</h1>
  </body>
</html>

To avoid any permission issue, as Nginx will run by the user “www-data,” we change the ownership of the domain’s document root directory to the Nginx user (www-data):

$ sudo chown -R nginx: /var/www/testsite.com

Create a Server Block

In the Debian system, by default, we store the Nginx server block files in the directory “/etc/nginx/sites-available/.” These server blocks are getting enabled by creating a symbolic link to the directory “/etc/nginx/sites-enabled/.”

Now create a server block file for testsite.com with the below configuration:

$ sudo vim /etc/nginx/conf.d/example.com.conf
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/public_html;
    index index.html;
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}

You can give any name for the server block file, but we give the name of the domain for identification and best use.

You can now enable this newly configured server block by creating a symbolic link file in the directory “sites-enabled” as shown below:

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

You can test the Nginx configuration for correct syntax using the following command:

$ sudo nginx -t

If there is no error, the output will look like below:

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

Now, restart the Nginx service for configured changes to take effect:

$ sudo systemctl restart nginx

To verify the server block working, open your site in the browser (http://testsite.com), and you will get the screen like below on your browser.

  • Facebook
  • Twitter
  • reddit
  • LinkedIn

Conclusion

Now, you have learned to create an Nginx server block for hosted websites on Debian 8, also learned to host multiple domains in a single Nginx instance and on a single Debian server. You can create as much as you can server blocks for hosting multiple domains by repeating the above steps.

If you have any doubt, problem, facing any issue in Nginx configuration or feedback, please leave a comment below.

Related Posts

How to Install MongoDB on Debian 10 Linux

How to Install MongoDB on Debian 10 Linux

MongoDB is an open-source and free NoSQL document based database system. Nowadays, it is prevalent as most application developers are using MongoDB, which can handle big data. As a MongoDB is a NoSQL database, It stores data in JSON-like documents where fields can...

How to Setup a Firewall with UFW on Debian 10 Linux

How to Setup a Firewall with UFW on Debian 10 Linux

Nowadays, a Firewall is an essential utility and property of any system for security; by default Debian Operating system having a firewall configuration tool named UFW (Uncomplicated Firewall). UFW is a user-friendly front-end tool to manage iptables firewall rules....

How to Install Eclipse IDE on Debian 10 Linux

How to Install Eclipse IDE on Debian 10 Linux

Eclipse is the most famous and widely used Java integrated development environment (IDE). It supports many plugins to enhance the capabilities to use for other programming language development environments such as PHP, C++, and JavaScript. You can install Eclipse IDE...

How to Install Ruby on Debian 10 Linux

How to Install Ruby on Debian 10 Linux

Nowadays, Ruby is the most popular language, especially for SaaS application development. It has a perfect and elegant syntax structure, and it is the language behind the ultimate robust framework known as Ruby on Rails. In this tutorial, we will explain the three...

Follow Us

Our Communities

More on Debian

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!

0 Comments

0 Comments

Submit a Comment

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

3 × five =

Shares