How to Configure UFW Firewall Rules for a Web Server (Step-by-Step)

This guide shows how to configure ufw firewall web server rules on Ubuntu, step by step, so a web VPS accepts web traffic and SSH while denying everything else. UFW is the simplest safe front end for the Linux firewall, and a web server needs only a short rule list. Follow the order below exactly: allow SSH first, then web ports, then enable the firewall. Enabling it before allowing SSH is the classic lockout, and it happens to experienced admins too.

Before you start, confirm you have an SSH session open and a way back in, such as provider console access, in case a rule goes wrong. Also confirm which web server you run and whether it needs HTTP only or HTTPS too, since the rules differ by one port. A plain HTTP site needs port 80. A site with TLS needs port 443 as well, and most sites today need both, since port 80 redirects visitors to the secure version.

Configure UFW Firewall Web Server: The Default-Deny Base

A good firewall starts by denying everything, then opening only named holes. UFW makes that stance easy: set the default incoming policy to deny and the default outgoing policy to allow. Outgoing allow keeps updates, package downloads, and API calls working without extra rules. Incoming deny means a new service you install later stays unreachable until you deliberately open its port, which is the safe direction to fail in.

The ufw default deny incoming stance is expressed in one command:

sudo ufw default deny incoming
sudo ufw default allow outgoing

Run those two commands before enabling anything. They set policy only; they do not activate the firewall yet, so a mistake here costs nothing until you enable. Check the plan once more: SSH, HTTP, and HTTPS open, everything else closed.

Step 1: Allow SSH Before Anything Else

Allow SSH first, using the port your server actually listens on. If you moved SSH to a custom port, allow that number, not port 22. If you still use the standard port, the OpenSSH profile covers it. Test that the rule appears in the status output before moving on. This step is the whole difference between a smooth setup and a lockout, so do not skip the status check.

sudo ufw allow OpenSSH
sudo ufw status verbose

If your SSH port is custom, allow the number instead of the profile, and confirm the number matches the SSH daemon setting. A mismatch here means the firewall allows a port nothing listens on while blocking the real one. Our guide to set up a firewall with UFW on Ubuntu Server 24.04 covers the standard and custom-port cases side by side, which helps if you changed ports earlier.

Step 2: Allow HTTP and HTTPS

Next, open the web ports. UFW ships with web server profiles that bundle the right ports, and the ufw allow nginx profile shortcut opens both HTTP and HTTPS for an Nginx server in one command. For Apache, the matching profile is Apache Full. If you run no named profile or you want explicit control, allow ports 80 and 443 by number instead. Both routes end at the same rules; profiles only save typing and reduce typo risk.

sudo ufw allow 'Nginx Full'
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Use either the profile or the port numbers, not both, since duplicates clutter the rule list. After adding the rules, check status again. You should see SSH plus the web rules and nothing else unexpected. If a rule looks wrong, delete it by number and re-add it rather than stacking corrections.

Step 3: Enable UFW and Verify From Outside

Now enable the firewall. UFW warns that enabling may disrupt existing SSH connections; that warning is normal, and your current session survives because its connection is already established. Confirm the enable, then verify from a second place: load the site in a browser, fetch it with a command-line tool from another machine, and open a fresh SSH session. All three should work. Then test the negative case: a port you did not open, such as a database port, should refuse connections from outside.

sudo ufw enable
sudo ufw status numbered

The numbered status output is your review tool from here on. Rules have numbers, and deleting by number beats deleting by description when duplicates exist. Save a copy of this output in your server docs as the known-good baseline, so a future change can be compared against it.

Rules for Common Web Server Extras

Most web servers need a few extra rules over time. A control panel needs its panel port opened, and only to your address if the panel supports address limits. A mail service needs mail ports, but only if the server actually sends or receives mail; many web servers send through an external provider and need no inbound mail ports at all. A monitoring agent that pulls from outside needs its port, while one that pushes data out needs none. Each extra rule should name a reason and an owner, so review day can tell needed rules from forgotten ones.

For the full stack around the firewall, our guide to install WordPress on a VPS with a LEMP stack shows where the firewall sits among the web server, PHP, and database layers. And if you issue TLS certificates on the server, our guide to get free SSL certificates with Let’s Encrypt explains the port 80 challenge flow, which is why port 80 must stay open even on HTTPS-only sites during renewal.

Rate Limiting SSH and Logging

UFW can rate-limit SSH to slow brute-force scripts: the limit rule allows a few connections, then denies further attempts from that address for a while. Apply it to SSH only, never to web ports, since rate-limiting a public website blocks real visitors behind shared addresses. Turn logging on at a low level while testing, watch denied packets for a day, then lower the level to keep logs small. A firewall log full of denied scans is normal; a denied packet to a port you meant to open is a misconfiguration worth fixing the same day.

Keep the firewall in step with the rest of the baseline. Our first 10 things to do after buying a VPS checklist puts firewall setup next to user accounts and updates, and our guide to set up automatic security updates on Ubuntu and Debian covers the update half, since a firewall with unpatched services behind it is only half a defense.

Common Mistakes

  • Enabling UFW before allowing SSH, which locks out the admin session.
  • Allowing a profile that opens more ports than the service needs.
  • Opening database ports to the world instead of limiting them to the app server.
  • Forgetting port 80, which breaks TLS certificate renewal on HTTPS sites.
  • Never reviewing rules, so temporary ports become permanent holes.

Review the numbered rules monthly and after every app install. A firewall review takes minutes and catches drift before a scan does. With SSH, HTTP, and HTTPS open and everything else denied, a web VPS has the firewall posture it needs, and every future rule should justify itself against that baseline.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.