Installing mod_geoip for Lighttpd On Fedora 9

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Thu, 2008-06-19 17:06. :: Fedora | Lighttpd

Installing mod_geoip for Lighttpd On Fedora 9

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 05/26/2008

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.

 

4 Links


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by arlencoupland (registered user) on Fri, 2008-06-20 15:27.

Hi,

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.

https://developer.openx.org/jira/browse/OX-1562

Cheers,

Arlen Coupland,  OpenX