Saturday, March 15, 2008

GeoIP libarary install for apache2 and ruby

1. Download the C API from http://www.maxmind.com/download/geoip/api/c/
2. Do the following to build and install GeoIP:
{{{
    ./configure
    make
    make check
    sudo make install
}}}
3. Download the ruby API from http://www.maxmind.com/download/geoip/api/ruby/
4. Do the following to build and install GeoIP ruby:
{{{
    ruby extconf.rb --with-geoip-include=/usr/local/include
    make
    sudo make install
}}}
5. Install mod_geoip
{{{
    sudo apxs -i -a -L/usr/local/lib -I/usr/local/include -lGeoIP -c mod_geoip.c
}}}
6. place
{{{
<IfModule mod_geoip.c>
    GeoIPEnable On
    GeoIPDBFile /usr/local/share/GeoIP/GeoIP.dat
LoadModule geoip_module /usr/lib/httpd/modules/mod_geoip.so
</IfModule>
}}}
inside your httpd.conf file.
If mod_geoip doesn't work, try taking the LoadModule geoip_module line outside the block.
If you get a "libGeoIP.so.1: cannot open shared object file: No such file or directory" error, add /usr/local/lib to /etc/ld.so.conf then run
/sbin/ldconfig /etc/ld.so.conf
7. mod_geoip can't install on Mac OSX because some unknown problem.
8. GeoIp ruby module install on Mac OSX is also some problem, but can install follow instruction of INSTALL
9. ruby example:


require 'net/geoip'

# access_type # Net::GeoIP::TYPE_DISK and Net::GeoIP::TYPE_RAM
#
g = Net::GeoIP.new(Net::GeoIP::TYPE_DISK)
# g = Net::GeoIP.open(db_filename[, access_type])


puts g.country_code_by_addr('220.181.37.55')
puts g.country_code_by_name('www.baidu.com')
puts g.country_code3_by_addr('220.181.37.55')
puts g.country_code3_by_name('www.baidu.com')
puts g.country_name_by_addr('220.181.37.55')
puts g.country_name_by_name('www.baidu.com')
puts g.country_id_by_addr('220.181.37.55')
puts g.country_id_by_name('www.baidu.com')
# >> CN
# >> CN
# >> CHN
# >> CHN
# >> China
# >> China
# >> 48
# >> 48

# puts g.region_by_addr('220.181.37.55') # Invalid database type GeoIP Country Edition, expected GeoIP Region Edition, Rev 1
# puts g.region_by_name('www.baidu.com') # Invalid database type GeoIP Country Edition, expected GeoIP Region Edition, Rev 1

# g.database_info() # Misc database info
# g.update_database(your_key_here[,debug]) # Updates your database

Reference:
1. http://www.maxmind.com/download/geoip/api/ruby/INSTALL
2. http://rubyforge.org/scm/?group_id=947
3. http://www.maxmind.com/app/ruby
4. http://www.howtoforge.com/mod-geoip-apache2-debian-etch
5. http://www.maxmind.com/app/mod_geoip

No comments :