Installing the Maxmind GeoLite City binary database and the mod-geoip Module for Apache2 on Ubuntu Linux
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.
mkdir /tmp/geoip
Now move into the directory that you just created.
cd /tmp/geoip
Now, that you're in the /tmp/geoip directory, go ahead and download the GeoLite City binary database from MaxMind.
wget 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.
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
sudo mkdir /usr/local/share/GeoIP
Now, move the GeoLite City binary database to it's new location.
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.
cd /tmp/geoip
Now download the GeoIP C library.
wget geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
Extract the archived GeoIP C library that you just downloaded.
gunzip < GeoIP.tar.gz | tar xvf -
Now move to the new directory that was created when you extracted the archived GeoIP C library.
cd GeoIP-*
Now, configure and install the GeoIP C library by issuing the following commands.
./configure
make
make check
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.
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)
# Load the geoip module
LoadModule geoip_module /usr/lib/apache2/modules/mod_geoip.so
<IfModule mod_geoip.c>
# Activate the GeoIP module
GeoIPEnable On
# Specifies the location of the City database and specifies that
# caching should not be performed
GeoIPDBFile /usr/local/share/GeoIP/GeoLiteCity.dat Standard
# Specifies the location of the Country database and that
# caching should not be performed
GeoIPDBFile /usr/local/share/GeoIP/GeoIP.dat Standard
</IfModule>
Restart Apache so that changes will take effect
Restart Apache so your changes will take effect by entering the following command.
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
<?php
/*
Uses mod-geoip to query the
MaxMind GeoLite City
binary database and returns
geographic information based
on the client's IP address
*/
$country_code = apache_note("GEOIP_COUNTRY_CODE");
$country_name = apache_note("GEOIP_COUNTRY_NAME");
$city_name = apache_note("GEOIP_CITY");
$region = apache_note("GEOIP_REGION");
$metro_code = apache_note("GEOIP_DMA_CODE");
$area_code = apache_note("GEOIP_AREA_CODE");
$latitude = apache_note("GEOIP_LATITUDE");
$longitude = apache_note("GEOIP_LONGITUDE");
$postal_code = apache_note("GEOIP_POSTAL_CODE");
echo 'Country code: '.$country_code.'<br>';
echo 'Country name: '.$country_name.'<br>';
echo 'City name: '.$city_name.'<br>';
echo 'Region: '.$region.'<br>';
echo 'Metro code: '.$metro_code.'<br>';
echo 'Area code: '.$area_code.'<br>';
echo 'Latitude: '.$latitude.'<br>';
echo 'Longitude: '.$longitude.'<br>';
echo 'Postal code: '.$postal_code.'<br>';
?>
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.