Installing mod_geoip for Apache2 On CentOS 6.3

Version 1.0
Author: Falko Timme
Follow me on Twitter

This guide explains how to set up mod_geoip with Apache2 on a CentOS 6.3 system. mod_geoip looks up the IP address of the client end user. This allows you to redirect or block users based on their country. You can also use this technology for your OpenX (formerly known as OpenAds or phpAdsNew) ad server to allow geo targeting.

I do not issue any guarantee that this will work for you!


1 Preliminary Note

I'm assuming that you have a running CentOS 6.3 system with a working Apache2 + PHP, e.g. as shown in this tutorial: Installing Apache2 With PHP5 And MySQL Support On CentOS 6.3 (LAMP).


2 Installing mod_geoip

Because mod_geoip is not available from the official CentOS repositories, we need to enable the EPEL repository:

rpm --import
rpm -ivh epel-release-6-7.noarch.rpm
yum install yum-priorities

Edit /etc/yum.repos.d/epel.repo...

vi /etc/yum.repos.d/epel.repo

... and add the line priority=10 to the [epel] section:

name=Extra Packages for Enterprise Linux 6 - $basearch

To install mod_geoip, we simply run:

yum install mod_geoip

You will then find the GeoIP database (GeoIP.dat) in the /usr/share/GeoIP directory. As the geographic allocation of IP addresses can change over time, it's a good idea to download the newest GeoIP.dat now:

cd /usr/share/GeoIP/
mv GeoIP.dat GeoIP.dat_orig
wget -N -q
gunzip GeoIP.dat.gz

Next we restart Apache:

/etc/init.d/httpd restart

That's it already!


3 A Short Test

To see if mod_geoip is working correctly, we can create a small PHP file in one of our web spaces (e.g. /var/www/html):

vi /var/www/html/geoiptest.php

Call that file in a browser, and it should display the SERVER array including values for GEOIP_COUNTRY_CODE, GEOIP_CONTINENT_CODE, and GEOIP_COUNTRY_NAME (make sure that you're calling the file from a public IP address, not a local one).

    [GEOIP_COUNTRY_NAME] => Germany
    [HTTP_HOST] =>
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1
    [HTTP_ACCEPT] => text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
    [HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5
    [HTTP_ACCEPT_ENCODING] => gzip,deflate
    [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
    [HTTP_KEEP_ALIVE] => 300
    [HTTP_CONNECTION] => keep-alive
    [PATH] => /sbin:/usr/sbin:/bin:/usr/bin
    [SERVER_SIGNATURE] => <address>Apache/2.2.15 (CentOS) Server at Port 80</address>

    [SERVER_SOFTWARE] => Apache/2.2.15 (CentOS)
    [SERVER_NAME] =>
    [SERVER_ADDR] =>
    [SERVER_PORT] => 80
    [REMOTE_ADDR] =>
    [DOCUMENT_ROOT] => /var/www/html
    [SERVER_ADMIN] => [email protected]
    [SCRIPT_FILENAME] => /var/www/html/geoiptest.php
    [REMOTE_PORT] => 57421
    [REQUEST_URI] => /geoiptest.php
    [SCRIPT_NAME] => /geoiptest.php
    [PHP_SELF] => /geoiptest.php
    [REQUEST_TIME] => 1344547179

If you want to use Apache2 + mod_geoip for your OpenX ad server, make sure you select MaxMind mod_apache GeoIP under Settings > Main Settings > Geotargeting Settings:


4 Use Cases

You can use mod_geoip to redirect or block/allow users based on their country. You can find some useful examples for this here:


Share this page:

Suggested articles

1 Comment(s)

Add comment


By: Sammy

[[email protected] downloads]# yum install mod_geoip

Loaded plugins: fastestmirror, priorities

Setting up Install Process

Loading mirror speeds from cached hostfile

epel/metalink                                                                    |  25 kB     00:00     

 * base:

 * epel:

 * extras:

 * rpmforge:

 * updates:

1617 packages excluded due to repository priority protections

No package mod_geoip available.



Did NOT work.