How to Set Up Firewalls on Web Hosting
This guide explains how to set up firewalls on web hosting, covering both shared hosting (via control panel tools) and VPS/dedicated servers (via command-line tools like UFW or firewalld). You’ll learn what each type of firewall protects, how to configure basic rules, and how to avoid common pitfalls that lock you out.
What You’ll Need
- Access to your hosting control panel (cPanel, Plesk, or your host’s custom panel) or SSH access to your server.
- Administrative (root or sudo) privileges on a VPS/dedicated server.
- A list of the ports your services use (e.g., 80 for HTTP, 443 for HTTPS, 22 for SSH).
- Your current IP address (in case you need to whitelist it for SSH).
Step-by-Step
For Shared Hosting (Control Panel Firewall)
- Log in to your hosting control panel. Go to the security or firewall section. In cPanel, look for “Firewall” or “ConfigServer Security & Firewall” (CSF). In Plesk, look for “Firewall” under “Tools & Settings.”
- Enable the firewall if it’s off. Most shared hosts have a basic firewall already active. If you see a toggle, turn it on.
- Add allowed ports. By default, ports 80 (HTTP) and 443 (HTTPS) are open. If you need other ports (e.g., 21 for FTP, 25 for SMTP), add them to the allowed list. Do not open ports you don’t need.
- Block specific IPs (optional). If you’ve seen suspicious traffic from a specific IP, add it to the deny list. Be careful not to block your own IP.
- Save and apply. Click “Save” or “Apply” to activate the changes. The firewall will now filter traffic according to your rules.
For VPS/Dedicated Server (UFW on Ubuntu/Debian)
- Connect via SSH. Use a terminal (Linux/macOS) or PuTTY (Windows) to log in as root or a user with sudo privileges.
- Check if UFW is installed. Run
sudo ufw status. If it says “command not found,” install it withsudo apt update && sudo apt install ufw. - Set default policies. Run
sudo ufw default deny incomingandsudo ufw default allow outgoing. This blocks all incoming traffic by default, which is the safest starting point. - Allow SSH (critical). Run
sudo ufw allow 22/tcp(or your custom SSH port). If you skip this, you’ll lock yourself out when you enable the firewall. - Allow web traffic. Run
sudo ufw allow 80/tcpandsudo ufw allow 443/tcp. - Allow other services as needed. For example,
sudo ufw allow 21/tcpfor FTP, orsudo ufw allow 25/tcpfor SMTP. Use specific ports only. - Enable the firewall. Run
sudo ufw enable. You’ll be warned that SSH might be disrupted; confirm with ‘y’. - Check your rules. Run
sudo ufw status verboseto see all active rules. Make sure SSH is listed as allowed.
For VPS/Dedicated Server (firewalld on CentOS/RHEL)
- Connect via SSH. Same as above.
- Check if firewalld is running. Run
sudo systemctl status firewalld. If it’s not active, start it withsudo systemctl start firewalldand enable it withsudo systemctl enable firewalld. - Add services. Run
sudo firewall-cmd --permanent --add-service=ssh, then--add-service=httpand--add-service=https. - Add custom ports (if needed). For a non-standard port, run
sudo firewall-cmd --permanent --add-port=8080/tcp. - Reload the firewall. Run
sudo firewall-cmd --reloadto apply changes. - Verify. Run
sudo firewall-cmd --list-allto see what’s allowed.
Common Mistakes
- Blocking SSH before enabling the firewall. Always allow SSH first, then enable the firewall. Otherwise, you’ll be locked out.
- Opening too many ports. Every open port is a potential entry point. Only open what your services actually need.
- Using the wrong protocol. TCP and UDP are different. Most web services use TCP, but some games or VoIP use UDP. Make sure you allow the correct protocol.
- Forgetting to save rules in firewalld. If you don’t use
--permanent, rules are lost after a reboot. - Blocking your own IP. If you add a deny rule for your own IP, you’ll lose access. Double-check IP addresses before blocking.
Tips
- Use a default-deny policy. Start by denying all incoming traffic, then allow specific ports. This is more secure than allowing everything and trying to block bad stuff.
- Test from an external network. After setting up, try accessing your website from a different network (e.g., your phone’s data) to ensure it works.
- Keep a backup of your config. If you’re using UFW, you can copy
/etc/ufw/to a safe place. For firewalld, back up/etc/firewalld/. - Use rate limiting for SSH. In UFW, you can add
sudo ufw limit 22/tcpto slow down brute-force attacks. - Check logs regularly. Look at
/var/log/ufw.log(UFW) orjournalctl -u firewalld(firewalld) to see blocked attempts and adjust rules.
Troubleshooting
- You’re locked out of SSH. If you can’t connect, use your hosting provider’s web-based console (e.g., VNC or serial console) to log in. Then run
sudo ufw allow 22/tcpand reload. If you have a backup, restore it. - Website is down after enabling firewall. Check that ports 80 and 443 are allowed. Run
sudo ufw statusorsudo firewall-cmd --list-allto see if they’re listed. If not, add them. - Email is not sending/receiving. SMTP (port 25), POP3 (110), and IMAP (143) might be blocked. Add them if you use mail services on the same server.
- Firewall won’t start. Check for syntax errors in your rules. For UFW, run
sudo ufw status verboseto see errors. For firewalld, runsudo firewall-cmd --stateand check the logs. - Changes don’t take effect. In firewalld, you must reload after making permanent changes. In UFW, you usually don’t need to reload, but you can run
sudo ufw reloadjust in case.
Frequently Asked Questions
Do I need a firewall on shared hosting?
Most shared hosts already have a server-level firewall. You can’t install your own, but you can use the control panel tools to adjust rules for your account. It’s still good to know how to use them.
What’s the difference between UFW and firewalld?
UFW is a front-end for iptables, common on Ubuntu/Debian. Firewalld is a firewall management tool for CentOS/RHEL, using zones. Both achieve the same goal; choose based on your OS.
Can I block an IP address with a firewall?
Yes. In UFW, use sudo ufw deny from IP_ADDRESS. In firewalld, use sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="IP_ADDRESS" drop'.
How do I know which ports to open?
Check your service documentation. Common ports: 80 (HTTP), 443 (HTTPS), 22 (SSH), 21 (FTP), 25 (SMTP), 3306 (MySQL). Only open what you need.
What is a default-deny policy?
It means all incoming traffic is blocked by default, and you explicitly allow only the ports you need. This is the most secure approach.
Will a firewall slow down my website?
No, a properly configured firewall has minimal performance impact. The overhead is negligible for most websites.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.