How to: install mod_geoip2 for Apache2 on CentOS server.

mod_geoip2 is an Apache 2.x module for finding the country and city that a web request originated from. It uses the GeoIP library and database to perform the lookup.

Steps:

  • Login via SSH as root to your server

  • Get the GeoIP C Library from MaxMind
    wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
    

    Wait until finished
    --09:43:23--  http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
    Resolving geolite.maxmind.com... 64.246.48.99
    Connecting to geolite.maxmind.com|64.246.48.99|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 1045241 (1021K) [application/x-gzip]
    Saving to: `GeoIP.tar.gz'
    
    100%[====================================================================================================================>] 1,045,241   30.1K/s   in 41s
    
    09:44:08 (24.9 KB/s) - `GeoIP.tar.gz' saved [1045241/1045241]
    

  • Extract the library:
    tar zxvf GeoIP.tar.gz
    

  • Change directory:
    cd GeoIP-1.4.6/
    

  • Configure the library:
    ./configure
    

    Wait until finished.
    checking for gcc... gcc
    checking for C compiler default output file name... a.out
    checking whether the C compiler works... yes
    checking whether we are cross compiling... no
    ...
    ...
    ...
    config.status: creating GeoIP.spec
    config.status: creating libGeoIP/Makefile
    config.status: creating apps/Makefile
    config.status: creating conf/Makefile
    config.status: creating data/Makefile
    config.status: creating man/Makefile
    config.status: creating test/Makefile
    config.status: executing depfiles commands
    

  • Make:
    make
    

    Wait until finished.
    Making all in libGeoIP
    make[1]: Entering directory `/root/GeoIP-1.4.6/libGeoIP'
    /bin/sh ../libtool --tag=CC   --mode=compile gcc -DPACKAGE_NAME=\"GeoIP\" -DPACKAGE_TARNAME=\"GeoIP\" -DPACKAGE_VERSION=\"1.4.6\" -DPACKAGE_STRING=\"GeoIP\ 1.4.6\" -DPACKAGE_BUGREPORT=\"support@maxmind.com\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -D__EXTENSIONS__=1 -D_ALL_SOURCE=1 -D_GNU_SOURCE=1 -D_POSIX_PTHREAD_SEMANTICS=1 -D_TANDEM_SOURCE=1 -DPACKAGE=\"GeoIP\" -DVERSION=\"1.4.6\" -DHAVE_DLFCN_H=1 -DHAVE_USHORT_TYPEDEF=1 -DHAVE_ULONG_TYPEDEF=1 -DLITTLE_ENDIAN_HOST=1 -DHAVE_STDINT_H=1 -DHAVE_ZLIB_H=1 -DHAVE_GETTIMEOFDAY=1 -DHAVE_VASPRINTF=1 -DHAVE_VSNPRINTF=1 -DHAVE_VSPRINTF=1 -DHAVE_GETHOSTBYNAME=1 -DHAVE_GETHOSTBYNAME_R=1 -DGETHOSTBYNAME_R_RETURNS_INT=1 -I.  -DGEOIPDATADIR=\"/usr/local/share/GeoIP\" -Wall   -g -O2 -c -o GeoIP.lo GeoIP.c
    mkdir .libs
    ...
    ...
    ...
    sed -e 's|DATADIR|/usr/local/share/GeoIP|g' -e 's|CONF_DIR|/usr/local/etc|g' './geoiplookup.1.in' >geoiplookup.1.tmp
    mv geoiplookup.1.tmp geoiplookup.1
    make[1]: Leaving directory `/root/GeoIP-1.4.6/man'
    make[1]: Entering directory `/root/GeoIP-1.4.6'
    make[1]: Nothing to be done for `all-am'.
    make[1]: Leaving directory `/root/GeoIP-1.4.6'
    

  • Make check
    make check
    

    Wait until finished.
    Making check in libGeoIP
    make[1]: Entering directory `/root/GeoIP-1.4.6/libGeoIP'
    make[1]: Nothing to be done for `check'.
    make[1]: Leaving directory `/root/GeoIP-1.4.6/libGeoIP'
    Making check in apps
    ...
    ...
    ...
    make[1]: Nothing to be done for `check-am'.
    make[1]: Leaving directory `/root/GeoIP-1.4.6'
    make[1]: Leaving directory `/root/GeoIP-1.4.6'
    

  • Make install
    make install
    

    Wait until finished.
    Making install in libGeoIP
    make[1]: Entering directory `/root/GeoIP-1.4.6/libGeoIP'
    make[2]: Entering directory `/root/GeoIP-1.4.6/libGeoIP'
    test -z "/usr/local/lib" || /bin/mkdir -p "/usr/local/lib"
    ...
    ...
    ...
    make[2]: Nothing to be done for `install-data-am'.
    make[2]: Leaving directory `/root/GeoIP-1.4.6'
    make[1]: Leaving directory `/root/GeoIP-1.4.6'
    

  • Get the GeoIP Apache2 API
    wget http://geolite.maxmind.com/download/geoip/api/mod_geoip2/mod_geoip2_1.2.5.tar.gz
    

    Wait until finished.
    --09:48:59--  http://geolite.maxmind.com/download/geoip/api/mod_geoip2/mod_geoip2_1.2.5.tar.gz
    Resolving geolite.maxmind.com... 64.246.48.99
    Connecting to geolite.maxmind.com|64.246.48.99|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 11602 (11K) [application/x-gzip]
    Saving to: `mod_geoip2_1.2.5.tar.gz'
    
    100%[====================================================================================================================>] 11,602      11.7K/s   in 1.0s
    
    09:49:02 (11.7 KB/s) - `mod_geoip2_1.2.5.tar.gz' saved [11602/11602]
    

  • Extract the API
    tar zxvf mod_geoip2_1.2.5.tar.gz
    

  • Change directory
    cd mod_geoip2_1.2.5
    

  • Install the API
    apxs -i -a -L/usr/local/lib -I/usr/local/include -lGeoIP -c mod_geoip.c
    

    Wait until finished.
    /usr/lib/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables  -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -pthread -I/usr/include/httpd  -I/usr/include/apr-1   -I/usr/include/apr-1  -I/usr/local/include  -c -o mod_geoip.lo mod_geoip.c && touch mod_geoip.slo
    mod_geoip.c: In function 'geoip_header_parser':
    mod_geoip.c:326: warning: implicit declaration of function 'ap_add_common_vars'
    mod_geoip.c:301: warning: unused variable 'gip'
    mod_geoip.c: At top level:
    ...
    ...
    ...
    chmod 755 /usr/lib/httpd/modules/mod_geoip.so
    [activating module `geoip' in /etc/httpd/conf/httpd.conf]
    

  • Get the latest GeoLite country database in binary format
    wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
    

    Wait until finished.
    --10:00:17--  http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
    Resolving geolite.maxmind.com... 64.246.48.99
    Connecting to geolite.maxmind.com|64.246.48.99|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 529742 (517K) [application/x-gzip]
    Saving to: `GeoIP.dat.gz'
    
    100%[====================================================================================================================>] 529,742     27.1K/s   in 21s
    
    10:00:42 (24.9 KB/s) - `GeoIP.dat.gz' saved [529742/529742]
    

  • Gunzip'd the database
    gunzip GeoIP.dat.gz
    

  • Move the database to its own directory.
    mkdir /usr/src/geo
    mv GeoIP.dat /usr/src/geo/
    

  • Edit Apache configuration file
    nano /etc/httpd/conf/httpd.conf
    

  • Go all the way down to the end of file (use "Page Down" button to navigate to the next page).

  • Add the following lines:
    <IfModule mod_geoip.c>
      GeoIPEnable On
      GeoIPDBFile /usr/src/geo/GeoIP.dat
    </IfModule>
    

  • Save and exit nano.
    Ctrl + O
    Ctrl + X
    

  • Restart Apache.
    service httpd restart
    

    OR
    /etc/init.d/httpd restart
    

  • Done.

Good luck!

Comments

Thank you very much for this guide, very helpful and accurate!

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

If you enjoyed this post, make sure you subscribe to our RSS Feed! Or if you prefer, you can Follow us on Twitter instead.