Mod_GeoIP module is used to get the geographic location of IP address of the visitor into the Apache web server. This module allows you to determine the visitor’s
country, organization 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.
Mod_GeoIP has both Free and Paid versions.
Free Version : In Free version the Geo City and Country databases are availble with 99.5% accuracy.
Paid Version : In Paid version you will get both databases with 99.8% accuracy with some more advanaced details about IP address.
Install Mod_GeoIP moduleEnable EPEL repository for RHEL/CentOS 6 Assuming system is 64bit:
# wget http://epel.mirror.net.in/epel/6/x86_64/epel-release-6-8.noarch.rpm
#rpm -Uvh epel-release-6-8.noarch.rpm
For 32 bt:
# wget http://epel.mirror.net.in/epel/6/i386/epel-release-6-8.noarch.rpm
#rpm -Uvh epel-release-6-8.noarch.rpm
Once you’ve EPEL repository enabled on your system, you can simple install it by running following command with their dependency packages.
# yum install mod_geoip GeoIP GeoIP-devel GeoIP-data zlib-devel
Download latest Geo City and Country DatabaseIt’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/GeoLiteCountry/GeoIP.dat.gz
# gunzip GeoIP.dat.gz
Enable Mod_GeoIP in ApacheTo enable mod_geoip module you need to add the following lines in your apache configuration file, say
/etc/httpd/conf/httpd.conf.
<IfModule mod_geoip.c>
GeoIPEnable On
GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
</IfModule>
Restart the Apache service
# /etc/init.d/httpd restartTesting Mod_GeoIP ModuleTo test the mod_geoip module is working correctly with Apache, we need to creat a PHP file called testgeoip.php under your document root directory and add below piece of php code to it.
# vi /var/www/html/testgeoip.php<html>
<head>
<title>What is my IP address and Country</title>
</head>
<body>
<?
if (getenv(HTTP_X_FORWARDED_FOR)) {
$pipaddress = getenv(HTTP_X_FORWARDED_FOR);
$ipaddress = getenv(REMOTE_ADDR);
echo "Your Proxy IP address is : ".$pipaddress. " (via $ipaddress) " ;
} else {
$ipaddress = getenv(REMOTE_ADDR);
echo "My IP address is : $ipaddress";
}
$country = getenv(GEOIP_COUNTRY_NAME);
echo "<br />My Country : $country";
?>
</body>
</html>
Now, try to call the file using web browser (e.g.
http:/your_ip-OR-your_domain/testgeoip.php). You will get your IP address and Country details.
Updating GeoIP DatabaseGeoIP 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/GeoLiteCountry/GeoIP.dat.gz
# gunzip GeoIP.dat.gz
Automatic GeoIP Database UpdatePlace the following script under /etc/cron.monthly.
# Automatic GeoIP Database Update from www.tecmint.com
#!/bin/sh
cd /usr/share/GeoIP
mv GeoIP.dat GeoIP.dat_org
wget -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gzip -d -f GeoIP.dat.gz
For more information about mod_geoip, visit
http://www.maxmind.com/app/mod_geoip.