Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Apache: Get Visitor Details  (Read 3285 times)

0 Members and 1 Guest are viewing this topic.


  • Guest
Apache: Get Visitor Details
« on: January 15, 2014, 02:01:47 pm »
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 module

Enable EPEL repository for  RHEL/CentOS 6

Assuming system is 64bit:

# wget
#rpm -Uvh epel-release-6-8.noarch.rpm

For 32 bt:

# wget
#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 Database

It’s good idea to download latest Geo City and Country Database to stay updated.

Code: [Select]
# cd /usr/share/GeoIP/
# mv GeoIP.dat GeoIP.dat_org
# wget
# gunzip GeoIP.dat.gz

Enable Mod_GeoIP in Apache

To 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

Restart the Apache service

# /etc/init.d/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 your document root directory and add below piece of php code to it.

# vi /var/www/html/testgeoip.php

Code: [Select]
<title>What is my IP address and Country</title>

    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";

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 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.

Code: [Select]
# cd /usr/share/GeoIP/
# mv GeoIP.dat GeoIP.dat_org
# wget
# gunzip GeoIP.dat.gz

Automatic GeoIP Database Update

Place the following script under /etc/cron.monthly.

Code: [Select]
# Automatic GeoIP Database Update from
cd /usr/share/GeoIP
mv GeoIP.dat GeoIP.dat_org
wget -q
gzip -d -f GeoIP.dat.gz

For more information about mod_geoip, visit