Lighttpd Install mod_geoip For Country / City Level Geo Targeting

Geolocation software is used to get the geographic location of visitor using IP address. You can determine country, organization and guess visitors location. This is useful for

a] Fraud detection

b] Geo marketing and ad serving

c] Target content

d] Spam fighting

e] And much more.

mod_geoip is a Lighttpd module for fast ip/location lookups. In this tutorial you will learn about mod_geoip installation and php server side examples to determine visitors country.

mod_geoip uses the MaxMind GeoIP / GeoCity databases, which comes in two version:

  • Free Version: Country and city databases are free with 99.5% accuracy.
  • Paid Version: If you need 99.8% accuracy and other fancy details about IP address use paid version.

See this page for Free vs Paid version details.

A note about CentOS / RHEL / Fedora Linux users

If you are using 3rd party repo (see RPMforge and EPEL repo installations FAQ), you can install binary mod_geoip package as follows and skip directly to configuration part:
# yum install lighttpd-mod_geoip

Step # 1: Install C API for mod_geoip

Type the following command to download and extract MaxMind C API:
# cd /tmp
# wget
# tar -zxvf GeoIP.tar.gz

Install required development package, enter:
# yum install zlib-devel
Configure, compile and install C API:
# cd GeoIP-1.4.6
# ./configure
# make
# make install

Configure GNU ld

You need link mod_geoip using C API. You need to configure dynamic linker run time bindings as follows:
# cd /etc/
# vi geoip.conf

Append the following configuration:


Save and close the file. Run ldconfig to activate configuration:
# ldconfig
Verify that the name of each directory including /usr/local/lib is scanned, and any links that are created:
# ldconfig -v | less
Sample output:

/usr/local/lib: -> ->
/usr/lib/mysql: -> ->
/usr/lib64/mysql: -> ->
/lib: -> ->
[Output truncated]

Step #2: Download lighttpd latest version

Type the following command:
# cd /tmp
# wget
# tar -zxvf lighttpd-1.4.22.tar.gz
# cd lighttpd-1.4.22

Step #3: Download mod_geoip patch

Type the following command:
# cd lighttpd-1.4.22/src
# wget -O mod_geoip.c

Compile lighttpd with mod_geoip patch

Edit and add the following after the last module:

mod_geoip_la_SOURCES = mod_geoip.c
mod_geoip_la_LDFLAGS = -module -export-dynamic -avoid-version -no-undefined
mod_geoip_la_LIBADD = $(common_libadd) -lGeoIP

Save and close the file. Now compile lighttpd as follows:
# cd ..
# aclocal
# automake -a
# autoconf
# make clean

Now you must use –enable-maintainer-mode option:
# ./configure --program-prefix= --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/usr/com --mandir=/usr/share/man --infodir=/usr/share/info --with-openssl --enable-maintainer-mode
# make
# make install

Step # 4: Download GeoLite Database (Free version)

Type the following command:
# wget
# gunzip GeoIP.dat.gz
# ls -lh GeoIP.dat

Sample output:

-rw-r--r-- 1 root root 1.1M Mar  9 21:13 GeoIP.dat

Install GeoIP.dat file:
# mkdir /usr/local/GeoIP
# cp -v GeoIP.dat /usr/local/GeoIP

Step # 5: Configure Lighttpd

Open your lighttpd.conf file and append the following configuration. First, enable mod_geoip:

server.modules +=    "mod_geoip"

Finally, set path to GeoIP.dat file and turn on memory caching for faster lookups:

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

Save and close the file. Finally, restart the lighttpd:
# /etc/init.d/lighttpd restart

Step # 6: Test your setup

mod_geoip will set environment variable such as follows:


You can use any server side programming language to determine visitors GEO location. Here is a sample php code:

 <title>What is my IP address - determine or retrieve my IP address</title>
    if (getenv(HTTP_X_FORWARDED_FOR)) {
        $pipaddress = getenv(HTTP_X_FORWARDED_FOR);
        $ipaddress = getenv(REMOTE_ADDR);
        echo "Your Proxy IP address is : ".$pipaddress. " (via $ipaddress) " ;
    } else {
        $ipaddress = getenv(REMOTE_ADDR);
        echo "Your IP address is : $ipaddress";
    $country = getenv(GEOIP_COUNTRY_NAME);
    echo "<br />Your country : $country";

Another example: Redirecting user to country specific URL

        // get country .. set default to INDIA
        // - USA customer website
        // - INDIA customer website and so on...
        $country_code = ( !empty( $_SERVER['GEOIP_COUNTRY_CODE'] ) ) ? $_SERVER['GEOIP_COUNTRY_CODE'] : 'IN';
        header( 'Location:'.strtolower( $country_code ).'/' );


🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • Erdem Mar 17, 2011 @ 15:32

    Hello Vivek..

    I installed mod_geoip from EPEL repository and tried the ‘What is my IP address’ code above and it worked. Great! 🙂

    Now, is there a chance to also display country flags alongside the name of the country? I have those country flags named ‘uk.png, us.png, tr.png’… but frankly have no idea how to do this.

  • Russel May 19, 2010 @ 22:25

    I followed this tutorial exactly the way you wrote it, but for some reason I can’t see the GeoIP commands in my PHP $_SERVER variable. I did this tutorial about 10 times already and I still can’t figure out the problem. I’m running lighttpd on ubuntu karmic. Any ideas what could be the problem?

  • vamsi Nov 19, 2009 @ 14:36

    this works..thanks..
    I am using in wordpress with translation plugin 😀

  • vamsi Oct 10, 2009 @ 13:04

    Thanks for the detailed tutorial..
    helped me a lot..

  • 🐧 nixCraft Aug 2, 2009 @ 10:01

    @svr / Ross:

    Yes, Apache got mod_geoip2

  • svar Aug 2, 2009 @ 9:27

    Is there is any such module available in apache?

  • Ross May 21, 2009 @ 8:27

    Any chance we may be able to get this tutorial in apache?

  • Balaviswanathan.V Apr 26, 2009 @ 5:32

    Great one. Thank you for the tutorial and is the installation of the paid version is the same………And one request is that could be please even cover tutorial for gentoo Linux…. If you give so I will be grateful to you……………Thanks


  • Aaron DM Mar 31, 2009 @ 14:43

    Good post. I had another one, can’t remember the site, but it was XML & MYSQL extracts, however they never updated regularly at all. Monthly is not bad.

  • Leszek Taczkowski Mar 29, 2009 @ 17:31

    Great info! Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @