Install and Enable Mod_maxminddb for Geolocation

How to install and enable the mod_maxminddb module in Apache.

4843 views
d

By. Jacob

Edited: 2022-02-24 00:15

To install the mod_maxminddb module for Apache, you should download the tarball files from GitHub. You can find them under the releases tab on the GitHub repository.

Installing from the tarball is covered in the documentation: http://maxmind.github.io/mod_maxminddb/. But I will also repeat the steps in the article. Please check the official documentation for updated instructions!

If you did everything in this tutorial correctly, you should be able to access GEO information via the $_SERVER global in PHP. I.e.:

echo $_SERVER['COUNTRY_CODE']; // Sometimes return unexpected values (I.e. A1|A2|EU|AP)
// See: https://dev.maxmind.com/geoip/legacy/mod_geoip2/ for more information

Before we start

1.

Before installing the module, you also need to install the libmaxminddb library. This can be installed via PPA in Ubuntu.

sudo add-apt-repository ppa:maxmind/ppa
sudo apt update
sudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin

More info here: https://github.com/maxmind/libmaxminddb/blob/master/README.md

If you decide to install libmaxminddb from tarball rather than PPA, downloads are available here: https://github.com/maxmind/libmaxminddb/releases

2.

You also need to install the dev package for your apache version, in my case, this was done like below:

sudo apt install apache2-dev

If you have not installed the apache2-dev package, you might get errors such as this configure: error: apxs not found. set apxs with --with-apxs.

Installing MaxMind with Apache

If you are trying to install on your live server, likely in the cloud or on a server with your hosting company, you can download the tarball using the wget command.

Download the mod_maxminddb tarball from here: https://github.com/maxmind/mod_maxminddb/releases

The exact steps I took to install mod_maxminddb was as follows:

  1. wget [URL_TO_TARBALL_FILE] -P ~/
  2. tar -xvzf [PATH_TO_TARBALL_FILE] -C ~/
  3. cd [PATH_TO_EXTRACTED_TARBALL]
  4. ./configure
  5. sudo make install

Note. ~ is short for the home directory.

This automatically installs and enables the module.

All I had to do afterwards was to update my Apache configuration files with links for the database files. You download the database files from MaxMind's website, read on to lean how.

Installing or updating the Databases

Note. To automate the below process, read this tutorial: Auto-updating GeoIP databases

It is a good idea to try installing manually the first time so that you will be familiarize yourself with the process.

1.

Again, simply use wget to download the database file from maxminds website. Databases are found here: https://dev.maxmind.com/geoip/geoip2/geolite2/

2.

After downloading the files, extract the contents, and then move the database files to /usr/local/share/

Note. It may depend on your distribution, but sometimes you might loose files stored in non-standard locations when updating the system. You can avoid this risk by storing them in safe locations.

The /usr/local/share/ location is good, because it is a safe "standard" location to keep the databases.

Linking the GeoIP databases

1.

You should link to the database files in the Apache configuration files.

Inside the configuration add the following:

MaxMindDBEnable On
MaxMindDBFile COUNTRY_DB /usr/local/share/GeoIP/GeoLite2-Country.mmdb
MaxMindDBFile CITY_DB    /usr/local/share/GeoIP/GeoLite2-City.mmdb

MaxMindDBEnv COUNTRY_CODE COUNTRY_DB/country/iso_code
MaxMindDBEnv CONTINENT_CODE CITY_DB/continent/code

Since I am hosting multiple sites on my server, I added this in a VHOST file in /etc/apache2/sites-available, but you can also use .htaccess files or the main configuration.

2.

Update or reload the Apache configuration: service apache2 restart

Tell us what you think:

Oliver

Note that within the GeoLite2 "City" database the Country lookup / Country names etc. are included as well! So no need to also use GeoLite2-Country.mmdb if already using …City.mmdb

Just change in the Apache Config the Env-Config as follows:
`MaxMindDBEnv COUNTRY_CODE <b>CITY_DB</b>/country/iso_code`

Check this page at maxmind.com where it confirms that Country data is included in the City DB: https://www.maxmind.com/en/geoip2-precision-city-service

Cheers

Paul

I have this in my .htaccess to block countries

MaxMindDBEnable On
MaxMindDBFile DB /usr/local/share/GeoIP/GeoIP2-Country.mmdb
MaxMindDBEnv MM_COUNTRY_CODE DB/country/iso_code

SetEnvIf MM_COUNTRY_CODE ^(RU|DE|FR) BlockCountry
Deny from env=BlockCountry

That works well......

But how to allow only one country ?

Tried
Allow from eny=BlockCountry
but nogo yet....

Chrs..

  1. An in-dept look at the use of headings (h1-h6) and sections in HTML pages.
  2. Pagination can be a confusing thing to get right both practically and programmatically. I have put a lot of thought into this subject, and here I am giving you a few of the ideas I have been working with.
  3. The best way to deal with a trailing question mark is probably just to make it a bad request, because it is a very odd thing to find in a request URL.
  4. How to optimize image-loading and automatically include width and height attributes on img elements with PHP.
  5. HTTP headers are not case-sensitive, so we are free to convert them to all-lowercase in our applications.

More in: Web development