Installing GeoIP Apache Module to Get Country From IP Address

Enabling, configuring, and using mod_geoip2 for Apache is very simple…

These steps will work for any Windows or Linux Apache installation, but are specific to WampDeveloper Pro (as it already contains mod_geoip2 and its configuration + databases).

Configuring mod_geoip2

Load WampDeveloper’s mod_geoip configuration by opening file httpd.conf and uncommenting line:

Include C:/WampDeveloper/Config/Apache/extra/wampd-geoip.conf

WampDeveloper’s included mod_geoip setup contains the following self-explanatory configuration:

<IfModule !mod_geoip.c>
	LoadModule geoip_module modules/mod_geoip.so
</IfModule>

<IfModule mod_geoip.c>
	# Enable for all websites and URLs
	GeoIPEnable On
	
	# Turn off mod_geoip token in server signature
	GeoIPToken Off
	
	# Change output charset from ISO-8859-1 (Latin-1) to UTF-8 (for city names only?)
	GeoIPEnableUTF8 On
	
	# Set only the "GEOIP_..." environment variables
	# Default is: All
	GeoIPOutput Env
	
	# Use true client IP instead of the possibly bogus IP in "Forwarded For" Headers
	# note - to use IP behind trusted proxy (ex: CloudFlare), use mod_remoteip
	GeoIPScanProxyHeaders Off
	
	# note - If Apache is crashing, instead of GeoIPFlags: MemoryCache or IndexCache, try using: Standard
	
	# Load Country level databases into memory (loading into memory will increase httpd proccess size 1.5-2x)
	GeoIPDBFile C:/WampDeveloper/Tools/GeoIP/GeoIP.dat MemoryCache
	GeoIPDBFile C:/WampDeveloper/Tools/GeoIP/GeoIPv6.dat MemoryCache
	
	# Additional DBs loaded into memory (MemoryCache) can greatly increase httpd process size (2-5x)
	#GeoIPDBFile C:/WampDeveloper/Tools/GeoIP/GeoLiteCity.dat Standard
	#GeoIPDBFile C:/WampDeveloper/Tools/GeoIP/GeoLiteCityv6.dat Standard
	#GeoIPDBFile C:/WampDeveloper/Tools/GeoIP/GeoIPASNum.dat IndexCache
	#GeoIPDBFile C:/WampDeveloper/Tools/GeoIP/GeoIPASNumv6.dat IndexCache
</IfModule>

Save changes to the httpd.conf file. Restart Apache.

Getting Country and Location Data From Client IP Address

When enabled, mod_geoip2 provides to Apache and PHP multiple environmental variables containing location specific data of the connecting/client IP address:

<?php
echo 'Client IP Address: '   . getenv('GEOIP_ADDR');
echo 'Client Country Code: ' . getenv('GEOIP_COUNTRY_CODE');
echo 'Client Country Name: ' . getenv('GEOIP_COUNTRY_NAME');
?>

This data further goes down to the specific city.

Output Variables of mod_geoip2

Blocking Countries

To use GeoIP blocking, set in website’s VirtualHost or .htaccess files the following configuration, updated to include the country-codes that you want to block.

GeoIPEnable On

# to block China
SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
# to block Russia
SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry
# ... place more countries here

Deny from env=BlockCountry

GeoIP Databases

GeoLite Legacy Databases are free and can be downloaded from:
http://dev.maxmind.com/geoip/legacy/geolite/

GeoLite Legacy Databases are updated on the first Tuesday of each month.

WampDeveloper stores the referenced GeoIPDBFile databases in folder C:\WampDeveloper\Tools\GeoIP, and you can use Windows Tasks Scheduler to automatically wget or cURL [both tools are provided] to get updated databases each new month.

Notes

1. “mod_geoip2″ (v2) is technically version 1.2.x.

2. Use “GeoIP Legacy” .dat databases, not “GeoLite2″ .mmdb databases (they are for mod_maxminddb).

3. mod_geoip module is only provided for Apache 2.4 VC11 32 and 64 bit builds, and Apache 2.2 VC9 32 bit builds.

4. Downloadable mod_geoip builds can be found at ApacheHaus…
mod_geoip-1.2.9 (rc): https://www.apachehaus.net/rc/
mod_geoip-1.2.8a: https://www.apachehaus.com/cgi-bin/download.plx

5. Documentation:
mod_geoip2 configuration documentation – http://dev.maxmind.com/geoip/legacy/mod_geoip2/
mod_geoip2 changelog – https://github.com/maxmind/geoip-api-mod_geoip2/releases

One thought on “Installing GeoIP Apache Module to Get Country From IP Address”

Leave a Reply to manoj Cancel reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>