Install Nginx with Server Blocks (Virtual Hosts) on Debian 10

Nginx is a very popular high-performance web server that combines the power of reverse proxying, load balancing, caching and so much more. Depending on how it is configured, it can act as a reverse proxy as well as a load balancer for HTTP/HTTPS servers.

Nginx web server has a phenomenal capability in serving thousands of concurrent connections and this makes it the fastest web server, powering over half of the busiest sites on the globe. These include Netflix, DuckDuckGo, and DropBox to mention just a few.

Read Also: How to Install Apache with Virtual Hosts on Debian 10

In this tutorial, we will walk you through the steps on how to install Nginx with virtual hosts to host multiple domains on a on Debian 10 server.

Prerequisites

Before we get started, ensure the following requirements are met:

  1. An instance of Debian 10.
  2. A Fully Qualified Domain Name (FQDN) pointing to the server.
  3. In this guide, we use the domain tecmint.com pointing to a Debian 10 system with an IP address 192.168.0.104.
  4. A good internet connection.

Step 1: Update the Debian 10 Package Repository

Before anything else, we need to update our local package repository to the latest versions. To achieve this, log in as a regular user with sudo privileges and run the command below.

$ sudo apt update -y
Update Debian 10 Repository
Update Debian 10 Repository

Step 2: Install Nginx on Debian 10

Since Nginx is present in Debian’s repositories, we can comfortably go ahead and install it using the apt package manager that comes with Debian.

$ sudo apt install nginx -y
Install Nginx on Debian 10
Install Nginx on Debian 10

Step 3: Checking the Status of Nginx Webserver

If you encountered no errors, then Nginx web server was successfully installed. It’s prudent to verify the status of the web server before making further configurations.

To check the status of Nginx, execute:

$ systemctl status nginx

If the web server is up and running, you’ll get the notification below.

Check Nginx Status on Debian 10
Check Nginx Status on Debian 10

If you wish to restart the Nginx web server, run the command.

$ systemctl restart nginx

To stop Nginx, issue the command.

$ systemctl stop nginx

To start the web server, run.

$ systemctl start nginx

To configure Nginx web server to start on boot run.

$ systemctl enable nginx

Read Also: 10 Most Used Nginx Commands Every Linux User Must Know

Step 4: Configure the Firewall to Open Nginx Port

With Nginx successfully installed and running, we need to allow web access to the service, especially to external users. If you have UFW firewall enabled, you need to allow HTTP access through the firewall.

To achieve this, execute the command.

$ sudo ufw allow 'Nginx HTTP'

Next, reload the firewall to effect the changes.

$ sudo ufw reload

Great, now you can verify that HTTP is allowed through the firewall by running.

$ sudo ufw status
Open Nginx Port on Debian 10
Open Nginx Port on Debian 10

From the snippet above, we can clearly see that Nginx HTTP has been allowed through the UFW firewall.

Step 5: Accessing Nginx Web Server

We have so far made the basic configurations to get Nginx up and running. To access the web server via the web browser, browse the server’s IP address as shown.

http://server-IP-address
Verify Nginx Webserver on Debian 10
Verify Nginx Webserver on Debian 10

This is a confirmation that Nginx is up and running.

Step 6: Configuring Nginx Server Blocks on Debian 10

This is an optional step and is useful when you want to host multiple domains on a Nginx web server. For this to work, you need to have a domain name pointed to your Debian server.

For this section, we shall use the domain name tecmint.com who’s A record is pointed to the server’s IP 192.168.0.104.

When you point the domain name to your server’s IP address, the domain name will soon change and point to your web server as shown.

Check Nginx with Domain on Debian 10
Check Nginx with Domain on Debian 10

Let’s now create a server block.

Firstly, let’s create a directory for our domain as shown.

$ sudo mkdir -p /var/www/html/tecmint.com

Then assign the required file ownership as shown.

$ sudo chown -R $USER:$USER /var/www/html/tecmint.com

Next, assign read and execute permissions to group and the public users as shown.

$ sudo chmod -R 755 /var/www/html/tecmint.com

Let’s now create a simple index.html sample webpage using vim text editor.

$ sudo vim /var/www/html/tecmint.com/index.html

Add some sample content to the file. This will be displayed on the browser.

<html>
    <head>
        <title>Welcome to Linux geeks</title>
    </head>
    <body>
        <h1>Success! Welcome to your new server block on Tecmint Nginx Web Server !</h1>
    </body>
</html>

Save and exit the editor

For this content to be served, a server block needs to be created.

Let’s create a server block

$ vim  /etc/nginx/sites-available/tecmint.com

Copy and paste the following content into the server block file.

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

        root /var/www/html/tecmint.com;
        index index.html index.htm index.nginx-debian.html;

        server_name tecmint.com www.tecmint.com;

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

Be sure to update the domain name tecmint.com with your own domain name.

To activate or enable the server block file, create a symbolic link as shown.

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

To verify that all settings in Nginx are properly configured, run.

$ sudo nginx -t

Great, we are good to go! Finally restart Nginx.

$ sudo systemctl restart nginx

Head out to your browser and refresh and if all went well, the browser should be serving your server block web page as shown.

Check Nginx Server Block Website on Debian 10
Check Nginx Server Block Website on Debian 10

Step 7: Accessing Nginx Log Files

To access log files about requests made to your server, access the file below.

$ sudo vim /var/log/nginx/access.log 

In case you bump into errors in your Nginx web server, examine the file for errors.

$ sudo vim /var/log/nginx/error.log
Conclusion

In this guide, you learned how to install Nginx on your Debian 10 instance and configuring it further to support additional domains. We hope you found this guide insightful. Your feedback will be appreciated..

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.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

1 thought on “Install Nginx with Server Blocks (Virtual Hosts) on Debian 10”

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.