Security & SSL

How to Configure a Firewall on Your Linux Web Server

Learn how to set up a firewall on your Linux web server using UFW and iptables to block unauthorized access and keep your sites secure.

Close-up of romantic love padlocks hanging on a metal wire mesh fence by a river.

To configure a firewall on a Linux web server, the fastest path is to use UFW (Uncomplicated Firewall) on Ubuntu/Debian or firewalld on CentOS/RHEL — allow only ports 22, 80, and 443, and deny everything else by default.

A server without a firewall is an open door. Every unnecessarily exposed port can become an attack vector. This guide takes you from zero to a working firewall in under 15 minutes, regardless of whether you run Apache or Nginx.

Why Your Web Server Needs a Firewall

When a server connects to the Internet, all its ports are reachable in principle. Without a firewall to filter traffic:

  • Bots continuously scan and attack open ports — SSH on port 22 is the most targeted.
  • Internal services like MariaDB/MySQL or Redis are exposed to the public internet.
  • An exploit in a secondary service can compromise the entire server.

A properly configured firewall is the first line of defense and the cheapest control you have. You can layer additional protection on top — visit elenlace.com to see security plans that combine firewalls, WAF, and monitoring.

UFW: The Easiest Option on Ubuntu and Debian

UFW is a front-end for iptables designed for simplicity. It comes pre-installed on Ubuntu; if it's missing, install it with apt install ufw.

Initial setup

# 1. Set default policy: deny all incoming traffic
sudo ufw default deny incoming
sudo ufw default allow outgoing

# 2. Allow SSH BEFORE enabling the firewall (don't lock yourself out!)
sudo ufw allow 22/tcp

# 3. Allow web traffic
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# 4. Enable the firewall
sudo ufw enable

# 5. Verify the status
sudo ufw status verbose

With this setup, only SSH, HTTP, and HTTPS are accessible. Everything else is silently dropped.

Additional useful rules

  • Rate-limit SSH attempts: sudo ufw limit 22/tcp — blocks IPs that exceed 6 attempts in 30 seconds.
  • Allow passive FTP: sudo ufw allow 21/tcp plus the passive port range (e.g., 49152:65535/tcp).
  • Block a specific IP: sudo ufw deny from 203.0.113.0.

firewalld: The Standard on CentOS, AlmaLinux, and Rocky

On Red Hat-derived distributions, firewalld is the default manager. It works with zones; the public zone is the default for internet-facing interfaces.

# Verify firewalld is running
sudo systemctl status firewalld

# Allow services using semantic names
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --permanent --zone=public --add-service=ssh

# Reload to apply permanent changes
sudo firewall-cmd --reload

# Verify
sudo firewall-cmd --list-all

To close a port you don't need (for example, 8080): sudo firewall-cmd --permanent --zone=public --remove-port=8080/tcp && sudo firewall-cmd --reload.

Essential Rules for a Web Server

Port Protocol Purpose Recommendation
22 TCP SSH Allow + rate-limit
80 TCP HTTP Allow (redirect to HTTPS)
443 TCP HTTPS Allow
3306 TCP MySQL/MariaDB Block (local access only)
6379 TCP Redis Block (local access only)
25, 465, 587 TCP SMTP Only if the server sends email

The golden rule: if you don't need it from the internet, close it. Database services should never listen on public interfaces.

Best Practices After Enabling the Firewall

  • Change the default SSH port: moving SSH from 22 to 2222 (or any high port) dramatically reduces bot noise. Remember to open the new port before closing 22.
  • Install fail2ban: it complements the firewall by banning IPs with repeated authentication failures.
  • Keep an emergency rescue plan: most VPS providers offer a web console (noVNC, KVM) via their control panel — use it if you accidentally lock yourself out.
  • Audit rules periodically: run sudo ufw status numbered or sudo firewall-cmd --list-all to review what's open.

For deeper protection, combine the OS firewall with a Web Application Firewall (WAF) that filters malicious traffic at the application layer — SQL injection payloads, XSS attempts, and more.

Key Takeaways

  • Use UFW on Ubuntu/Debian and firewalld on CentOS/AlmaLinux; both simplify iptables management.
  • The default policy must be deny incoming — you only open what you actually need.
  • Allow SSH before enabling the firewall so you don't lose access.
  • Keep database ports (3306, 6379) closed to external traffic.
  • Pair the firewall with fail2ban and, for production sites, a layer-7 WAF.

Want a server that comes pre-configured with firewall, SSL, and monitoring? The team at elenlace.com sets up the full environment so you can focus on your business.

FAQ

Are UFW and iptables the same thing?

Not exactly. UFW is a simplified interface that translates your commands into iptables rules under the hood. You can use iptables directly, but UFW is more readable and less error-prone for most use cases.

Can I rely on my hosting provider's firewall instead of configuring my own?

If you're on shared hosting, you don't have access to server-level firewall configuration — that's managed by the provider. A self-managed firewall is relevant when you have a VPS or dedicated server. On shared hosting, focus on WAF rules and .htaccess hardening instead.

Will blocking port 3306 break my PHP application?

No, as long as PHP and MySQL are on the same server. Local connections (via 127.0.0.1 or a Unix socket) don't pass through the network firewall. You're only blocking connections coming in from outside the server.

What if I lose SSH access due to a misconfigured rule?

Most VPS providers offer a web-based console (noVNC, VNC, KVM) accessible from their control panel. Log in through that, fix the firewall rules, and reconnect via SSH normally.

Compare providers

Other providers and guides worth comparing:

← All