mod_geoip Apache module helps you to find the location of the IP address of the website visitors.This post describes how to install mod_geoip in cPanel using EasyApache 3. If your cPanel server is using EasyApache 4, please click on the following button to get the tutorial to install mod_geoip: Here
Install mod_geoip
Please login to the server via SSH and run the following commands:
mkdir /usr/share/GeoIP cd /usr/share/GeoIP
Then you need to download the GeoIP database.
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz gzip -d GeoIP.dat.gz
You have installed the GeoIP database now. Next step is to install custom mod_geoip module for cPanel.
cd /usr/local/src/ wget https://documentation.cpanel.net/download/attachments/2326651/custom_opt_mod-mod_geoip.tar.gz tar -C /var/cpanel/easy/apache/custom_opt_mods -xzf custom_opt_mod-mod_geoip.tar.gz
Rebuild Apache using EasyApache. You can find the mod_geoip module is listed on the “Short Options List”
/scripts/easyapache
Install mod_geoip
You need to load and enable mod_geoip once the Apache rebuild completed.
Please go to:
WHM >> Service Configuration >> Apache Configuration >> Include Editor >> Pre Main Include >> Select the current Apache version
Add the following codes into the box:
LoadModule geoip_module /usr/local/apache/modules/mod_geoip.so GeoIPEnable On GeoIPDBFile /usr/share/GeoIP/GeoIP.dat MemoryCache
Also, you can verify the module is installed properly on the server by using the following command.
root@server [~]# httpd -M | grep geoip geoip_module (shared) root@server [~]#
Redirect a domain according to country IP address (.htaccess rule)
Add the following into your .htaccess file:
GeoIPEnable On # Canada RewriteEngine on RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$ # India RewriteEngine on RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^IN$ RewriteRule ^/?$ "http\:\/\/in\.domain\.com\/" [R=301,L] #US RewriteEngine on RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^US$ RewriteRule ^/?$ "http\:\/\/us\.domain\.com\/" [R=301,L]
Replace domain.com with your domain name in the .htaccess rule. Also, you can add more countries in the .htaccess file.
That’s it!