Installing mod_geoip for Lighttpd On Fedora 9

Version 1.0
Author: Falko Timme

This guide explains how to set up mod_geoip with lighttpd on a Fedora 9 system. mod_geoip looks up the IP address of the client end user. This allows you to redirect or block users based on their country. You can also use this technology for your OpenX (formerly known as OpenAds or phpAdsNew) ad server to allow geo targeting.

I do not issue any guarantee that this will work for you!


1 Preliminary Note

I'm assuming that you have a running Fedora 9 system with a working lighttpd and PHP.


2 Installing mod_geoip

To install mod_geoip for lighttpd, we simply run:

yum install lighttpd-mod_geoip

Then open /etc/lighttpd/lighttpd.conf and add "mod_geoip", to the server.modules section; also add the geoip.db-filename and geoip.memory-cache directives below the server.modules section:

vi /etc/lighttpd/lighttpd.conf
server.modules              = (
#                               "mod_rewrite",
#                               "mod_redirect",
#                               "mod_alias",
#                               "mod_cml",
#                               "mod_trigger_b4_dl",
#                               "mod_auth",
#                               "mod_status",
#                               "mod_setenv",
#                               "mod_proxy",
#                               "mod_simple_vhost",
#                               "mod_evhost",
#                               "mod_userdir",
#                               "mod_cgi",
#                               "mod_compress",
#                               "mod_ssi",
#                               "mod_usertrack",
#                               "mod_expire",
#                               "mod_secdownload",
#                               "mod_rrdtool",
                                "mod_accesslog" )

geoip.db-filename = "/usr/share/GeoIP/GeoIP.dat"
geoip.memory-cache = "enable"

You will find the GeoIP database (GeoIP.dat) in the /usr/share/GeoIP directory. As the geographic allocation of IP addresses can change over time, it's a good idea to download the newest GeoIP.dat now:

cd /usr/share/GeoIP/
mv GeoIP.dat GeoIP.dat_orig
gunzip GeoIP.dat.gz

Restart lighttpd:

/etc/init.d/lighttpd restart

That's it already!


3 A Short Test

To see if mod_geoip is working correctly, we can create a small PHP file in one of our web spaces (e.g. /var/www/lighttpd) (of course, PHP must be enabled in your lighttpd installation):

vi /var/www/lighttpd/geoiptest.php

Call that file in a browser, and it should display the SERVER array including values for GEOIP_COUNTRY_CODE, GEOIP_COUNTRY_CODE3, and GEOIP_COUNTRY_NAME (make sure that you're calling the file from a public IP address, not a local one).

    [HOSTNAME] =>
    [TERM] => xterm
    [SHELL] => /bin/bash
    [HISTSIZE] => 1000
    [SSH_CLIENT] => 3419 22
    [QTDIR] =>
    [QTINC] => /include
    [SSH_TTY] => /dev/pts/0
    [USER] => root
    [LS_COLORS] => ...
    [CCACHE_DIR] => /var/cache/ccache
    [MAIL] => /var/spool/mail/root
    [PATH] => /sbin:/usr/sbin:/bin:/usr/bin
    [INPUTRC] => /etc/inputrc
    [PWD] => /usr/share/GeoIP
    [CCACHE_UMASK] => 002
    [LANG] => en_US.UTF-8
    [SHLVL] => 3
    [HOME] => /root
    [LOGNAME] => root
    [QTLIB] => /lib
    [CVS_RSH] => ssh
    [SSH_CONNECTION] => 3419 22
    [LESSOPEN] => |/usr/bin/ %s
    [_] => /usr/sbin/lighttpd
    [SERVER_SOFTWARE] => lighttpd/1.4.19
    [SERVER_NAME] =>
    [SERVER_PORT] => 80
    [SERVER_ADDR] =>
    [REMOTE_PORT] => 59559
    [REMOTE_ADDR] =>
    [SCRIPT_NAME] => /geoiptest.php
    [PATH_INFO] =>
    [SCRIPT_FILENAME] => /var/www/lighttpd/geoiptest.php
    [DOCUMENT_ROOT] => /var/www/lighttpd/
    [REQUEST_URI] => /geoiptest.php
    [REDIRECT_STATUS] => 200
    [HTTP_HOST] =>
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20080404 Firefox/
    [HTTP_ACCEPT] => ...
    [HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5
    [HTTP_ACCEPT_ENCODING] => gzip,deflate
    [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
    [HTTP_KEEP_ALIVE] => 300
    [HTTP_CONNECTION] => keep-alive
    [GEOIP_COUNTRY_NAME] => Germany
    [PHP_SELF] => /geoiptest.php
    [REQUEST_TIME] => 1211821794

If you want to use lighttpd + mod_geoip for your OpenX ad server, make sure you select MaxMind mod_apache GeoIP under Settings > Main Settings > Geotargeting Settings. This will work for lighttod + mod_geoip as well.


Share this page:

Suggested articles

1 Comment(s)

Add comment




Nice to see tutorials about mod_geoip as it can really have performance benefits over the non-module database.  Also users of OpenX find great performance benefits with lighttpd so it useful to have a guide specific to this.

Please note one bug fixed in OpenX 2.4.6+ involving mod_geoip and lighttpd due to the fact that the majority of the mod_geoip $_SERVER variable names are slightly different with mod_geoip in lighttpd compared to the apache version.


Arlen Coupland,  OpenX