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_geoip",
                                "mod_access",
#                               "mod_cml",
#                               "mod_trigger_b4_dl",
#                               "mod_auth",
#                               "mod_status",
#                               "mod_setenv",
                                "mod_fastcgi",
#                               "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
wget http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz
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
<?php
print_r($_SERVER);
?>

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).

Array
(
    [HOSTNAME] => server1.example.com
    [TERM] => xterm
    [SHELL] => /bin/bash
    [HISTSIZE] => 1000
    [SSH_CLIENT] => 192.168.0.24 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] => 192.168.0.24 3419 192.168.0.100 22
    [LESSOPEN] => |/usr/bin/lesspipe.sh %s
    [G_BROKEN_FILENAMES] => 1
    [_] => /usr/sbin/lighttpd
    [PHP_FCGI_CHILDREN] => 1
    [FCGI_ROLE] => RESPONDER
    [SERVER_SOFTWARE] => lighttpd/1.4.19
    [SERVER_NAME] => 84.143.142.69
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PORT] => 80
    [SERVER_ADDR] => 192.168.0.100
    [REMOTE_PORT] => 59559
    [REMOTE_ADDR] => 84.143.142.69
    [SCRIPT_NAME] => /geoiptest.php
    [PATH_INFO] =>
    [SCRIPT_FILENAME] => /var/www/lighttpd/geoiptest.php
    [DOCUMENT_ROOT] => /var/www/lighttpd/
    [REQUEST_URI] => /geoiptest.php
    [QUERY_STRING] =>
    [REQUEST_METHOD] => GET
    [REDIRECT_STATUS] => 200
    [SERVER_PROTOCOL] => HTTP/1.1
    [HTTP_HOST] => 84.143.142.69
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14
    [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_CODE] => DE
    [GEOIP_COUNTRY_CODE3] => DEU
    [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:

1 Comment(s)