Skip to content

Installing Nginx on Ubuntu — packages, firewall, and the first sanity check

A clean install of Nginx on Ubuntu LTS — apt package vs upstream repo, UFW rules, default site layout, and the smoke tests that catch a broken install before it ships.

Getting Nginx running on a fresh Ubuntu host is a five-minute job, but doing it cleanly — package source decided, firewall opened, default site removed, version pinned in the runbook — is what separates a server you can hand off from one you can’t. This article walks the install on Ubuntu 22.04/24.04 and the verification an operator should do before pointing DNS at the box.

How to verify

Before installing, confirm what is on the box and what is listening:

lsb_release -a
dpkg -l | grep -E 'nginx|apache2'
ss -ltnp | grep -E ':80|:443'
sudo systemctl list-units --type=service --state=running | grep -i web

If Apache is present and active on port 80, decide now whether to remove it (sudo apt purge apache2) or move it to a non-overlapping port — they cannot both bind 80 at once.

What’s happening

Ubuntu ships two practical Nginx packages: nginx from the distro archive (stable, lagging upstream by a release or two) and nginx-mainline from the Nginx team’s PPA (newer, lines up with upstream releases). For production we install the distro package by default — it gets security backports through unattended-upgrades and integrates with the system’s nginx.service unit. We only switch to the upstream repo when we need a specific module or a fix that has not yet been backported.

The distro package lays the config out under /etc/nginx, with sites-available and sites-enabled symlinked the same way Debian does. The default nginx.service unit drops the master process as root (needed to bind 80/443) and workers as the www-data user.

The procedure

  1. Update the package index and install:
    sudo apt update
    sudo apt install -y nginx
  2. Enable and confirm the service is running:
    sudo systemctl enable --now nginx
    systemctl is-active nginx
  3. Open the firewall. UFW ships profiles for Nginx out of the box:
    sudo ufw app list
    sudo ufw allow 'Nginx Full'   # 80 + 443
    sudo ufw reload
    If you only need TLS, use Nginx HTTPS instead. See UFW firewall basics for the wider firewall posture.
  4. Confirm version, modules, and config syntax:
    nginx -V 2>&1 | tr ' ' '\n' | grep -E '^--with'
    sudo nginx -t
    nginx -V writes to stderr — the redirect is intentional. The --with flags tell you which modules were compiled in (e.g. http_ssl_module, http_v2_module, http_realip_module).
  5. Smoke-test from another host. Hitting curl -I http://<server-ip>/ should return 200 OK with Server: nginx/1.x.x.
  6. Disable the default site once you have a real server block to publish:
    sudo rm /etc/nginx/sites-enabled/default
    sudo nginx -t && sudo systemctl reload nginx
  7. Pin the version in your runbook. dpkg-query -W -f='${Version}\n' nginx gives the exact build; record it next to the date so future drift is obvious.

Common pitfalls

  • The default site greedily binds default_server on port 80. If you forget to disable it and add a server block that omits default_server, the default keeps catching unmatched Host headers.
  • nginx -s reload versus systemctl reload nginx — both work, but systemctl is preferred under automation because it returns a meaningful exit code if the master is not running.
  • ufw allow 80 works but does not tag the rule; using the Nginx Full profile makes audits readable.
  • Installing nginx-extras pulls in modules you may not want (Lua, GeoIP, etc.); for most clients we install plain nginx and add modules deliberately.
  • The default config sets worker_connections 768 — fine for a small VM, but a load-balancer host will need that tuned up alongside worker_rlimit_nofile.

In the engagements we run, the first Nginx install is the boring part — the runbook captures the package source, the modules in use, the firewall posture, and the rollback steps. That repeatability is what we offer as managed operations for clients who would rather not own the toil.