Nginx from scratch on Ubuntu: install, server blocks, reload
Nginx is a web server that reads its rules from config files, so you install it, describe a site in a server block, test the syntax and reload it.
- Updated Nov 27, 2026
- Markdown
Install nginx, serve a static site from its own server block and reload it safely after testing the config.
- An Ubuntu server you can reach over SSH with sudo
- Port 80 open in the firewall
Nginx serves files and forwards requests, and everything it does comes from text files you write. On Ubuntu the pattern is: install the package, put one file per site in sites-available, link it into sites-enabled, test it, then reload. This lesson builds a static site that way and shows where to look when it does not behave.
How it works#
The nginx package starts a master process that reads /etc/nginx/nginx.conf and runs worker processes that handle requests. That main file ends with include /etc/nginx/sites-enabled/*;, so every file linked in that folder is part of the config.
Each site is one or more server blocks. A server block says which port to listen on (listen), which host names it answers to (server_name), and what to do with a request (root, index, location). When a request arrives, nginx first picks the server block by port and by the Host header, then picks a location inside it.
Nginx runs as a systemd service. A reload (systemctl reload nginx) sends a signal so new workers start with the new config while old workers finish their requests. If the new config is broken, nginx keeps the old one. That is why you run nginx -t first and reload instead of restart.
Step 1: Install nginx#
sudo apt update
sudo apt install nginxThe package enables and starts the service. Check it:
systemctl status nginx --no-pager
curl -I http://localhostWhat you should see: active (running) and a 200 OK with Server: nginx. The page at http://203.0.113.10 shows “Welcome to nginx” if port 80 is open. If it does not load from outside, see set up a firewall with ufw.
Step 2: Create the site content#
sudo mkdir -p /var/www/example.com/html
echo '<h1>Hello from example.com</h1>' | sudo tee /var/www/example.com/html/index.htmlNginx workers run as the www-data user on Ubuntu, so the files must be readable by it. Files you create with the default umask are world readable, which is enough. See Linux file permissions if you get a 403 later.
Step 3: Write the server block#
Create /etc/nginx/sites-available/example.com:
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;
}
}listen 80andlisten [::]:80accept IPv4 and IPv6.server_namelists the host names this block answers to.rootis the folder that maps to the URL/. A request for/about.htmlreads/var/www/example.com/html/about.html.indexnames the file served for a directory URL.try_fileschecks the exact file, then a directory with an index, then returns 404. A single page app would end with/index.htmlinstead of=404.
Step 4: Enable the site and test#
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -tWhat you should see: syntax is ok and test is successful. If it names a file and line, fix that line first. Do not reload on a failed test.
Step 5: Make it the default and reload#
The stock default file in sites-enabled has default_server on port 80, so requests with an unknown host name land there. You have two choices. Remove the link (sudo rm /etc/nginx/sites-enabled/default, the original stays in sites-available), or mark your block instead:
listen 80 default_server;Only one block per port may be default_server, or nginx -t fails with a duplicate error. Then apply it:
sudo systemctl reload nginxCheck it worked#
curl -I http://example.com
curl -s http://example.com
sudo tail -n 5 /var/log/nginx/access.logThe headers show 200 OK, the body shows your heading, and the access log shows the requests. Before DNS exists you can test with curl -H 'Host: example.com' http://203.0.113.10. Details on log reading are in read logs with journalctl, and the nginx-specific files are /var/log/nginx/access.log and /var/log/nginx/error.log.
Edge cases and gotchas#
- A symlink needs an absolute path.
ln -s sites-available/example.com sites-enabled/creates a broken link, because the link is resolved from its own folder. Use the full/etc/nginx/sites-available/...path. - A missing semicolon breaks the whole file. Every directive ends with
;. The error names the line after the real mistake, so look one line up. rootinsidelocationrepeats work. Putrootat server level so every location shares it, and override only where needed.nginx -tdoes not test DNS or ports. It checks syntax and file paths. A port already held by another program fails only at reload or start.- Editing
sites-enableddirectly works but hides the original. Keep the real file insites-availableso you can disable a site by removing a link. - Reload is not instant for long connections. Old workers stay until their connections close, so a stuck download can keep an old config alive for a while.
- Directory listings are off. A folder without an index file returns 403 or 404 unless you add
autoindex on;, which you rarely want on a public site.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
nginx: [emerg] unexpected "}" in /etc/nginx/sites-enabled/example.com:12 | Missing ; or brace above the line | Fix the file and line named, run nginx -t again |
bind() to 0.0.0.0:80 failed (98: Address already in use) | Apache or another proxy holds port 80 | Find it with sudo ss -tlnp, stop it. See fix port already in use |
403 Forbidden | No index file, or www-data cannot read the path | Add index.html, fix permissions on every parent folder |
404 Not Found for a file that exists | Wrong root or typo in the path | Check error.log, it prints the full path nginx tried |
| “Welcome to nginx” shows for your domain | Default block matched | Match server_name, remove the default link |
duplicate default server for 0.0.0.0:80 | Two blocks use default_server | Keep it on one block only |
nginx: [warn] conflicting server name "example.com" on 0.0.0.0:80, ignored | Two blocks list the same name | Remove the duplicate from one file |
Job for nginx.service failed after reload | Config error or port clash | Run sudo nginx -t and journalctl -u nginx -n 30 |
Quick reference#
sudo apt install nginx
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t # syntax check
sudo systemctl reload nginx # apply without dropping connections
sudo systemctl restart nginx # full restart, cuts connections
sudo tail -f /var/log/nginx/access.log # live requests
sudo tail -n 50 /var/log/nginx/error.log
curl -I http://example.com # headers onlyRelated lessons#
- What is a reverse proxy: the next job nginx does after serving files.
- Run a service with systemd: how nginx is started and reloaded.
- Nginx reverse proxy with free HTTPS using certbot: the short path to HTTPS.
- Self-host a static site with Docker: the same idea inside a container.
Next#
Static files are only half the job, so continue with Nginx reverse proxy for a Node app.
Try it: Test, reload and check nginx
Check a new server block, apply it and confirm it serves.
Want a real machine? Free browser labs: Killercoda
Check what you learned
Frequently asked questions
Where is the nginx config file on Ubuntu?
The main file is /etc/nginx/nginx.conf. Site files live in /etc/nginx/sites-available and are activated by symlinks in /etc/nginx/sites-enabled.
What is the difference between nginx reload and restart?
Reload applies the new config without dropping connections, and keeps the old config if the new one is invalid. Restart stops the process and starts it again, which cuts live connections.
How do I test an nginx config without restarting?
Run sudo nginx -t. It parses every included file and prints whether the syntax is ok and the test is successful.
Why do I see the Welcome to nginx page instead of my site?
The request matched the default server block. Check that server_name matches the host you typed and remove the default symlink from sites-enabled.
Sources
Keep going: guides and tutorials
- Self-Hosted Static Site Hosting: Deploy a Folder With Levelrail Self-hosted static site hosting from a plain folder: one deploy command, _redirects, _headers and a custom 404 honored, plus a one command rollback.
- Deploy Next.js on a VPS With Docker, Git Push and HTTPS Deploy a Next.js app to your own VPS with Docker, a health check, HTTPS and rollback. Includes the NEXT_PUBLIC build trap that leaves variables empty.