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 3309 times)

0 Members and 1 Guest are viewing this topic.

Vinil

  • 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:

Quote
# 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:

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

Quote
# 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 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 add the following lines in your apache configuration file, say /etc/httpd/conf/httpd.conf.

Quote
<IfModule mod_geoip.c>
GeoIPEnable On
GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
</IfModule>


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]
<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 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 http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
# gunzip GeoIP.dat.gz


Automatic GeoIP Database Update


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

Code: [Select]
# 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.