1

I can't get the GeoIP PHP extension working on Ubuntu 18.04 with PHP 7.2

i have intalled it by the cli: sudo apt-get install -y php7.2-geoip

Its enabled in system but not working:

php7.2 -i | grep geoip
/etc/php/7.2/cli/conf.d/20-geoip.ini,
geoip
geoip support => enabled
geoip extension version => 1.1.1
geoip library version => 1006012
geoip.custom_directory => no value => no value

testing with the following php code:

<?php
echo $_SERVER['GEOIP_COUNTRY_CODE'];

but the GEOIP module is not working

  • Do you have some reason to believe that code should work? – Michael Hampton Aug 7 at 19:29
0

It seems like you are mixing up two things. You install the geoip extension for PHP. However, $_SERVER['GEOIP_COUNTRY_CODE'] is populated by Apache mod_geoip module.

Install Apache module

sudo apt install -y libapache2-mod-geoip

Modify the file /etc/apache2/mods-enabled/geoip.conf and remove the # in front of GeoIPDBFile - and set GeoIPENable to On:

  GeoIPEnable On
  GeoIPDBFile /usr/share/GeoIP/GeoIP.dat

Save the file, restart apache: apache2ctl restart

Now you will have $_SERVER['GEOIP_COUNTRY_CODE'] available.

| improve this answer | |
  • Hi, @frands-hansen , its showing the country code, but the problem is only showing same country even from other location IP, showing only Server IP's country code "CA" – Mitra Aug 15 at 6:55
  • Do you have some sort of reverse proxy in front of your apache? Please try echo $_SERVER['REMOTE_ADDR'] and GEOIP_ADDR - does that show the IP address of the connecting client properly? – Frands Hansen Aug 15 at 11:54
  • Hi, @frands-hansen , No, I have 2 IP on my server only, on the server have Apache and Nginx, with Vestacp – Mitra Aug 15 at 19:36
  • Hi, @frands-hansen I try echo GEOIP_ADDR. its displaying IP addr of the client!,, and REMOTE_ADDR displaying IP of the server – Mitra Sep 10 at 18:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.