How to install the Maxmind Geolite City database and the mod-geop module for apache on Debian

By: Jacob Seiner
August 24, 2009


 

One of the coolest additions you can add to an Apache web server is the geolocation software from Maxmind. With it you can log visits & customize user experience based on the city you're located in. Unlike other tutorials, we not only install the Maxmind country database, but also the free Geolite City database from Maxmind. Both databases will be queried by the mod-geoip module for apache, which you'll also learn to install, and which is faster than other lookup methods.

Finding the geographical location of an ip address couldn't be simpler. In a few minutes, you'll have the foundation of a geotargeting/geotracking setup on your Debian based server. 

Here's how to install maxmind geolite city on debian lenny 64 bit.

 

With root privileges, follow the following steps.

Install the mod-geoip module for apache2

 

  1. apt-get update

apt-get install libapache2-mod-geoip

 

DOWNLOAD AND INSTALL THE GeoLite City DATABASE

Download and install the free GeoLite City Database

 

  1. cd /tmp

 

  1. wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

gunzip GeoLiteCity.dat.gz

 

  1. mv GeoLiteCity.dat /usr/share/GeoIP/

Make a simple change to your apache configuration

 

Next, find the file /etc/apache2/mods-available/geoip.conf

 

And edit it so that it's contents look like this:

 

  1. <IfModule mod_geoip.c>

  GeoIPEnable On

  GeoIPDBFile /usr/share/GeoIP/GeoIP.dat Standard

  GeoIPDBFile /usr/share/GeoIP/GeoLiteCity.dat Standard

</IfModule>

Save and close the file after making your edits.

Restart your web server

 

Restart apache with this command:

 

  1. /etc/init.d/apache2 restart

Test out your Maxmind mod-geoip & GeoLite City Installation

 

Now test to see if it works. Create and run a PHP file:

 

  1. vi maxmindtest.php

It's contents should be as follows:

  1.       <?php
  2.       /*
  3.        
  4.         Uses mod-geoip to query the
  5.         MaxMind GeoLite City
  6.         binary database and returns
  7.         geographic information based
  8.         on the client's IP address
  9.        
  10.       */
  11.        
  12.       $country_code = apache_note("GEOIP_COUNTRY_CODE");
  13.       $country_name = apache_note("GEOIP_COUNTRY_NAME");
  14.       $city_name = apache_note("GEOIP_CITY");
  15.       $region = apache_note("GEOIP_REGION");
  16.       $metro_code = apache_note("GEOIP_DMA_CODE");
  17.       $area_code = apache_note("GEOIP_AREA_CODE");
  18.       $latitude = apache_note("GEOIP_LATITUDE");
  19.       $longitude = apache_note("GEOIP_LONGITUDE");
  20.       $postal_code = apache_note("GEOIP_POSTAL_CODE");
  21.        
  22.       echo 'Country code: '.$country_code.'<br>';
  23.       echo 'Country name: '.$country_name.'<br>';
  24.       echo 'City name: '.$city_name.'<br>';
  25.       echo 'Region: '.$region.'<br>';
  26.       echo 'Metro code: '.$metro_code.'<br>';
  27.       echo 'Area code: '.$area_code.'<br>';
  28.       echo 'Latitude: '.$latitude.'<br>';
  29.       echo 'Longitude: '.$longitude.'<br>';
  30.       echo 'Postal code: '.$postal_code.'<br>';
  31.        
  32.       ?>

After running the above script, you should see information describing your location. If you don't, well, look over the instructions again and see if you skipped a step.

Country code: US
Country name: United States
City name:
Region:
Metro code: 0
Area code: 0
Latitude: 38.000000
Longitude: -97.000000
Postal code:

No comments

Add comment

* - required field

*

CAPTCHA image for SPAM prevention
If you can't read the word, click here.
*
*
 

Netdip.com is an excellent web site that's powered by TYPO3 and other great open source software. Netdip.com is also a fat free alternative to ice cream.