Lab note · Hardening
Hardening a small self-hosted web stack
A small self-hosted setup usually looks like this: one Linux server, a handful of containers, a reverse proxy in front and a few websites or tools behind it. It is cheap and flexible. It is also easy to leave something exposed that you never meant to publish. This is the checklist I work through before anything goes public. It is deliberately generic.
1. One front door: the reverse proxy
Only the reverse proxy should listen on the public ports, 80 and 443. Everything else sits behind it. That gives one place to handle TLS, headers, logins and logging, instead of trusting every app to get it right.
Check what is actually listening, not what you think is listening:
ss -ltnp
Anything bound to 0.0.0.0 or [::] that is not the proxy or SSH deserves a hard look.
2. Bind containers to localhost
This one catches a lot of people. Publishing a container port with -p 8080:80 binds it to every interface, and container runtimes often add their own firewall rules that sidestep a host firewall like ufw. Your "firewalled" admin panel can be wide open.
# exposed to the internet:
-p 8080:80
# only reachable from the server itself:
-p 127.0.0.1:8080:80
Better still, put the app on an internal container network the proxy shares, and publish no port at all. Then scan your own server from outside to prove it.
3. TLS everywhere, done automatically
Use a proxy that requests and renews certificates on its own, redirect plain HTTP to HTTPS, and turn on HSTS once you are sure every name works over HTTPS. Before requesting a certificate for a new name, check that DNS points only at your server from more than one resolver; a stray old record makes validation flaky.
4. Security headers
Headers cost nothing and close off whole classes of problems. My baseline for a static site:
Strict-Transport-Security: max-age=31536000; includeSubDomains
Content-Security-Policy: default-src 'self'; frame-ancestors 'none'
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
A strict Content Security Policy is easiest if you write the site that way from the start: no inline scripts or styles, no third-party fonts or trackers. Retrofitting it later is painful. Internal tools also get X-Robots-Tag: noindex, nofollow so they do not end up in search results.
5. A login in front of every admin tool
Dashboards, monitoring panels, database admin pages and remote desktops should never be reachable without a login, even if the tool has its own. Some tools let the first visitor create the admin account, which turns a fresh install into a race. Put basic authentication or single sign-on at the proxy before the route goes live, then finish the tool's own setup yourself.
Use long random passwords, keep them in a root-only file or a password manager, and never put them in a URL, where they leak into logs and browser history.
6. No directory listings
Quick file servers often list a folder's contents when there is no index page. If you share files by link, make sure the folder itself returns nothing useful, and prefer long, unguessable paths for anything private.
7. DNS hygiene: CAA and no dangling records
A CAA record limits which certificate authorities may issue for your domain. Pair it with regular checks for records that still point at services you stopped using. Those are takeover risks, as I wrote about in my dangling DNS note.
8. Back up before every change
Before editing proxy configuration or anything that serves live traffic:
- Take a dated backup of the files you are about to touch.
- Record what "working" looks like first: the status codes of the important pages.
- Add new configuration in new files where you can, instead of editing old ones.
- Re-check the same pages after the change. If anything differs, remove your change and restore.
It sounds slow. In practice it makes changes faster, because you are never afraid of them.
9. Keep it updated and keep watching
- Automatic security updates for the operating system, and pinned, regularly reviewed container image tags.
- SSH with keys only and a tool that bans repeated failed logins.
- An uptime check on every public site, and someone who actually reads the alerts.
None of this makes a server unbreakable. It makes it a much worse target than the next one, and it means that when something does go wrong, you notice and can put it back.