Last Updated: February 25, 2016
·
2.72K
· j7mbo

Apache - Block specific countries using mod_geoip

Here's a really simple set of commands to block certain countries from accessing content served via Apache. You can use this on your vps to only accept requests from the UK, for example.

Here's the top of a modified 000-default virtualhost:

<VirtualHost *:80>
    DocumentRoot /var/www

    <Directory /var/www/>
        # Default Config
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None

        # Only allow specific countries
        GeoIPEnable On
        SetEnvIf GEOIP_COUNTRY_CODE GB AllowCountry
        Deny from all
        Allow from env=AllowCountry
    </Directory>
    ... etc

Once you restart apache (sudo service apache2 restart), any IP that isn't from within Great Britain (GB) will be served a 403 - it's great to see a load of these appear in your /var/log/apache2/access.log files.