I’ve recently was required to install PHP GeOIP on one of the Linux servers I do maintain here is how I did it:
1. Install support for GeoIP
– Luckily though on Debian Linux there is a bundled deb package, so installation is trivial and clean.
debian-server:~# apt-get install php5-geoip
2. Furthermore it’s necessery to grasp geoip’s city database
debian-server:~# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
– Extract the gz file to prepare it for use by PHP GeoIP:
debian-server:~# /bin/gunzip GeoLiteCity.dat.gz
– Move the GeoLiteCity.dat file to /usr/share/GeoIP like so:
debian-server:~# mv GeoLiteCity.dat /usr/share/GeoIP
Now in order for the new geoip database to be red by PHP GeoIP we need to restart the Apache Webserver.
debian-server:~# /etc/init.d/apache2 restart
3. It’s also recommendable to update periodically the Geo IP GeoLiteCity.dat database in order to be always up-to-date with the latest IP location information provided out there, to do so I’ve created a 3 liner that does the job.
To start using the geoip_update.sh download it from here and set it to execute via cron.
debian-server:~# cd /usr/sbin;
debian-server:~# wget https://www.pc-freak.net/bshscr/geoip_update.sh
debian-server:~# echo "02 00 1 * * /usr/sbin/geoip_update.sh >/dev/null 2>&1" | crontab -
That’s all now PHP GeoIP database will be updated once a month on day 01 at 02:00 a.m.
Now in order to check if your PHP GeoIP is ready to go and used by php scripts.
Create a new sample script, let’s say check_php_geoip.php and put in it:
Place the script somewhere under your Apache document root and invoke it via a browser, if it executes correctly you should get an output similar to:
Array ( [country_code] => BG [country_code3] => BGR [country_name] => Bulgaria [region] => 40 [city] => Dobric [postal_code] => [latitude] => 43.5666999817 [longitude] => 27.8332996368 [dma_code] => 0 [area_code] => 0 )