Installing the Maxmind GeoLite City binary database and the mod-geoip Module for Apache2 on Ubuntu Linux

December 20, 2008

This short install guide will assume that your server is running Ubuntu. I have tested this guide on two systems. One, a 64-bit 8.04 system, and the second a 32-bit 8.10 system.

To complete this install guide, you'll need to have the Apache web server running with PHP configured.

1. Install the GeoLite City database (Binary Version)

I'll first walk you through downloading and installing the GeoLite City binary database.

First, make a temporary directory to download the GeoLite City database.

  1. mkdir /tmp/geoip

Now move into the directory that you just created.

  1. cd /tmp/geoip

Now, that you're in the /tmp/geoip directory, go ahead and download the GeoLite City binary database from MaxMind.

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

You'll notice that the database is gzip compressed, so you'll have to uncompress the database before you can use it.

  1. gunzip GeoLiteCity.dat.gz

Now, it's time to copy the uncompressed database to the location where it will be accessed by the mod-geoip Apache module and also by the C API binary.

First create the GeoIP directory

  1. sudo mkdir /usr/local/share/GeoIP

Now, move the GeoLite City binary database to it's new location.

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

2. Install the GeoIP C Library

NOw you need to install the GeoIP C library. Nothing's going to work unless you have this library installed.

Here's all you have to do.

If you're not already there, go back to the directory you created earlier in the /tmp directory.

  1. cd /tmp/geoip

Now download the GeoIP C library.

  1. wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz

 

Extract the archived GeoIP C library that you just downloaded.

  1. gunzip < GeoIP.tar.gz | tar xvf -

 

Now move to the new directory that was created when you extracted the archived GeoIP C library.

  1. cd GeoIP-*

Now, configure and install the GeoIP C library by issuing the following commands.

  1. ./configure

  1. make

  1. make check

  1. sudo make install

3. Install the MaxMind mod-geoip module for Apache2

Install the Apache module for Ubuntu

Installing the mod-geoip module on Ubuntu Linux (and probably other Debian variants) is done easily with the following command.

  1. sudo apt-get install libapache2-mod-geoip
2.1.2

(Keep in mind though, that when a newer version of mod-geoip is released, the proper version number will need to replace the 2.1.2 version in the above example. Also, the install command might work by removing the version number from the package. However, the following is what I used at the time of this writing to install mod-geoip.)

Enabling mod-geoip

Nothing's going to work unless mod-geoip is enabled in your apache2 configuration. You'll need the following lines in your apache2.conf file (located on Ubuntu systems at /etc/apache2/apache2.conf)

  1.  
  2. # Load the geoip module
  3.  
  4. LoadModule geoip_module /usr/lib/apache2/modules/mod_geoip.so
  5.  
  6.  
  7.  
  8. <IfModule mod_geoip.c>
  9.  
  10. # Activate the GeoIP module
  11.  
  12. GeoIPEnable On
  13.  
  14.  
  15.  
  16.   # Specifies the location of the City database and specifies that
  17.  
  18.   # caching should not be performed
  19.  
  20.  
  21.  
  22.   GeoIPDBFile /usr/local/share/GeoIP/GeoLiteCity.dat Standard
  23.  
  24.  
  25.  
  26.   # Specifies the location of the Country database and that
  27.  
  28.   # caching should not be performed
  29.  
  30.  
  31.  
  32.   GeoIPDBFile /usr/local/share/GeoIP/GeoIP.dat Standard
  33.  
  34.  
  35.  
  36. </IfModule>
  37.  
  38.  

Restart Apache so that changes will take effect

Restart Apache so your changes will take effect by entering the following command.

  1. sudo /etc/init.d/apache2 restart

4. Test your MaxMind GeoIP/GeoLite City Installation

Now that you've installed MaxMind's GeoLite City binary database, the GeoIP C library, and the mod-geoip module for the Apache web server, you should now test your installation with a simple PHP script that will query the GeoLite City datbase against your IP address and (hopefully) print out some information about your location.

You can use the following PHP script to test your GeoIP installation

  1. <?php
  2.  
  3. /*
  4.  
  5.   Uses mod-geoip to query the
  6.  
  7.   MaxMind GeoLite City
  8.  
  9.   binary database and returns
  10.  
  11.   geographic information based
  12.  
  13.   on the client's IP address
  14.  
  15. */
  16.  
  17.  
  18.  
  19. $country_code = apache_note("GEOIP_COUNTRY_CODE");
  20.  
  21. $country_name = apache_note("GEOIP_COUNTRY_NAME");
  22.  
  23. $city_name = apache_note("GEOIP_CITY");
  24.  
  25. $region = apache_note("GEOIP_REGION");
  26.  
  27. $metro_code = apache_note("GEOIP_DMA_CODE");
  28.  
  29. $area_code = apache_note("GEOIP_AREA_CODE");
  30.  
  31. $latitude = apache_note("GEOIP_LATITUDE");
  32.  
  33. $longitude = apache_note("GEOIP_LONGITUDE");
  34.  
  35. $postal_code = apache_note("GEOIP_POSTAL_CODE");
  36.  
  37.  
  38.  
  39. echo 'Country code: '.$country_code.'<br>';
  40.  
  41. echo 'Country name: '.$country_name.'<br>';
  42.  
  43. echo 'City name: '.$city_name.'<br>';
  44.  
  45. echo 'Region: '.$region.'<br>';
  46.  
  47. echo 'Metro code: '.$metro_code.'<br>';
  48.  
  49. echo 'Area code: '.$area_code.'<br>';
  50.  
  51. echo 'Latitude: '.$latitude.'<br>';
  52.  
  53. echo 'Longitude: '.$longitude.'<br>';
  54.  
  55. echo 'Postal code: '.$postal_code.'<br>';
  56.  
  57.  
  58. ?>

Now when you call the script in a browser, you should see some information that describes your geographic location.

Note: As of January 19, 2009 I am in the process of moving this site to a new server from Creativevps.com. At this time, I have not installed the GeoIP software on this machine and therefore you will not see any script output below.

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

 

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.


10 < lib.newsrootlineandtabmenu

13