Security & Configuration

UFW Firewall Rules for Web Servers

Build and verify a minimal UFW policy for web servers while preserving legitimate administration and internal service access.

A host firewall reduces the number of network services reachable from outside the server. For a typical web host, public access is usually limited to HTTP and HTTPS, while SSH is restricted to administrator networks where practical. Database, cache, control-panel, and internal application ports should not be exposed merely because a service is listening.

Inventory before changing rules

First identify active listeners and the current firewall state:

sudo ss -tulpn
sudo ufw status verbose

Confirm the SSH port and the provider's console or recovery access. If a hosting control panel manages firewall rules, use its supported interface or coordinate changes with that system. Competing firewall managers can overwrite one another's configuration.

Build a minimal policy

A basic UFW policy might be:

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow from 203.0.113.10 to any port 22 proto tcp

Replace the example address with a real, stable administrator address. If administrators connect from changing networks, choose a deliberate alternative rather than accidentally locking out all access. Enable the firewall only after the required management rules have been added and checked.

Validate from outside the server

After applying rules, test SSH from a second terminal and check website access from an external network. Confirm that database ports such as 3306 and cache ports such as 6379 are not publicly reachable unless a documented requirement exists. A local ss result shows what is listening; an external connection test shows what the network actually exposes.

Common failure modes

Cloud firewalls, provider security groups, router ACLs, and UFW may all apply at once. A port can be allowed in one layer and blocked in another. Conversely, allowing a port at every layer can unintentionally publish an internal service. Keep a change record, review rules periodically, and remove temporary access after maintenance. Firewall rules are only one layer: services still need authentication, updates, and secure configuration.

Share this page Help other developers discover this resource.