Friday, July 31, 2009

GeoIP module installation with Apache from source

Here we will talk about installing and configuring free GeoIP from source for your Apache2.x and on RedHat servers. I normally install both GeoIP API as well as GeoIP module for Apache to make sure that, it works with Apache as well as i can query GeoIP database from command prompt also. Here are those two steps.

First - To install GeoIP API you need to download source and compile it, as well as you need to install GeoIP database file. The GeoIP data file will be used by Apache module also. Here are few commonds which you need to run to install the GeoIP API.

as you know already that I use to install everything in /usr/local so

$ cd /usr/local
$ wget http://www.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
$ tar -zxvf GeoIP.tar.gz
$ cd GeoIP-1.4.4/
$ ./configure
$ make clean
$ make
$ make install

Trust me this will install the GeoIP API in one shot. :)

Now its time to install free GeoIP database file. So

$ cd /usr/local/share/
$ mkdir GeoIP
$ cd GeoIP
$ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
$ tar -zxvf GeoLiteCity.dat.gz

Second - To install GeoIP module for the Apache you need to download the module source and compile it. Here are few commands which you need to run. Note that there is pre-requisit of Apache already installed on the server. Maxmind has Apache modules availible for both Apache 1.x and 2.x. You can refer http://geolite.maxmind.com/download/geoip/api/mod_geoip/ for Apache 1.x or http://geolite.maxmind.com/download/geoip/api/mod_geoip2/ for Apache 2.x module source. As I have Apache 2.x installed on my server so I have used second link.

$ cd ..
$ wget http://geolite.maxmind.com/download/geoip/api/mod_geoip2/mod_geoip2_1.2.5.tar.gz
$ tar -zxvf mod_geoip2_1.2.5.tar.gz
$ cd mod_geoip2_1.2.5/
$ /usr/local/apache2/bin/apxs -i -a -L /usr/local/GeoIP-1.4.4/libGeoIP -I /usr/local/GeoIP-1.4.4/libGeoIP -l GeoIP -c mod_geoip.c

Rewrite various paths in last line as per you apache insallation.

Now you need to check and configure your Apache. So open your httpd.conf file and find LoadModule string in it, most likely you get some thing like

LoadModule geoip_module modules/mod_geoip.so

if above line is not written in your apache configuration, paste it after others LoadModule lines.

Now paste follwing line also

GeoIPEnable On
GeoIPDBFile /usr/local/share/GeoIP/GeoLiteCity.dat


Now just restart your apache and your are done.

If you want to test that whether GeoIP module is install or not. Write a php script as



when you run this, it will output the array as

Array
(
[GEOIP_CONTINENT_CODE] => AS
[GEOIP_COUNTRY_CODE] => SG
[GEOIP_COUNTRY_NAME] => Singapore
[GEOIP_REGION] => 00
[GEOIP_CITY] => Singapore
[GEOIP_DMA_CODE] => 0
[GEOIP_AREA_CODE] => 0
[GEOIP_LATITUDE] => 1.293100
[GEOIP_LONGITUDE] => 103.855797
.
.
.
[REQUEST_TIME] => 1249023542
[argv] => Array
(
)

[argc] => 0
)

Note that now it has few " GEOIP_ " variables in the $_SERVER array.

One last and most trickest thing, that how to keep on updating this GeoIP database file. I will teach you how to do with free and non-free versions also.

For free you can simply write a shell script as

$ vim /usr/local/share/GeoIP/geoipupdate.sh

paste following lines into it

#!/bin/sh
cd /usr/local/share/GeoIP
wget -c http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
tar -zxvf GeoLiteCity.dat.gz

Change mode of file as

$ chmod 777 geoipupdate.sh

Now add the script into cron, do

$ crontab -e

and paste following line into it. This will update your geodatabase file every night at 1 AM.

1 0 * * * /usr/local/share/GeoIP/geoipupdate.sh

Here you are done with free GeoIP setup and you have already written a cron to update the free GeoIP database.

For paid version it almost same except update cron and different database files. In case of paid version you will have file name GeoIP.dat rather than GeoLiteCity.dat. This is how I am updating my GeoIP database files for paid version.

After Installtion you have to edit your GeoIP.conf file -

$ vim /usr/local/etc/GeoIP.conf

# If you purchase a subscription to the GeoIP database,
# then you will obtain a license key which you can
# use to automatically obtain updates.
# for more details, please go to
# http://www.maxmind.com/app/products

# see https://www.maxmind.com/app/license_key_login to obtain License Key,
# UserId, and available ProductIds

# Enter your license key here
LicenseKey [Enter your license key here]

# Enter your User ID here
UserId [Enter your User ID here]

# Enter the Product ID(s) of the database(s) you would like to update
# By default 106 (MaxMind GeoIP Country) is listed below
ProductIds 106

Now create a directory logs, this will store all update logs.

$ mkdir /usr/local/GeoIP-1.4.4/logs/

now we need a shell script geoipupdate.sh to update database file.

$ vim /user/local/share/GeoIP/geoipupdate.sh

#!/bin/bash
yyyymmdd=` date +%Y%m%d`
HOME_DIR=/usr/local/GeoIP-1.4.4
LOG_DIR=$HOME_DIR/logs
start=` date +'%Y%m%d %H:%M:%S'`
echo Start update geoip [$start] >> $LOG_DIR/update$yyyymmdd.log
$HOME_DIR/apps/geoipupdate >> $LOG_DIR/update$yyyymmdd.log
end=` date +'%Y%m%d %H:%M:%S'`
echo End update geoip [$end] >> $LOG_DIR/update$yyyymmdd.log
echo "***********" >> $LOG_DIR/update$yyyymmdd.log

You can setup the cron, as we did for free verison to run at 1 AM every night.

1 0 * * * /usr/local/share/GeoIP/geoipupdate.sh

No comments:

Post a Comment