How to get mod_geoip to work on cPanel (Country and City)

30 19
Read Time:1 Minute, 48 Second

mod_geoip allows you to look up a user’s location from their IP Address. It comes in handy when you want to display flags, enable download server location and target specific users


First run following commands

cd /var/cpanel/easy/apache/custom_opt_mods
wget http://docs.cpanel.net/twiki/pub/EasyApache/EasyApacheCustomModules/custom_opt_mod-mod_geoip.tar.gz
 tar -xvf custom_opt_mod-mod_geoip.tar.gz



Run EasyApache via cPanel or via following command



/scripts/easyapache


mkdir /usr/share/GeoIP/

cd /usr/share/GeoIP/
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoIP.dat.gz
gunzip GeoLiteCity.dat.gz
rm -f *.gz

Thereafter in cPanel WHM go to 

Apache Configurations > Pre VirtualHost Include

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

Finally run

service httpd restart

If everything works well, you’ll get apache to run mod_geoip. You can test it by adding this php code on test.php

  <?php

    /*

      Uses mod-geoip to query the

      MaxMind GeoLite City

      binary database and returns

      geographic information based

      on the client’s IP address


      */

    $country_code = apache_note(“GEOIP_COUNTRY_CODE”);

    $country_name = apache_note(“GEOIP_COUNTRY_NAME”);

    $city_name = apache_note(“GEOIP_CITY”);

    $region = apache_note(“GEOIP_REGION”);

    $metro_code = apache_note(“GEOIP_DMA_CODE”);

    $area_code = apache_note(“GEOIP_AREA_CODE”);

    $latitude = apache_note(“GEOIP_LATITUDE”);

    $longitude = apache_note(“GEOIP_LONGITUDE”);

    $postal_code = apache_note(“GEOIP_POSTAL_CODE”);



    echo ‘Country code: ‘.$country_code.'<br>’;

    echo ‘Country name: ‘.$country_name.'<br>’;

    echo ‘City name: ‘.$city_name.'<br>’;

    echo ‘Region: ‘.$region.'<br>’;

    echo ‘Metro code: ‘.$metro_code.'<br>’;

    echo ‘Area code: ‘.$area_code.'<br>’;

    echo ‘Latitude: ‘.$latitude.'<br>’;

    echo ‘Longitude: ‘.$longitude.'<br>’;

    echo ‘Postal code: ‘.$postal_code.'<br>’;

 


?>

About Post Author

Vishal Barot

Hi there, Vishal Barot here. Full-time web developer and graphic designer with over 10 years of experience. I've been experimenting with web development since I was 17 years old. If you are looking to have a professional website or design then I can develop it for you. Looking forward to working with you.
Happy
Happy
19 %
Sad
Sad
18 %
Excited
Excited
12 %
Sleepy
Sleepy
19 %
Angry
Angry
15 %
Surprise
Surprise
16 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment

Your email address will not be published. Required fields are marked *

The maximum upload file size: 1 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop files here