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
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
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:
- wget [URL_TO_TARBALL_FILE] -P ~/
- tar -xvzf [PATH_TO_TARBALL_FILE] -C ~/
- cd [PATH_TO_EXTRACTED_TARBALL]
- 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
To automate the below process, read this tutorial: Auto-updating GeoIP databases
It is a good idea to do it manually the first time, so that you will be familiar with how it is done.
Again, simply use wget to download the database file from maxminds website. Databases are found here: https://dev.maxmind.com/geoip/geoip2/geolite2/
After downloading the files, extract the contents, and then move the database files to /usr/local/share/
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
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.
Update or reload the Apache configuration: service apache2 restart