mod_GeoIP on CentOS 7
Mod_GeoIP is an Apache module that can be used to get the geographic location of IP address of the visitor into the Apache web server. The module allows you to determine the visitor’s country and location. It is specially useful for Geo Ad Serving, Target Content, Spam Fighting, Fraud Detection, Redirecting/Blocking visitors based on their country and much more.
GeoIP module allows system administrators to redirect or block web traffic according on the client geographical location. The geographical location is learned via client IP address.
Mod_GeoIP has two versions, one is Free and another one is Paid.
Enable EPEL Repository
Mod_Geoip is not available under official repository, install and enable third party EPEL repository.
yum install epel-release
Install Mod_GeoIP
Once you’ve EPEL repository enabled on your system, you can simple install mod_geoip by running following command with their dependency packages.
yum install mod_geoip GeoIP GeoIP-devel GeoIP-data zlib-devel
Download latest Geo Databases
It’s good idea to download latest Geo City and Country Database to stay updated.
cd /usr/share/GeoIP/
mv GeoIP.dat GeoIP.dat_org
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
gunzip GeoLite2-Country.tar.gz
gunzip GeoLite2-City.tar.gz
Enable Mod_GeoIP in Apache
After the module has been installed, open and edit the module main configuration file, with a command line text editor such as vim, and activate the module server-wide, as illustrated in the below excerpt.
vim /etc/httpd/conf.d/geoip.conf
Set the line GeoIPEnable from Off to On. Also, make sure you add the absolute path to GeoIP database file.
<IfModule mod_geoip.c>
GeoIPEnable On
GeoIPDBFile /usr/share/GeoIP/GeoIP.dat MemoryCache
</IfModule>
Restart the Apache service to reflect changes.
systemctl restart httpd
If you are running multiple sites, It’s not recommended to turn on GeoIP module server-wide. You should enable the GeoIP module only in <Location>
or <Directory>
blocks where you would actually perform the traffic redirection or block.
Updating GeoIP Database
GeoIP database is updated beginning of every month. So, its is very important to keep GeoIP database up-to-date. To download latest version of database use the following command.
cd /usr/share/GeoIP/
mv GeoIP.dat GeoIP.dat_org
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
gunzip GeoLite2-Country.tar.gz
gunzip GeoLite2-City.tar.gz
For more information about mod_geoip and its usage can be found at http://www.maxmind.com/app/mod_geoip.