Install and configure Nginx virtual hosts for beginners

Beginner 20 min Nov 19, 2026 19 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 Debian 11 AlmaLinux 9 Rocky Linux 9 Fedora 41

Learn how to install Nginx on Ubuntu, Debian, AlmaLinux, Rocky Linux and Fedora, then set up your first server block to host a static website. Covers directory structure, systemctl management and firewall configuration.

Prerequisites

  • A server running Ubuntu, Debian, AlmaLinux, Rocky Linux or Fedora with root or sudo access
  • A domain name pointed at your server, or willingness to test with an IP address
  • Basic familiarity with the Linux command line

What this solves

This tutorial walks you through installing Nginx and creating your first virtual host, called a server block in Nginx terminology, to serve a static HTML website. You will learn the directory structure, how to manage the service with systemctl, and how to troubleshoot common problems.

By the end you will have a working Nginx installation serving a custom domain, with a verified configuration and an open firewall port for HTTP traffic.

Step-by-step installation

Update your package manager

Always update your package index before installing new software so you get the latest available version and security patches.

sudo apt update && sudo apt upgrade -y
sudo dnf upgrade -y

Install Nginx

This installs the Nginx package along with its default configuration files and a systemd service unit.

sudo apt install -y nginx
sudo dnf install -y nginx

Start and enable the service

Starting the service launches Nginx immediately. Enabling it ensures Nginx starts automatically on every reboot.

sudo systemctl enable --now nginx

Check that the service is active before moving on. You can read more about managing services in general in our guide on checking and managing running services on Linux with systemctl.

sudo systemctl status nginx

Understand the Nginx directory structure

On Debian and Ubuntu, Nginx splits configuration into two directories: /etc/nginx/sites-available holds all defined server blocks, and /etc/nginx/sites-enabled contains symlinks to the ones that are actually active. This lets you keep a config file on disk without it being loaded.

On AlmaLinux, Rocky Linux and Fedora, Nginx instead uses /etc/nginx/conf.d, where any file ending in .conf is loaded automatically. There is no separate enable step on these distros.

ls -la /etc/nginx/
Note: The main config file at /etc/nginx/nginx.conf includes these directories with an include directive. Do not remove that line or your server blocks will stop loading.

Create the web root directory

Create a dedicated directory for your site's files, separate from the default Nginx welcome page. Using a per-site directory under /var/www is the standard convention and keeps multiple sites organized.

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

Set correct ownership so Nginx's worker process can read the files. On Debian and Ubuntu the Nginx user is www-data. On AlmaLinux, Rocky and Fedora it is nginx.

sudo chown -R www-data:www-data /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com/html
sudo chown -R nginx:nginx /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com/html
Never use chmod 777. It gives every user on the system full read, write and execute access to your website files. Directories should use 755 (owner can read, write, execute, everyone else can read and execute) and files should use 644. Fixing ownership with chown is almost always the real solution to a permission denied error, not loosening permissions.

Create a test HTML file

Add a simple HTML page so you have something to verify once the server block is live.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example.com</title>
</head>
<body>
<h1>It works! Nginx is serving example.com</h1>
</body>
</html>

Create your first server block

A server block tells Nginx which domain to respond to, where its files live, and which port to listen on. On Debian and Ubuntu, create a new file in sites-available.

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html;

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

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html;

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

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}

Enable the server block (Debian and Ubuntu only)

On Debian and Ubuntu you need to symlink the file into sites-enabled to activate it. AlmaLinux, Rocky and Fedora load everything in conf.d automatically, so this step does not apply there.

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

It is good practice to remove the default server block once you have your own configured, to avoid conflicting catch-all rules.

sudo rm -f /etc/nginx/sites-enabled/default

Test the configuration syntax

Always test your configuration before reloading Nginx. This catches typos and syntax errors without taking the running server down.

sudo nginx -t

A successful test prints syntax is ok and test is successful. If you see an error, it will point to the exact file and line number to fix.

Reload Nginx to apply changes

Reloading re-reads the configuration without dropping active connections, which is safer than a full restart.

sudo systemctl reload nginx

Open the firewall for HTTP traffic

If you have a firewall enabled, you need to explicitly allow port 80 for HTTP. Avoid disabling the firewall entirely, allow only the specific service you need.

sudo ufw allow 'Nginx HTTP'
sudo ufw reload
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

For a deeper look at crafting custom firewall rules beyond the basics, see our guide on implementing network security policies with iptables and firewalld.

Verify your setup

Confirm Nginx is running and listening on port 80.

sudo systemctl status nginx
sudo ss -tlnp | grep :80

Test locally with curl, then from a browser using your server's public IP or domain, for example 203.0.113.10.

curl -I http://example.com
curl -I http://203.0.113.10

You should see an HTTP/1.1 200 OK response and, in a browser, your test HTML page with the heading you wrote earlier.

Viewing access and error logs

Nginx writes two log files per site by default: an access log recording every request, and an error log recording problems. These are the first place to look when something is not working.

sudo tail -f /var/log/nginx/example.com.access.log
sudo tail -f /var/log/nginx/example.com.error.log

If you prefer a centralized view across multiple services, see our tutorial on setting up NGINX monitoring with Prometheus and Grafana once you are comfortable with the basics.

Common issues

SymptomCauseFix
403 Forbidden in browserWrong ownership or missing execute permission on parent directoriesRun sudo chown -R www-data:www-data /var/www/example.com and ensure directories are 755, never chmod 777
Default Nginx page still shows instead of your siteDefault server block still enabled, or server_name mismatchRemove /etc/nginx/sites-enabled/default and confirm server_name matches the domain you are testing
nginx -t fails with unexpected tokenMissing semicolon or unmatched brace in server blockRe-check the file named in the error output, fix syntax, rerun sudo nginx -t
Connection refused from outside the serverFirewall blocking port 80Allow HTTP with ufw or firewalld as shown above
Changes to HTML not appearingBrowser cache or wrong root path in server blockHard refresh the browser and confirm root directive points to the correct directory
Service fails to startAnother process already bound to port 80Check with sudo ss -tlnp | grep :80, stop the conflicting service

Next steps

Running this in production?

Want this handled for you? This works for a single server. When you run multiple environments or need this available 24/7, keeping it patched, monitored and backed up is a different job. See how we run infrastructure like this for European teams.

Automated install script

Run this to automate the entire setup

#nginx #web server #virtual hosts #server blocks #linux #beginner #static website

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. Fully managed, with one fixed contact who knows your setup.

You get one fixed contact who knows your setup

Rotterdam 22:47 · reachable in a message, no ticket form

Learn more: managed cloud infrastructure