A firewall controls incoming and outgoing traffic to your VPS, blocking unwanted connections while allowing legitimate ones. Here is how to configure two common firewalls.
Option A: UFW (Uncomplicated Firewall) - Great for beginners
-
Check Status:
ufw status -
Set Default Policies:
ufw default deny incomingandufw default allow outgoing -
Allow Essential Ports:
-
SSH (if you changed the port, use that number):
ufw allow 22/tcp -
Web Traffic (HTTP):
ufw allow 80/tcp -
Secure Web Traffic (HTTPS):
ufw allow 443/tcp -
(Optional) FTP:
ufw allow 21/tcp
-
-
Enable the Firewall:
ufw enable -
Verify:
ufw status verbose
Option B: CSF (ConfigServer Security & Firewall) - More advanced
CSF is popular on cPanel/WHM servers.
-
Install: (Instructions vary, usually via root SSH).
-
Allow/Deny IPs: Edit
/etc/csf/csf.allowor/etc/csf/csf.deny. -
Open Ports: Edit
/etc/csf/csf.confand look for theTCP_INandTCP_OUTdirectives. Add or remove port numbers as needed (e.g.,TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995"). -
Restart CSF: After changes, run
csf -rto restart.