Install Mod_GeoIP for Apache in RHEL/CentOS 6.3/5.8
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 two different version one is Free and another one is Paid and uses MaxMind GeoIP / GeoCity databases.
- 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.
If you like to check out the more differences betweetn Free and Paid version, please visit the Maxmind.com.
This how-to guide explains how to setup and install Mod_GeoIP module for Apache in RHEL 6.3/6.2/6.1/6.0/5.8 CentOS 6.3/6.2/6.1/6.0/5.8 using EPEL repository with YUM package manager utility.
We assume that you already have running RHEL 6.3 and CentOS 6.3 system with a working LAMP (Linux, Apache, MySQL and PHP) setup. If not, then read our articles where we’ve shown the installation of both operating systems with LAMP.
Installation Of RHEL/CentOS 6.3
- Red Hat Enterprise Linux (RHEL) 6 Installation Guide with Screenshots
- CentOS 6.3 Step by Step Installation Guide with Screenshots
LAMP Setup on RHEL/CentOS 6.3
Enable EPEL Repository in RHEL/CentOS 6/5
By default mod_Geoip is not avilable under RHEL / CentOS official repository, so we need to install and enable third party EPEL repository.
RHEL/CentOS 6
# wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm # rpm -Uvh epel-release-6-7.noarch.rpm Preparing... ########################################### [100%] 1:epel-release ########################################### [100%]
RHEL/CentOS 5
# wget http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm # rpm -Uvh epel-release-5-4.noarch.rpm Preparing... ########################################### [100%] 1:epel-release ########################################### [100%]
Install Mod_GeoIP in RHEL/CentOS 6/5
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 Database
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/GeoLiteCountry/GeoIP.dat.gz # gunzip GeoIP.dat.gz
Enable Mod_GeoIP in Apache
To enable mod_geoip module you need to open the /etc/httpd/conf/httpd.conf configuration file.
# vi /etc/httpd/conf/httpd.conf
And add the following lines of code it at the bottom.
<IfModule mod_geoip.c> GeoIPEnable On GeoIPDBFile /usr/share/GeoIP/GeoIP.dat </IfModule>
Restart the Apache service to reflect changes.
# /etc/init.d/httpd restart OR # service httpd restart
Testing Mod_GeoIP Module
To test the mod_geoip module is working correctly with Apache, we need to creat a PHP file called testgeoip.php under Apache root directory (e.g. /var/www/html).
# vi /var/www/html/testgeoip.php
Insert the following piece of php code to it.
<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://localhost/testgeoip.php). You will get your IP address and Country details.
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/GeoLiteCountry/GeoIP.dat.gz # gunzip GeoIP.dat.gz
Automatic GeoIP Database Update
We have written a smaill shell script that will automatically download the latest version of GeoIP database every month. Just place the any of the following script under /etc/cron.monthly.
Script 1
# 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
Script 2
#!/bin/sh GEOIP_MIRROR="http://geolite.maxmind.com/download/geoip/database" GEOIPDIR=/usr/share/GeoIP TMPDIR= DATABASES="GeoLiteCity GeoLiteCountry/GeoIP asnum/GeoIPASNum GeoIPv6" if [ -d "${GEOIPDIR}" ]; then cd $GEOIPDIR if [ -n "${DATABASES}" ]; then TMPDIR=$(mktemp -d geoipupdate.XXXXXXXXXX) echo "Updating GeoIP databases..." for db in $DATABASES; do fname=$(basename $db) wget --no-verbose -t 3 -T 60 "${GEOIP_MIRROR}/${db}.dat.gz" -O "${TMPDIR}/${fname}.dat.gz" gunzip -fdc "${TMPDIR}/${fname}.dat.gz" > "${TMPDIR}/${fname}.dat" mv "${TMPDIR}/${fname}.dat" "${GEOIPDIR}/${fname}.dat" chmod 0644 "${GEOIPDIR}/${fname}.dat" done [ -d "${TMPDIR}" ] && rm -rf $TMPDIR fi fi
Redirecting Users based on Country
The below example code will redirect users based on the country code that we set to AS (Asia). This way you can redirect any users based on their county code.
GeoIPEnable On GeoIPDBFile /usr/share/GeoIP/GeoIP.dat # Redirect one country RewriteEngine on RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AS$ RewriteRule ^(.*)$ https://www.tecmint.com$1 [R,L]
Blocking Users based on Country
This example will block users based on the country code that GeoIP sets. Below example will block users from AS (Asia) and US (United States) countries.
GeoIPEnable On GeoIPDBFile /usr/share/GeoIP/GeoIP.dat SetEnvIf GEOIP_COUNTRY_CODE AS BlockCountry SetEnvIf GEOIP_COUNTRY_CODE US BlockCountry # ... place more countries here Deny from env=BlockCountry
Allowing Users based on Country
This below example will only allow users from below mentioned countries.
GeoIPEnable On GeoIPDBFile /usr/share/GeoIP/GeoIP.dat SetEnvIf GEOIP_COUNTRY_CODE AS AllowCountry SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry # ... place more countries here Deny from all Allow from env=AllowCountry
For more information about mod_geoip and its usage can be found at http://www.maxmind.com/app/mod_geoip. If you’re having any trouble in setting up mod_geoip module, please let us know via comments and please don’t forget to share it with your friends.
i get error: /usr/bin/perl: symbol lookup error: /usr/local/lib64/perl5/auto/Geo/IP/IP.so: undefined symbol: GeoIP_open
@Jasmin,
Please go through the following link to fix the issue..
https://github.com/maxmind/geoip-api-perl/issues/2
I used the sample script, it shows the IP, not showing Country name .
Nice article
Each and every step clearly defined
What about nginx ?
@Krunal,
We are in process of upgrading this article to support nginx web server..stay tuned for the updates..
Hello,
deny on geopip.conf does not work. I get the error:
Stopping httpd: [ OK ]
Starting httpd: Syntax error on line 21 of /etc/httpd/conf.d/geoip.conf:
deny not allowed here
i used the syntax:
SetEnvIf GEOIP_COUNTRY_CODE DE AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE AT AllowCountry
Deny from all
Allow from env=AllowCountry
How can i fix this?
Thanks!
Better create .htaccess file and add the block lines..it shoud work..
Hi there, nice article.
But how can I block countries for all my domains which are hosted on my server.
Or should I have to paste the code into each .htaccess for each domain?
Yes, you must create a specific .htaccess file under each domain and add the following example of code to block countries.