Installing and using a free GeoIP database

Today's article is about install and update GeoIP database on Debian-like systems, integrate it with PHP and Apache.
To install GeoIP on Debian 8 or Ubuntu 16.04 simple run this command:
apt-get install geoip-database geoip-database-extra

Now you have a GeoIP installed. But databases are obsolete. To update GeoIP databases, find data files on your system:
dpkg -L geoip-database | grep GeoIP.dat

You get a path like /usr/share/GeoIP/
Go to that directory and download updates:
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

Unpack files with replace old:
gzip -df *.gz

Rename GeoLiteCity.dat to GeoIPCity.dat:
mv -f GeoLiteCity.dat GeoIPCity.dat

You can put all these commands into one bash file (e.g. geoipupd.sh) to run as single command:
#!/bin/bash
cd /usr/share/GeoIP/
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gzip -df *.gz
mv -f GeoLiteCity.dat GeoIPCity.dat

Don't forget to add executable rights (chmod +x geoipupd.sh). Also, you can put this script to /etc/cron.mounthly
Okay, you have now installed GeoIP database with fresh data. To use in PHP, you should install PHP-GeoIp module:
apt-get install php-geoip

Now you can use GeoIP functions in PHP:
geoip_country_code_by_name('8.8.8.8');

To use GeoIP directly with Apache, install corresponding Apache module:
apt-get install libapache2-mod-geoip

That's all.

Comments

Popular posts from this blog

Create custom Spinner on ActionBar