Enabling geodata support in Apache, or building mod_geoip from source
While working on a WHM server I ran into the following problem installing mod_geoip:
mod_geoip-1.2.7-1.el5.x86_64 from epel has depsolving problems
--> Missing Dependency: httpd-mmn = 20051115 is needed by package mod_geoip-1.2.7-1.el5.x86_64 (epel)
You could try using --skip-broken to work around the problem
You could try running: package-cleanup --problems
package-cleanup --dupes
rpm -Va --nofiles --nodigest
The program package-cleanup is found in the yum-utils package.
This is because in WHM apache is customized and lives in the /usr/local/apache/ folder. So yum doesn’t see it and insists it can’t work without httpd. You’ll hit the same problem if your Apache is built from source.
Not much fun, but no reason to be sad either.
Install GeoIP:
yum install geoip-devel geoip-data geoip
Download the mod_geoip source:
wget -O 1.2.9.tar.gz https://github.com/maxmind/geoip-api-mod_geoip2/archive/1.2.9.tar.gz
gunzip 1.2.9.tar.gz
cd geoip-api-mod_geoip2-1.2.9/
It’s built using apxs:
/usr/local/apache/bin/apxs -i -a -L/usr/local/lib -I/usr/local/include -lGeoIP -c mod_geoip.c
During compilation the module gets installed into the right directory and the following line gets added to the Apache config:
LoadModule geoip_module modules/mod_geoip.so
In WHM the apache config file (/usr/local/apache/conf/httpd.conf) is dynamic, meaning it can get regenerated from templates depending on circumstances. That’s why you shouldn’t edit it directly - your settings will get lost, sites will go down, and the client will scream at you over the phone. In WHM edit this file instead:
nano /usr/local/apache/conf/includes/post_virtualhost_global.conf
Drop this into it:
LoadModule geoip_module modules/mod_geoip.so
<IfModule mod_geoip.c>
GeoIPEnable On
GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
</IfModule>
Run:
/usr/local/apache/bin/apachectl -t
And restart apache to enable the module.
To check it works, create a geoiptest.php file in the site root with the following content:
<?php
print_r($_SERVER);
?>
Open the file in a browser and you’ll see GEOIP_ADDR, GEOIP_CONTINENT_CODE, GEOIP_COUNTRY_NAME, and everything else.
To have the city and country data update automatically, create the /etc/cron.monthly/geoip file with the following content:
#!/bin/bash
mv /usr/share/GeoIP/GeoIP.dat /usr/share/GeoIP/GeoIP.dat.`$(ls /usr/share/GeoIP/GeoIP.dat* |wc -l)`
wget -q -O /usr/share/GeoIP/GeoIP.dat.gz http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip /usr/share/GeoIP/GeoIP.dat.gz
All that’s left is to make the file executable, after which GeoIP data will update every month:
chmod +x /etc/cron.monthly/geoip
Example of restricting site access for North America:
RewriteEngine on
RewriteCond %{ENV:GEOIP_CONTINENT_CODE} ^NA$
RewriteRule ^(.*)$ - [F]