A PHP script to download Geolite 2 tar.gz files from Maxmind every week or month, and update your Country, City, or ASN mmdb file.
Prerequisites: Basic knowledge and experience of PHP. Ability to schedule script (cron job/task manager). Server running at least PHP 5.4.
The PHP Script:
Old image, the new script similarly reports the result of mmdb file updates.
Maxmind updates its GeoLite data files every month. Manually installing the latest data file on my site was one of those (never) do later tasks; so I decided to write my own PHP script to automate the process and share it here.
Please read the Warning/Disclaimer (below), and test before live use. You can set NOTIFICATION_MODE to “3” (screen display), to avoid bombarding yourself with emails during initial testing.
I’d appreciate feedback, especially on its use with Windows Servers (not tested in this environment); and what, if any, modifications are required.
About the Script:
The script is intended to:
- get a tar.gz file from Maxmind and copy it to your server
- extract the .mmdb database to a file in the same directory as the script (or other specified directory)
- check for errors and if necessary revert back to previous database file
- provide information to identify the cause of errors during testing/live running
- notify you of errors by email
To set up, just change the constants at the top of the script to suitable values for your site (see Configuring Script – below). If the script is installed and run in your site’s Maxmind “database” folder then you don’t even need to specify any paths or directories.
The script backs-up the current gzip and database file before update (prior back-ups are overwritten). On error, the database file (only) is recovered from the back-up.
Although designed for Maxmind, it can be modified to obtain and extract gzips and tars from other remote sites.
See below for scheduling, configuring and issues.
Scheduling the script
Cron job emails:
Cron notification emails do not differentiate between a script’s success and failure; and according to my host, most users do not read them.
The script provides its own email to more effectively alert you of errors, but obviously can’t identify errors in the cron job command itself; so you may still want to set the cron job to email you for stderr.
Example commands that may work for you:
php /path/to/update_maxmind2_dbfile.php or;
wget -O – http://example.com/update_maxmind2_dbfile.php
You may also need to insert a path in front of the php/wget command.
Some hosts block use of wget (but may allow curl).
Configuring the Script
To set up, you just have to change the values of the constants to ones appropriate to your site.
032| // the download URL for Maxmind database file 033| DEFINE ('FROM_URL', 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz'); 034| //DEFINE ('FROM_URL', 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz'); 035| //DEFINE ('FROM_URL', 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN.tar.gz'); 036| 037| // the directory location on your site where the Maxmind mmdb file should be installed: 038| DEFINE ('MY_MAXMIND_DATA_DIR', realpath( __DIR__ ) . '/'); 039| 040| // settings needed for notification of the result of update 041| DEFINE ('ADMIN_EMAIL_ADDRESS','email@example.com'); // your email address 042| DEFINE ('THIS_SITE', 'example.com'); // domain name of site (for use in notification email) 043| DEFINE ('NOTIFICATION_MODE', 1); // 1 = email, 2 = screen + email, 3 = screen
Lines 33 to 35 (Maxmind download URL):
You can use the script to update either the Country, City or ASN mmdb file.
Un-comment the line with the URL you need and “//” comment out the other 2 lines. e.g. if you want the script to update the City mmdb you would uncomment line 34 and comment out lines 33 & 35. The URLs are current as @ 6 July 18 but might be changed at some point by Maxmind. The script will email you if it is unable to connect to Maxmind.
Line 38 (folder location to which Maxmind mmdb file should be extracted):
038DEFINE ('MY_MAXMIND_DATA_DIR', realpath( __DIR__ ) . '/');
MY_MAXMIND_DATA_DIR is the folder path where the mmdb file will be extracted. The path value MUST end with a “/”. In the above line the path “string” is set to be the current folder (the one the script runs in). If you want to use another folder then replace “realpath( __DIR__ )” with your own path “string” in one of the usual ways e.g. hard coded path “‘/myhome/public_html/maxdata/‘” or use $_SERVER[‘DOCUMENT ROOT’] etc etc.
If the Maxmind Data directory you specify does not exist, the the script will create it for you. If the directory containing the script has 775 permissions then the new directory will also be given set with 775 permission; otherwise it will default to setting as 755.
Set “NOTIFICATION_MODE” to 2 (display messages), and run the script from a browser to test before changing NOTIFICATION_MODE back to 3 and scheduling it as a cron job.
The remainder of the constants are self explanatory.
Permission Denied (Unix/Linux and users, owners, permissions):
The script may report that it is unable to create/write to the files in your Maxmind Directory this is probably an ownership/permissions problem.
If you originally created a file via say by anonymous FTP it will be “owned” by that user; this probably won’t be the same user that your (CLI or Web Served) scripts run under. So 644 permissions (Owner: read/write; Group and World: read) would prevent the script writing to the file; likewise a script cannot access a directory without execute permissions.
In these circumstances, you can either change to less secure permissions, or delete the dir & file (make sure there are no other files you need in the dir) and use the script to create from scratch.
This forum answer provides more info on “Permission Denied” Problems.
Problems with getting the file (cURL)
File “links” on some sites may take too long, redirect, or require cURL to provide a user agent or referer etc etc (at the time of writing this is not the case with Maxmind). You can set curl options to cater for these type of problems.
Warning and disclaimer.
Use the script and suggestions on this page at your own risk.
I don’t claim to be an expert; I write about problems I’ve encountered and how I resolved them in my server environment. I’m human and make mistakes. Any code, content and suggestions are intended as a rough guide only. I can’t guarantee that the solutions on this site will work for you. Nor can I guarantee that they won’t be harmful to your particular site or business.
As always comments are welcome.
Andy Wrigley+ has worked in IT and Computer Audit for 30 years, and loves independent travel.