{"id":40,"date":"2012-03-02T01:48:38","date_gmt":"2012-03-02T01:48:38","guid":{"rendered":"http:\/\/codebangers.com\/?p=40"},"modified":"2012-03-18T20:43:53","modified_gmt":"2012-03-18T20:43:53","slug":"40","status":"publish","type":"post","link":"https:\/\/codebangers.com\/40\/","title":{"rendered":"How to install Maxmind\u2019s GeoIP on Ubuntu\/Linux for PHP"},"content":{"rendered":"<p>provides a useful database and API for server side IP Geolocation. It is useful when you don\u2019t want your server to hang on external services each time fetching a location from an IP address is needed. This posts goes through the installation process of the database with a PHP extension on an Ubuntu platform. I\u2019m writing this for my own use. If it can be useful to you, yeepee.<br \/>\nSee it working\u00a0<a href=\"http:\/\/apps.cibul.org\/maxmind\/\" target=\"_blank\" rel=\"noopener\">here<\/a>. I\u2019ll appreciate it if you can comment at the bottom of the page on how off from the result showed on the page is from your actual location.<br \/>\nOpen a terminal<br \/>\nDownload the C library in any location on your system (the latest as of today is\u00a0<a href=\"http:\/\/geolite.maxmind.com\/download\/geoip\/api\/c\/GeoIP-1.4.6.tar.gz\" target=\"_blank\" rel=\"noopener\">here<\/a>):<\/p>\n<p style=\"text-align: center;\">[cc lang=&#8221;php&#8221; tab_size=&#8221;2&#8243; lines=&#8221;40&#8243;]<br \/>\nwget\u00a0http:\/\/geolite.maxmind.com\/download\/geoip\/api\/c\/GeoIP-1.4.6.tar.gz<br \/>\n[\/cc]<br \/>\nDecompress it:<br \/>\n<code>tar\u00a0-xvzf\u00a0GeoIP-1.4.6.tar.gz<\/code><br \/>\nand go into the folder to type the following:<br \/>\n[cc lang=&#8221;php&#8221; tab_size=&#8221;2&#8243; lines=&#8221;40&#8243;]<br \/>\nsudo\u00a0.\/configure<br \/>\nsudo\u00a0make<br \/>\nsudo\u00a0make\u00a0check<br \/>\nsudo\u00a0make\u00a0install<br \/>\n[\/cc]<\/p>\n<p>Download the latest PHP extension (today its\u00a0<a href=\"http:\/\/pecl.php.net\/get\/geoip-1.0.7.tgz\" target=\"_blank\" rel=\"noopener\">here<\/a>)<br \/>\nDecompress it, go in the geoip folder and type<br \/>\n[cc lang=&#8221;php&#8221; tab_size=&#8221;2&#8243; lines=&#8221;40&#8243;]<br \/>\nsudo\u00a0phpize<br \/>\nsudo\u00a0.\/configure<br \/>\nsudo\u00a0make<br \/>\nsudo\u00a0make\u00a0install<br \/>\n[\/cc]<br \/>\nAdd the following line in your php.ini file used by apache (in my case: \/etc\/php5\/apache2\/php.ini):<br \/>\n[cc lang=&#8221;php&#8221; tab_size=&#8221;2&#8243; lines=&#8221;40&#8243;]<br \/>\nextension=geoip.so<br \/>\n[\/cc]<br \/>\nTo see if things have been working well until now, create a php test web page. We\u2019ll make it prettier later on.<br \/>\nTo complete the installation, unzip and set\u00a0<a href=\"http:\/\/geolite.maxmind.com\/download\/geoip\/database\/GeoLiteCity.dat.gz\" target=\"_blank\" rel=\"noopener\">GeoLiteCity<\/a>\u00a0database in the folder indicated by the test page (here \/usr\/local\/share\/GeoIP\/GeoIPCity.dat).<br \/>\nOnce the database is downloaded and placed in the correct location, use the following functions to get the latitude or longitude from the ip address:<\/p>\n<p>[cc lang=&#8221;php&#8221; tab_size=&#8221;2&#8243; lines=&#8221;40&#8243;]<br \/>\n$ip_location\u00a0=\u00a0geoip_record_by_name($_SERVER[&#8216;REMOTE_ADDR&#8217;]);<br \/>\n$ip_location[&#8216;latitude&#8217;];<br \/>\n$ip_location[&#8216;longitude&#8217;];<br \/>\n[\/cc]<br \/>\nTo wrap things up, the following code puts the retrieved latitude and longitude on a neat little google map. Paste this in your test page:<br \/>\nThings should be working now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>provides a useful database and API for server side IP Geolocation. It is useful when you don\u2019t want your server to hang [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":9,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,30,15,4,31,5],"tags":[9,10,8,7,11],"class_list":["post-40","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-geolocation","category-howto","category-linux-1","category-php","category-programming","category-ubuntu","tag-geoip","tag-linux","tag-php-2","tag-php-geoip","tag-ubuntu-2"],"_links":{"self":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":23,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":222,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts\/40\/revisions\/222"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/media\/9"}],"wp:attachment":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}