Refresh GeoIP automatically

GeoIP is a very useful tool provided by MaxMind. It can determine which country, region, city, postal code, and area code the visitor is coming from in real-time. For more information, visit MaxMind website.

This tool is also coming with an Apache module allowing to redirect users depending on their location. For example, we could redirect all users from France to the French home page of a multi-language website, or we could block the traffic to users from a specific country.

To install this module on a Debian server, you simply need to run the following command:

apt-get install libapache2-mod-geoip

But, how does this module work? How does it know where the user comes from?  😯
It is actually quite simple: GeoIP is using a mapping file of IP address by country. On Debian, this file is stored in the folder /usr/share/GeoIP and is named GeoIP.dat.

However, the IP addresses are something which change all the time. So this file will get out-of-date very quickly. This is why MaxMind provides an updated file at the beginning of each month for free. To read the installation instructions, please click the following link:

This is good and well, but who will remember or even have the time to update this file every month? And imagine if you have to do this on hundreds of servers?
The solution is to use a shell script which will download, extract and install the updated GeoIP file automatically once a month:


# Go in the GeoIP folder
cd /usr/share/GeoIP

# Remove the previous GeoIP file (if present)
rm GeoIP.dat.gz

# Download the new GeoIP file

# Remove the previous GeoIP backup file
rm GeoIP.dat.bak

# Backup the existing GeoIP file
mv GeoIP.dat GeoIP.dat.bak

# Extract the new GeoIP file
gunzip GeoIP.dat.gz

# Change the permission of the GeoIP file
chmod 644 GeoIP.dat

# Reload Apache
service apache2 reload

You can place this file in your root folder and set up the following crontab job:

0 0 3 * * /root/

This will execute the script automatically on the third day of every month.

, , , , , , ,

  1. #1 by Miguel Almeida on 24 Sep 2013 - 17:48

    You can use the CheckCache keyword within the plugin (GeoIPDBFile /usr/share/GeoIP/GeoIP.dat CheckCache) to achieve the same result.

  2. #2 by smoreau on 24 Sep 2013 - 22:26

    Miguel Almeida :

    You can use the CheckCache keyword within the plugin (GeoIPDBFile /usr/share/GeoIP/GeoIP.dat CheckCache) to achieve the same result.

    I don’t agree with you. The Apache directive you gave us doesn’t update the GeoIP database from the MaxMind website.
    This directive will only check if the local GeoIP file has been updated in order to reload it (which would prevent the reload of the entire Apache).

    Please read the following documentation for more information:

(will not be published)