Home » Proxy Articles » How to automatically update GeoLite Country
If you are using the MaxMind GeoLite Country open source database to filter your web proxy traffic then you need to ensure that your GeoLite database is updated at regular intervals. Doing this manually can be a bit of a chore especially if you are running a number of web proxies. In this article we explain how you can update your GeoLite database automatically using a simple PHP script to retrieve the data along with a crontab entry to automate the process.
This article assumes that you already have GeoLite Country installed on your web proxy, if you do not please see our article IP Blocking with GeoLite Country and PHProxy for installation instructions.
The first step is to open up Notepad or your preferred text editor and create a new file and save it to the root of your web proxy using the filename "update-geolite.php". Copy and paste the following code into the file:
<?php// Read remote data source
$ZP=@gzopen("http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz","r");
$UnzippedData=@gzread($ZP,2097152); // 2MB
@gzclose($ZP);
// Check we have data
if (strlen($UnzippedData)>0)
{
// Write data to local file
$LZFP=@fopen("/full/path/to/geolite/file/geoip.dat","w+");
@fwrite($LZFP,$UnzippedData);
@fclose($LZFP);
}
?>
You need to edit the file path on line 10 and change it from "/full/path/to/geolite/file/geoip.dat" to the location where your DAT file is located.
The second step is to change the permissions on your DAT file so that the new script has the correct access rights to overwrite the contents of the file. You can do this using most FTP applications and the permissions you need to set are 0777 using the chmod command.
Due to these permissions it is highly recommended that you do not store the DAT file at the root of your web proxy. We recommend that you create a randomly named directory to store the DAT file and create a blank index file to prevent people from prying into the directory.
After you have set the permissions on the DAT file and set the correct path on line 10 of the script you can upload the script to your server. Once uploaded call the script in your browser and you should get a blank screen as the script does not output any data. If you get any error messages then check the file path and permissions are correct.
If no errors are reported, you next need to check the DAT file itself to ensure that it was correctly updated. This is simply a case of browsing to the location in your FTP application and checking the size and timestamp of the file. If it was correctly updated the size should be greater than 0 bytes and the timestamp should have changed to the current date and time.
Once you have confirmed that the file is being correctly updated, the final step is to create a crontab entry to call the update script twice a month. MaxMind aims to update the database on the 1st of each month, however sometimes they are a little late so we update it twice just to be sure.
Experienced SSH users can copy the following cron entry directly into their crontab. Less experienced users should login to their server management application such as cPanel.
The cron entry below will request the update script on the 2nd and the 10th of each month at 4am using the wget command. You will need to edit the domain name to reflect that of your own.
0 4 2,10 * * wget -O- -q http://www.domain.com/update-geolite.php >> /dev/null 2>&1Thats all there is to it. Now just wait for the date of the next cron run and check the filetime on the DAT file to ensure that everything is running smoothly.