The ConfigServer Firewall (CSF) is one of the best Linux firewalls ever created. Developed by configserver.com, CSF turns your server into a powerfully defensive firewall and intrusion detection system.
This firewall also has the nifty ability to block an entire countries’ traffic. Now why would you have to block an entire country from accessing your website? Reasons range from reducing bandwidth to security risks to annoying spam. Maybe you have hackers and have traced them back to China, or maybe you just have a crazy ex stalking you from Uzbekistan. Whatever the reason, CSF makes it easy, and it should block 99% of the blocked countries’ traffic.
How it works is CSF uses the Maxmind GeoLite County database to access Classless Inter-Domain Routing (CIDR). Each country has its own 2 letter CIDR country code, which covers all the IP addresses in that country.
Now before we begin, make sure CSF is installed on your cPanel server, and that you have access to WHM. cPanel and WHM comes free with a HostDime managed dedicated server.
How to Block Traffic from a Country in 7 Steps
1. Log into WHM as root.
2. Click on the “ConfigServer Security & Firewall” link within the “Plugins” section.
3. Click on the “Firewall Configuration” button.
4. Search for the “Country Code List and Settings” section.
5. Enter in the 2 letter CIDR country code you would like to block within the CC_DENY section.
6. Scroll down to the very bottom and click the “Change” button.
7. Click the “Restart csf+lfd” button to apply the changes and restart the firewall.
After restarting, the countries you have added will no longer be able to access the server.
A few notes to consider after doing this.
The more IPs and countries you add, the slower your server becomes as it has to perform a check in the iptables every time a visitor goes to your website. Also you may not want to block Germany’s traffic with this method, as one of the CSF servers resides in that country, which will cause CSF to not update and will display an error message.
If you are a managed client and have any questions, open up a chat!
Jared Smith is HostDime’s SEO and Content Strategist.