Installing mod_geoip for Apache2 On Ubuntu 12.04

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Fri, 2012-09-07 15:01. :: Ubuntu | Web Server | Apache

Installing mod_geoip for Apache2 On Ubuntu 12.04

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Follow me on Twitter
Last edited 08/09/2012

This guide explains how to set up mod_geoip with Apache2 on an Ubuntu 12.04 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 Ubuntu 12.04 system with a working Apache2, e.g. as shown in this tutorial: Installing Apache2 With PHP5 And MySQL Support On Ubuntu 12.04 LTS (LAMP).

Because we must run all the steps from this tutorial with root privileges, we can either prepend all commands in this tutorial with the string sudo, or we become root right now by typing

sudo su

 

2 Installing mod_geoip

To install mod_geoip, we simply run:

apt-get install libapache2-mod-geoip

Then we open /etc/apache2/mods-available/geoip.conf and uncomment the GeoIPDBFile line so that the file looks as follows:

vi /etc/apache2/mods-available/geoip.conf

<IfModule mod_geoip.c>
  GeoIPEnable On
  GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
</IfModule>

Next we restart Apache:

/etc/init.d/apache2 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):

vi /var/www/geoiptest.php

<html>
<body>
<?php
$country_name = apache_note("GEOIP_COUNTRY_NAME");
print "Country: " . $country_name;
?>
</body>
</html>

Call that file in a browser, and it should display your country (make sure that you're calling the file from a public IP address, not a local one).

 

4 Use Cases

You can use mod_geoip to redirect or block/allow users based on their country. You can find some useful examples for this here: http://www.maxmind.com/app/mod_geoip

If you want to use mod_geoip with OpenX/OpenAds/phpAdsNew, this PDF file has instructions for it: http://www.maxmind.com/openads_geoip.pdf

 

5 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 playfla (registered user) on Tue, 2012-10-02 16:34.

Thanks for the great howto. I ran into an issue with the testing method above using apache_note.

No output but an error in the log

mod_fcgid: stderr: PHP Fatal error: Call to undefined function apache_note()

From what I understand it is due to a server configuration using either suphp or running Apache as cgi.

I verified in phpinfo that despite the error Geoip is working fine. To get the output I just had to access the Apache environment variables.

 

<?php
// Example use of getenv()
$country = getenv('GEOIP_COUNTRY_NAME');
print "Country: " . $country;
// Or simply use a Superglobal ($_SERVER or $_ENV)
$ccode = $_SERVER['GEOIP_COUNTRY_CODE'];
print "<br>Country Code: " . $ccode;
?>

http://www.php.net/manual/en/function.apache-getenv.php

Submitted by Ben Lacey (not registered) on Wed, 2014-03-12 12:24.

The module creates server variables that you can use in your PHP files so instead of using apache_note() use the following instead:

<?php
echo "Country Name: " . $_SERVER["GEOIP_COUNTRY_NAME"];
echo "<br/>Country Code: " . $_SERVER["GEOIP_COUNTRY_CODE"];
echo "<br>Continent: " . $_SERVER["GEOIP_CONTINENT_CODE"];
?>

Have a look here: http://dev.maxmind.com/geoip/legacy/mod_geoip2/ at "Output Variables" - these can all be used using $_SERVER['output-variable-here'] syntax.