• 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 Debian 9

Written by Admin, Updated On May 7, 2019
debian, nginx
How to Set Up Nginx Server Blocks on Debian 9

Using Nginx Server Blocks you can run more than one website on a single machine. You can specify the separate document root for each domain. In addition, you also can create a separate security policy for each site and use different SSL certificates for each site and much more. In this guide, we will learn how to set up Nginx Server Blocks on a Debian 9.

Create Nginx Server Blocks On Debian#

Nginx Server Blocks are once kind of Virtual Hosts as in Apache.

Prerequisites#

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

Create the Directory Structure#

At first, we will create a directory structure to store the site data and serve response to visitor’s requests.

The top level directory is considered as DocumentRoot directory. You can set any location as document root as per your choice but it is best practice to define in directory structure. So we will store all document root at /var/www directory.

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

We will create separate directory for each domain inside /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

After that, we will 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

Add the following 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>

All above commands run as sudo user so root user is owner of all new created files and directories. We need to change ownership of document root directories to avoid permission issue later for regular user. So our regular user can modify files in our web directories without any problems.

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

Creating a Server Block#

In Debian systems, default location for Nginx server blocks configuration files is at /etc/nginx/sites-available directory. Those can be used by enabling through symbolic links to the /etc/nginx/sites-enabled/ directory.

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

sudo nano /etc/nginx/sites-available/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, We need to enable nginx server block by making symbolic link at /etc/nginx/sites-enabled/ directory. Run the below command to create symbolic link:

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

To test the Nginx configuration for correct syntax by typing:

sudo nginx -t

It will show 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, to verify the server block is working as expected open http://example.com to your web browser and it should show you as following :

nginx server block welcome page

Conclusion#

You learned how to create an Nginx server blocks to host multiple website on a single Debian machine. You can repeat the steps given above and create additional server blocks for all your domains.

If you have any question, feel free 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 Nginx on Debian 9 Server
Next Article   Secure Nginx with Let’s Encrypt on Debian 9

Related Posts

  • How to Change Hostname Debian 11

    How to Change Hostname on Debian 11

    February 3, 2023
  • How to Install Nginx on Ubuntu 22.04

    How to Install Nginx on Ubuntu 22.04

    January 28, 2023
  • How to Install Python 3.11 on Debian 11

    How to Install Python on Debian 11

    January 25, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Python 3.11 on Debian 11
    How to Install Python on Debian 11 January 25, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 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