Short answer: A firewall decides which network traffic is allowed in and out, using rules such as "allow web traffic on ports 80 and 443, block everything else". A network firewall protects a whole network, a host firewall runs on one server, and a web application firewall (WAF) inspects website requests for attacks like SQL injection. Websites are safest with layers: a strict server firewall plus a WAF.
A firewall is one of the oldest ideas in network security and still one of the most useful: most attacks on a server start with something reachable that didn't need to be.
What a firewall actually does
Every packet arriving at a server has a source address, a destination port and a protocol. A firewall compares those against its rules, in order, and allows or drops the packet.
Modern firewalls are stateful: they remember connections. When your server makes a request out (to download updates, say), the firewall lets the replies back in automatically, without a rule opening that port to everyone.
The safe pattern on any server is default deny: block all incoming traffic, then allow only the services you use. See network ports explained for which ports those usually are.
The main types
| Type | Where it runs | What it looks at | Example |
|---|---|---|---|
| Network firewall | At the edge of a network: a router, a provider's network | Addresses, ports, protocols for many machines | Office router, hosting network edge |
| Host firewall | On the server itself | Traffic to that one machine | UFW, firewalld, nftables, Windows Firewall |
| Web application firewall | In front of a website | The content of HTTP requests | ModSecurity, cloud WAF services |
| DDoS filtering | At the provider or a scrubbing service | Floods of traffic | See DDoS protection |
A network or host firewall knows who is connecting and to which port, but not whether a request to port 443 is a normal page view or an attack hidden inside it. That is the WAF's job: it reads the request itself and blocks known attack patterns, such as SQL injection and cross-site scripting. See web application firewalls.
A safe default on a VPS
On Ubuntu and Debian, UFW is the simplest way to manage the server's firewall. Allow SSH before you turn it on, or you will lock yourself out:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose
Better still, allow SSH only from your own address: sudo ufw allow from 203.0.113.24 to any port 22 (use your public IP from What is my IP?). Our UFW firewall guide covers rate limiting, IPv6 and common mistakes.
On shared hosting you don't manage the server firewall: your host runs it for every site on the server, and you control your site's own security settings.
What a firewall can't do
- It can't fix vulnerable software. If a plugin has a flaw, an attack through port 443 looks like normal web traffic to a network firewall. Keep software updated; see website security basics.
- It can't stop stolen passwords. A valid login is allowed in. Use strong passwords and two-factor authentication.
- It needs looking after. Rules added "for a quick test" and never removed are a common way servers end up exposed. Review them from time to time.
Frequently asked questions
Do I need a firewall on my server if my host has one?
Yes, if you run your own VPS or dedicated server. The provider's network protections help, but a host firewall on the server is what decides exactly which of your services are reachable.
What is the difference between a firewall and antivirus?
A firewall controls network traffic; antivirus and malware scanners look for harmful files and programs already on a machine. See scanning a website for malware.
Does a firewall slow down my website?
A host firewall adds no noticeable delay. A WAF inspects each request, which costs a tiny amount of time, usually far less than it saves by blocking bad traffic.
Is a WAF enough on its own?
No. A WAF protects the website layer; the server still needs a firewall that blocks everything else, and the software behind it still needs updates.



