Installing Nginx and Directory Layout
Introduction
Before you write virtual hosts or reverse proxies, Nginx must be installed and you need to know where configs, logs, and web roots live on disk. This chapter installs Nginx on Ubuntu/Debian, verifies the service, maps the important paths, and establishes habits—nginx -t before every reload—that prevent broken production sites.
Prerequisites
- What Is Nginx
- A Linux environment: VPS, VM, or WSL2 with Ubuntu
sudoaccess for package install and editing/etc/nginx/- Basic familiarity with processes and systemd helps but is not required for install steps
Choose Your Platform
This track uses Ubuntu LTS and Debian commands (apt). On Rocky, Alma, or Fedora:
sudo dnf install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginxPaths are similar (/etc/nginx/), but site layout may use conf.d/*.conf only—no sites-available split on some RHEL images. Ubuntu/Debian steps below are the default in examples.
Install on Ubuntu / Debian
sudo apt update
sudo apt install -y nginxCode explanation:
apt updaterefreshes package indexesnginxmeta-package pulls the stable open-source build from your distro
Enable and start the service:
sudo systemctl enable nginx
sudo systemctl start nginxCheck version and service state:
nginx -v
sudo systemctl status nginx --no-pagerExpected: active (running) and a version string like nginx/1.24.x.
Verify HTTP Works Locally
curl -I http://127.0.0.1/Code explanation:
-Irequests headers only—you should seeHTTP/1.1 200 OKandServer: nginx- Confirms Nginx listens on port 80 before you change any config
Open in a browser on the same machine: http://127.0.0.1/ or http://localhost/. On a remote VPS, use the public IP only if your firewall allows port 80.
Default Welcome Page and Web Root
Debian/Ubuntu often place the default site at /var/www/html/:
echo '<h1>hello_code Nginx</h1>' | sudo tee /var/www/html/index.html
curl http://127.0.0.1/Code explanation:
teewrites the file as root; Nginx reads it as thewww-datauser- Replacing
index.htmlconfirms you are hitting the expected document root
Some packages also ship sample files under /usr/share/nginx/html/—your active root directive in config decides which directory is used.
Configuration Directory Map
| Path | Purpose |
|---|---|
/etc/nginx/nginx.conf | Main config; includes other files |
/etc/nginx/sites-available/ | Site definition files (Debian/Ubuntu) |
/etc/nginx/sites-enabled/ | Enabled sites (symlinks into sites-available) |
/etc/nginx/conf.d/ | Drop-in .conf snippets (all distros) |
/etc/nginx/mime.types | File extension → MIME type map |
/var/log/nginx/access.log | Request log |
/var/log/nginx/error.log | Errors and warnings |
/run/nginx.pid | Master process PID (path may vary) |
Typical flow on Ubuntu:
/etc/nginx/nginx.conf
└── include /etc/nginx/sites-enabled/*;
└── symlink → /etc/nginx/sites-available/defaultCode explanation:
sites-availableholds all site configs you maintainsites-enabledcontains only symlinks to configs that should be live- Disabling a site = remove symlink, reload—not necessarily delete the file
Tip
Backup Before Editing
Copy before change:
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bakPeek at the Main Config
sudo less /etc/nginx/nginx.confYou will see blocks like:
Code explanation:
user— workers drop privileges to this accountworker_processes auto— one worker per CPU core (typical)http { }— all virtual hosts and proxies live inside this context (detailed in the next chapter)
Do not change nginx.conf heavily on day one—add sites under sites-available instead.
Test Config and Reload
Always validate syntax before applying:
sudo nginx -tSuccess output:
nginx: configuration file /etc/nginx/nginx.conf test is successfulApply changes without dropping all connections:
sudo systemctl reload nginx| Command | Effect |
|---|---|
reload | Reread config; graceful worker handoff |
restart | Stop and start entire service; brief downtime |
stop / start | Full stop or cold start |
Use reload after config edits when nginx -t passes.
Warning
Never Skip nginx -t
A bad config can leave Nginx unable to restart. Always test first; fix errors before reload.
Useful curl Checks
After changes:
curl -I http://127.0.0.1/
curl -v http://127.0.0.1/ 2>&1 | head -20Code explanation:
-I— status line and response headers-v— verbose; shows request/response exchange for debugging
Test a specific hostname locally (before DNS exists):
curl -I -H "Host: mysite.test" http://127.0.0.1/Useful once you add server_name mysite.test; in a virtual host.
Common Install Problems
| Symptom | Likely cause | Fix |
|---|---|---|
Unit nginx.service not found | Install failed or wrong OS | Re-run apt install nginx; check dpkg -l nginx |
Address already in use | Port 80 taken by Apache or another app | sudo ss -tlnp | grep :80; stop conflicting service |
403 Forbidden on default page | Wrong permissions on web root | ls -la /var/www/html; ensure files are world-readable |
nginx: command not found | Package not installed | Install nginx; use full path /usr/sbin/nginx -v |
| Cannot reach from browser on VPS | Firewall / cloud security group | Open port 80; see Linux networking |
Post-Install Checklist
-
nginx -vprints a version -
systemctl status nginxshows active (running) -
curl -I http://127.0.0.1/returns 200 - You know paths:
/etc/nginx/,/var/log/nginx/,/var/www/html/ - You can run
sudo nginx -tandsudo systemctl reload nginx
Relationship to the Linux Track
Linux chapter 15 jumps quickly to proxy_pass and deploy. This Nginx track starts here with install and layout so later chapters on HTTPS, routing, and projects assume you already know where files live and how to reload safely.
FAQ
Do I need Nginx on WSL2?
Yes for hands-on config practice. Install with apt inside WSL; browse via http://127.0.0.1/ from Windows.
Which user runs Nginx workers?
On Debian/Ubuntu, usually www-data. App files in root must be readable by that user (or use ACLs).
sites-available vs conf.d?
Both work. Ubuntu tradition uses sites-available + symlinks; many examples use conf.d/mysite.conf directly. Pick one style per server.
How do I uninstall?
sudo systemctl stop nginx
sudo apt remove nginxConfig under /etc/nginx/ may remain unless you apt purge nginx.
Can I run multiple Nginx versions?
Distro package is enough for learning. Custom builds or Docker images are advanced topics.
Default site still shows after I add a new config?
Another server block may be default_server on port 80. Later chapters cover listen 80 default_server and server_name matching.
Is SELinux an issue?
On RHEL with SELinux enforcing, custom web roots need correct context (httpd_sys_content_t). Ubuntu learners rarely hit this; RHEL deploys should check audit.log.