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
- Update the package index and install:
sudo apt update sudo apt install -y nginx - Enable and confirm the service is running:
sudo systemctl enable --now nginx systemctl is-active nginx - Open the firewall. UFW ships profiles for Nginx out of the box:
If you only need TLS, usesudo ufw app list sudo ufw allow 'Nginx Full' # 80 + 443 sudo ufw reloadNginx HTTPSinstead. See UFW firewall basics for the wider firewall posture. - Confirm version, modules, and config syntax:
nginx -V 2>&1 | tr ' ' '\n' | grep -E '^--with' sudo nginx -tnginx -Vwrites to stderr — the redirect is intentional. The--withflags tell you which modules were compiled in (e.g.http_ssl_module,http_v2_module,http_realip_module). - Smoke-test from another host. Hitting
curl -I http://<server-ip>/should return200 OKwithServer: nginx/1.x.x. - 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 - Pin the version in your runbook.
dpkg-query -W -f='${Version}\n' nginxgives the exact build; record it next to the date so future drift is obvious.
Common pitfalls
- The default site greedily binds
default_serveron port 80. If you forget to disable it and add a server block that omitsdefault_server, the default keeps catching unmatched Host headers. nginx -s reloadversussystemctl reload nginx— both work, butsystemctlis preferred under automation because it returns a meaningful exit code if the master is not running.ufw allow 80works but does not tag the rule; using theNginx Fullprofile makes audits readable.- Installing
nginx-extraspulls in modules you may not want (Lua, GeoIP, etc.); for most clients we install plainnginxand 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 alongsideworker_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.