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

Choose Your Platform

This track uses Ubuntu LTS and Debian commands (apt). On Rocky, Alma, or Fedora:

bash
sudo dnf install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx

Paths 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

bash
sudo apt update
sudo apt install -y nginx

Code explanation:

  • apt update refreshes package indexes
  • nginx meta-package pulls the stable open-source build from your distro

Enable and start the service:

bash
sudo systemctl enable nginx
sudo systemctl start nginx

Check version and service state:

bash
nginx -v
sudo systemctl status nginx --no-pager

Expected: active (running) and a version string like nginx/1.24.x.

Verify HTTP Works Locally

bash
curl -I http://127.0.0.1/

Code explanation:

  • -I requests headers only—you should see HTTP/1.1 200 OK and Server: 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/:

bash
echo '<h1>hello_code Nginx</h1>' | sudo tee /var/www/html/index.html
curl http://127.0.0.1/

Code explanation:

  • tee writes the file as root; Nginx reads it as the www-data user
  • Replacing index.html confirms 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

PathPurpose
/etc/nginx/nginx.confMain 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.typesFile extension → MIME type map
/var/log/nginx/access.logRequest log
/var/log/nginx/error.logErrors and warnings
/run/nginx.pidMaster process PID (path may vary)

Typical flow on Ubuntu:

text
/etc/nginx/nginx.conf
    └── include /etc/nginx/sites-enabled/*;
            └── symlink → /etc/nginx/sites-available/default

Code explanation:

  • sites-available holds all site configs you maintain
  • sites-enabled contains 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:

bash
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak

Peek at the Main Config

bash
sudo less /etc/nginx/nginx.conf

You will see blocks like:

Code explanation:

  • user — workers drop privileges to this account
  • worker_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:

bash
sudo nginx -t

Success output:

text
nginx: configuration file /etc/nginx/nginx.conf test is successful

Apply changes without dropping all connections:

bash
sudo systemctl reload nginx
CommandEffect
reloadReread config; graceful worker handoff
restartStop and start entire service; brief downtime
stop / startFull 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:

bash
curl -I http://127.0.0.1/
curl -v http://127.0.0.1/ 2>&1 | head -20

Code explanation:

  • -I — status line and response headers
  • -v — verbose; shows request/response exchange for debugging

Test a specific hostname locally (before DNS exists):

bash
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

SymptomLikely causeFix
Unit nginx.service not foundInstall failed or wrong OSRe-run apt install nginx; check dpkg -l nginx
Address already in usePort 80 taken by Apache or another appsudo ss -tlnp | grep :80; stop conflicting service
403 Forbidden on default pageWrong permissions on web rootls -la /var/www/html; ensure files are world-readable
nginx: command not foundPackage not installedInstall nginx; use full path /usr/sbin/nginx -v
Cannot reach from browser on VPSFirewall / cloud security groupOpen port 80; see Linux networking

Post-Install Checklist

  • nginx -v prints a version
  • systemctl status nginx shows 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 -t and sudo 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?

bash
sudo systemctl stop nginx
sudo apt remove nginx

Config 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.