mod_geoip is an Apache module created by MaxMind. It’s widely used to geo-locate your visitors to know geographical location (country, city, IP lookup, etc). On this post today we will explore an easy way to install mod_geoip on cPanel servers.
Follow this step by step instructions on how to get mod_geoip installed on your cPanel/WHM server
How can I install mod_geoip on cPanel servers?
Login to your VPS / Cloud Hosting or Dedicated server as root (if you don’t have root, you must ask your web hosting provider to install this module for you)
1. Download, extract and install MaxMind GeoIP Database
mkdir /usr/share/GeoIP && cd /usr/share/GeoIP wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz gzip -d GeoIP.dat.gz
2. Install mod_geoip on cPanel
The following commmand will download and install mod_geoip for cPanel servers.
wget https://documentation.cpanel.net/download/attachments/2326651/custom_opt_mod-mod_geoip.tar.gz?version=29&modificationDate=1460059677554&api=v2 tar -C /var/cpanel/easy/apache/custom_opt_mods -xzf custom_opt_mod-mod_geoip.tar.gz
3. Recompile Apache to activate mod_geoip
For this we will use cPanel’s EasyApache script so this mod_geoip can become an active Apache module. Move to:
WHM >> Software >> EasyApache >> Start customizing based on profile >> Activate / Check ‘Mod GeoIP’ >> then hit ‘Save and Build’.
That should start the apache compilation process to add mod_geoip to cPanel / WHM server.
4. Enable mod_geoip from Apache configuration
Now that Apache recompilation is done and that mod_geoip is an active apache module, now you need to enable mod_geoip for cPanel, as it’s there, but not enabled until you do it. Move to:
WHM >> Service Configuration >> Apache Configuration >> Include Editor >> Pre Main Include>> Select your current Apache version
Then copy and paste this details into your text area:
LoadModule geoip_module /usr/local/apache/modules/mod_geoip.so GeoIPEnable On GeoIPDBFile /usr/share/GeoIP/GeoIP.dat MemoryCache
That’s all. At this time mod_geoip should be installe into cPanel and working as expected.
Install GeoIP PHP Extension
However, the story doesn’t finish here, now you need to install and add PHP GeoIP extension into your system. Run this command as root:
pecl install geoip
Then add this code to your php.ini file, usually located at /usr/local/lib/php.ini
extension=geoip.so
Restart Apache to apply changes:
service httpd restart
How can I test PHP GeoIP is working?
You can test your GeoIP PHP module by running:
php -i |grep geo
The output should be something like:
php -i |grep geo geoip geoip support => enabled geoip extension version => 1.0.7 geoip library version => 1004008 geoip.custom_directory => no value => no value
Important Note: at the time of writing this tutorial I didn’t knew there was a new MaxMind GeoIP module, you can find more about it here: https://github.com/maxmind/mod_maxminddb. Soon I will be releasing a new mod_maxminddb tutorial for cPanel servers, stay tuned.
Hello.
It doesn’t work.
wget https://documentation.cpanel.net/download/attachments/2326651/custom_opt_mod-mod_geoip.tar.gz?version=29&modificationDate=1460059677554&api=v2
–2017-05-06 18:40:40– https://documentation.cpanel.net/download/attachments/2326651/custom_opt_mod-mod_geoip.tar.gz?version=29
Resolving documentation.cpanel.net… 208.74.123.67
Connecting to documentation.cpanel.net|208.74.123.67|:443… connected.
HTTP request sent, awaiting response… 404 Not Found
2017-05-06 18:40:41 ERROR 404: Not Found.
Hello! I noticed that Maxmind has discontinued geolite DB (https://blog.maxmind.com/2018/01/02/discontinuation-of-the-geolite-legacy-databases/) and also recently removed the GeoLiteCountry/GeoIP.dat.gz file, therefore mod_geoip must be replaced with mod_maxminddb, since the format of even the IP db is different (now it is a file of type .mmdb instead of .dat),
This article about mod_geoip is really useful, have you considered creating a new article for the installation and configuration of the new apache module for cpanel / whm? Since really so far there is no type of documentation about it.
Regards!
Daniel