Block visits using GeoIP

Discover our Public Cloud offer

Shared Hosting for Everyone, imagined by developers, for developers.

Discovering the Public Cloud

MaxMind DB1 is an Apache module of GeoIP that aims to locate a user based on their IP address.

Installation

Run the following commands in SSH at the root of the account:

mkdir mod_maxminddb
cd mod_maxminddb/
wget https://github.com/maxmind/mod_maxminddb/releases/download/1.2.0/mod_maxminddb-1.2.0.tar.gz
tar xf mod_maxminddb-1.2.0.tar.gz 
cd mod_maxminddb-1.2.0/
./configure --with-apxs=/usr/alwaysdata/apache/latest/bin/apxs && make
cp ./src/.libs/mod_maxminddb.so ~/
cd
rm -fr mod_maxminddb

Then add in the Web > Configuration menu of your alwaysdata administration interface:

LoadModule maxminddb_module $HOME/mod_maxminddb.so

Finally create an account on their interface to retrieveone of their databases: free or paid depending on your needs.

Uses

Block some countries

In this example, we are using the free database which we place at the root of the account and we block China and the United States.

Add the following at the top of a .htaccess file at the root of the website:

MaxMindDBEnable On
MaxMindDBFile COUNTRY_DB $HOME/GeoLite2-Country.mmdb
MaxMindDBEnv COUNTRY_CODE COUNTRY_DB/country/iso_code

SetEnvIf COUNTRY_CODE CN BlockCountry
SetEnvIf COUNTRY_CODE US BlockCountry

Deny from env=BlockCountry

Only allow certain countries

In this example, we are using the free database which we place at the root of the account and we only allow France.

Add the following at the top of a .htaccess file at the root of the website:

MaxMindDBEnable On
MaxMindDBFile COUNTRY_DB $HOME/GeoLite2-Country.mmdb
MaxMindDBEnv COUNTRY_CODE COUNTRY_DB/country/iso_code

SetEnvIf COUNTRY_CODE FR AllowCountry

Deny from all
Allow from env=AllowCountry

  1. MaxMind DB can also be used for geotargeting in marketing and offers city-level databases. ↩︎