Skip to content
Follow
Ubuntu Linux

How to Set up a Nginx Server Block on Ubuntu Linux

Richard
Written by
Richard
Dec 7, 2021 Updated Jul 14, 2026 4 min read
Enable Automatic Suspension in Ubuntu Linux Easily
Enable Automatic Suspension in Ubuntu Linux Easily

Setting up an Nginx server block on Ubuntu Linux lets you run many websites from one server.

A server block tells the Nginx web server how to handle requests for a specific website. Each block acts like a mini-manager for one site, setting its rules for visitors.

You can host multiple sites on a single Ubuntu machine this way. For example, Nginx can show example.com on one address and another.org on another, each with its own security setup. The server block file holds all the important details for each site.

This includes where the site’s files live, its security settings, and where to find security certificates. It’s a smart way to use your server’s power without needing extra machines.

⚡ Quick Answer

Create website directories in /var/www/, then create an Nginx configuration file in /etc/nginx/sites-available/. Link this file to /etc/nginx/sites-enabled/ and restart Nginx.

How to create website directory structures on Ubuntu Linux

Creating a separate folder for each website’s files, called the document root, is the first step to setting up an nginx server block on Ubuntu. This organisation helps your server quickly find and show the correct content for each website you host.

Here’s an example of a directory structure for multiple websites with unique content and domains.

Each website domain requires a dedicated folder for its files, called a document root. For example, the website "example.com" stores its files in the `/var/www/example.com/public_html` folder. This document root setup ensures Nginx sends the correct website files for multiple sites on one server, preventing content mix-ups for up to 10 different domains.

You create a directory for the example.com website and its files using the `sudo mkdir -p /var/www/example.com/html` command. This specific command creates the necessary folders within the `/var/www/` path to store your website content.

🐧Bash / Shell
sudo mkdir -p /var/www/example.com/public_html

You’ll need an index.html file in each document root to display content to visitors. Run the commands below to create an index.html file for the example.com domain.

The basic HTML file for testing is created by copying and pasting the provided content into the configuration file and then saving the file.

💻Code
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

Once you save the file, we can configure Nginx to reference this content. To avoid permission issues, you’ll want to change the ownership of the domain document root directory and all files within it to the www-data user.

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

How to create an Nginx server block file on Ubuntu Linux

Now that you’ve created domain content in the directory above, configure the Nginx server block configuration file for that content.

You’ll find the Nginx server block configuration files on Ubuntu Linux within the /etc/nginx/sites-available directory.

To create a server block file in the sites-available directory for our content, run the commands below. This creates a site-specific Virtual Host configuration file.

This Nginx server block configuration works with most environments. Copy and paste the provided Nginx server block configuration into the `nginx.conf` file you created and save the `nginx.conf` file.

Once saved, the Nginx server block configuration file becomes active when Nginx reloads its configuration. Reloading ensures Nginx reads the new file, allowing the server block to handle website requests starting from version 1.14.2. This step makes the server block functional.

💡Tip
You use the symbolic link command to enable the new server block configuration file. This creates a symbolic link from the server block file to the sites-enabled directory.

Run the commands below to enable the configuration file for our domain.

🐧Bash / Shell
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
⚠️Warning
After that, run the commands below to restart the Nginx service.
🐧Bash / Shell
sudo systemctl restart nginx

Once Nginx is restarted, browse to the server hostname or IP address. It should display the content file we created above.

Nginx server block configuration file test example Ubuntu
apache virtual host file test

Repeat this process for other domains and their server block configuration files. You can create as many virtual host files as your server can handle.

That should do it!

Conclusion:

  • Configuring Nginx server blocks allows you to host multiple websites on a single server, optimizing resource usage.
  • Each website requires a unique directory structure, document root, and configuration settings.
  • By following the steps outlined, users can create and enable server blocks for different domains effectively.
  • Always ensure proper permissions are set for document root directories to avoid access issues.
  • Regularly check server logs to monitor traffic and troubleshoot any problems that may arise.
  • Feel free to create additional server blocks as needed, expanding your web hosting capabilities.

What is a server block in NGINX?

In NGINX, a server block is a configuration block that specifies how the server should handle traffic for a specific domain or group of domains. This allows you to define different settings and behavior for different websites or applications that are hosted on the same server.

Can NGINX have multiple server blocks?

Can NGINX handle multiple server blocks? Yes, you create each block within the Nginx configuration file to achieve this. In Nginx, you can have different configurations for each domain or subdomain hosted on your server. The `nginx.conf` file will have the server blocks.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

📚 Related Tutorials

How to Upgrade Ubuntu Linux
Ubuntu Linux How to Upgrade Ubuntu Linux

No comments yet — be the first to share your thoughts!

Leave a Comment

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