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:
<php
print_r(geoip_record_by_name('php.net'));
>
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 )
More helpful Articles
Tags: How to Install and Setup and Test GeOIP support in PHP on Apache2 in Debian Lenny Linux
Mozilla/5.0 (X11; U; Linux x86_64; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/5.0 Safari/531.2+ Debian/squeeze (2.30.6-1) Epiphany/2.30.6
Note that after
http://www.pc-freak.net/bshscr/geoip_update.sh
You will get geoip_update.sh.gz, you need to unzip it before you proceed with the steps to set it in crontab.
so exec:
# gzip -d geoip_update.sh.gz
View CommentView CommentMozilla/5.0 (X11; U; Linux x86_64; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/5.0 Safari/531.2+ Debian/squeeze (2.30.6-1) Epiphany/2.30.6
Another note is that by default the php5-geoip module will look up for the file:
/usr/share/GeoIP/GeoIPCity.dat and your geoip won’t work correctly, will quit immediately after test with an error message of:
Warning: geoip_record_by_name(): Required database not available at /usr/share/GeoIP/GeoIPCity.dat. in /root/geoip.php on line 2
Fixing this issue comes to:
mv /usr/share/GeoIP/GeoIPCityLite.dat /usr/share/GeoIP/GeoIPCity.dat
View CommentView CommentMozilla/5.0 (X11; U; Linux x86_64; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/5.0 Safari/531.2+ Debian/squeeze (2.30.6-1) Epiphany/2.30.6
Also don’t forget to make the geoip_update.sh executable with:
chmod +x geoip_update.sh
View CommentView CommentMozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36
Est-il possible de vous reprendre certaines phrases sur mon blog perso ?
View CommentView CommentMozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/64.0.3282.140 Chrome/64.0.3282.140 Safari/537.36
Thanks!
I have been trying to do this for php7 with not positive results. It would be great if you could address this on the latest php version/
Nice website btw!
Many blessings
😉
View CommentView Comment