Go to the official website Maxmind and create an account. Interestingly, I could not create an account via VPN. The service recognizes the IP addresses of hosters and does not allow me to create an account. But an existed account works via VPN. Therefore, in order to create an account, I had to use the real IP address of some computer (either my own or using vpngate+softether).
Install the geoip module https://www.drupal.org/project/geoip
I also had to install a patch on the D7 core.
I took the patch from here: https://www.drupal.org/files/issues/2019-05-08/3026443-d7-72.patch
Now you can use the GeoIP API. I only needed the geoip_country_code(geoip_ip_address()) function, which gives the 2-letter country code.
For example, you can check the condition:
if (geoip_country_code(geoip_ip_address()) == "XX") {
// do something
}
Install the GeoIP module on Drupal 10 || 11:
composer require 'drupal/geoip:^3.0'
./vendor/bin/drush pm-install geoip
Although this module is from the same developer, it is significantly different. The module's API is also different. To use it, you need to add a function to a custom plugin (or Drupal::service) that looks something like this:
function is_client_ip_country_code_XX() {
$ip_address = \Drupal::request()->getClientIp();
$user_country_code = \Drupal::service('geoip.geolocation')->geolocate($ip_address);
if (isset($user_country_code)) {
if ($user_country_code == "XX") {
return TRUE;
} elseif ($user_country_code <> "XX") {
return FALSE;
}
}
}
And then add in the theme files (inside the YOURTHEME_preprocess_YOURBLOCK function):
// // GeoIP API
if (is_client_ip_country_code_XX()) {
// do something
}
// prevent cache this block
// this code also need Internal Page Cache module disabled
// works with Dynamic Page Cache module enabled
$variables['#cache']['max-age'] = 0;
! It is important to note that you need to disable Internal Page Cache, but Dynamic Page Cache can be enabled. Then we can set max-age to "0" and the block will be dynamic. Otherwise, Drupal by default makes a cache of the page of the first anonymous user and displays the same for all subsequent ones. In fact, GeoIP API becomes useless due to such caching.
Documentation says that there should be a possibility of HTTPS connection from our server. You can do it like this:
curl -I https://mm-prod-geoip-databases.a2649acb697e2c09b632799562c076f2.r2.cloudflarestorage.com
We'll get some answer (most likely 400, but that's not important).
Download the geoipupdate
utility from GitHub https://github.com/maxmind/geoipupdate/releases (the latest package for Debian) and install it on the system.
wget https://github.com/maxmind/geoipupdate/releases/download/v6.1.0/geoipupdate_6.1.0_linux_amd64.deb
Then we install it (the package does not require any dependencies):
dpkg -i geoipupdate_6.1.0_linux_amd64.deb
To configure /etc/GeoIP.conf, go to your personal account https://www.maxmind.com/en/account and create a new licence key through the menu "my accounts" - "manage license key" and then enter the account ID and license key in the config. Also select "EditionIDs" (in my case - GeoLite2-Country).
We will not change other options, but we will take them into account. For example, the fact that by default the database is downloaded to the folder /usr/share/GeoIP.
Let's check what is in this folder:
ls -lh /usr/share/GeoIP
total 7.8M
-rw-r--r-- 1 root root 1.6M Nov 8 2018 GeoIP.dat
-rw-r--r-- 1 root root 6.2M Nov 8 2018 GeoIPv6.dat
Some old databases in the old format...
Let's try to update by simply running the command in the console
geoipupdate
Check again:
ls -lh /usr/share/GeoIP
total 14M
-rw-r--r-- 1 root root 1.6M Nov 8 2018 GeoIP.dat
-rw-r--r-- 1 root root 6.2M Nov 8 2018 GeoIPv6.dat
-rw-r--r-- 1 root root 6.1M Feb 20 13:15 GeoLite2-Country.mmdb
All that's left is to write a small bash script that will execute this command on cron and copy it to the necessary folders of the site or sites.
Let's check where the executable file is:
~# whereis geoipupdate
geoipupdate: /usr/bin/geoipupdate /usr/share/man/man1/geoipupdate.1
Now let's make a script that downloads databases from the Maxmind service and copies them to the folders where Drupal is located:
#!/bin/bash
/usr/bin/geoipupdate
$geoipdb="GeoLite2-Country.mmdb"
D7="/home/user/example.com/www/sites/all/libraries/geoip"
cp /usr/share/GeoIP/$geoipdb $D7/$geoipdb
chown user:www-data $D7/$geoipdb
chmod 644 $D7/$geoipdb
D10="/home/user/example2.com/www/web/sites/default/files"
cp /usr/share/GeoIP/$geoipdb $D10/$geoipdb
chown user:www-data $D10/$geoipdb
chmod 644 $D10/$geoipdb