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.
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.
安装 EPEL 源
下载最新的 EPEL Repository 自动安装程序* **
wget http://mirrors.sohu.com/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
如果是 64 位系统,则改为
wget http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
如果没有安装 wget 程序,可使用下面的指令安装
yum install wget
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>