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

Mitra
  • 113
  • 5

1 Answers1

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.

Frands Hansen
  • 4,657
  • 1
  • 17
  • 29
  • 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 '20 at 06: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 '20 at 11:54
  • yes I have Nginx, and this $_SERVER['REMOTE_ADDR'] - Its displaying IP address of the client properly. but not correct country, my ip country is CA and its displaying FR? – Mitra Dec 25 '20 at 22:08