mod geoip

Often it is desireable to Geotarget your web content. This is a way we do geotargetting using MaxMinds geoip database.

This document is a stepwise explanation of how to
1. obtain the relevant csv files from GeoIP
2. create the tables
3. import the data
4. use this data to figure out where your visitors are coming from

It is relevant when you use the MySQL based solution combined with PHP. There’s a simpler method to achieve this as well please use the contact form or post a comment here to discuss that.

-by Imtiaz A. Khan -230906

Step 1. -Obtain the relevant csv
GeoIP provides a free version of there database that can be used for pretty much all the functionality
of geotargetting The csv version of the database is available at

For the latest version Please visit :

Extract the compressed files after download completes. You should have two files namely..
GeoLiteCity-Blocks.csv and GeoLiteCity-Location.csv

Rename these to locip.csv and lookup.csv respectively. Remove all double qoutes from locip.csv.

The data in these files is under the following heads:
locip.csv: startIpNum,endIpNum,locId
lookup.csv: locId,country,region,city,postalCode,latitude,longitude,dmaCode,areaCode

We’re going to import these to the database in the next step. Before that remove
the header rows and any copyright information from the files as we need just the data.

Step 2. -create the database and the tables
We need a database for the two tables that will be used. And a table structure for the data.

login to your MySQL server and run the following commands:


USE geoip;

country CHAR(15) NOT NULL,
region CHAR(2) NOT NULL,
city CHAR(30) NOT NULL,
postalcode CHAR(15) NOT NULL,
latitude CHAR(15) NOT NULL,
longitude CHAR(15) NOT NULL,
dmaCode CHAR(15) NOT NULL,
areaCode CHAR(15) NOT NULL

Step 3. -import the data
Next we do the actual import. From the command line run the following
commands to import the data in csv files into the MySQL tables

mysqlimport –delete –local –fields-terminated-by=’,’
–lines-terminated-by=’n’ –user=’user’
–password=’password’ geoip locip.csv

mysqlimport –delete –local –fields-terminated-by=’,’
–fields-optionally-enclosed-by='”‘ –lines-terminated-by=’n’
–user=’user’ –password=’password’ geoip lookup.csv

NOTE: The names of files and the qoutes are important.

Check the data in the database by running random selects just to make sure all got in fine.

Step 4. -check your work
You can take a look at GeoIP in action using the following php script.

< ?php global $link; $link= mysql_connect("localhost","username","password"); if( !$link ) die("could not connect to MySQL"); mysql_select_db("geoip",$link) or die("Could't open organizer::" .mysql_error() ); $a=split(".", $REMOTE_ADDR); $ip=$a[0] . "." . $a[1] . "." . $a[2]. "." . $a[3]; $ip_num=16777216*$a[0] + 65536*$a[1] + 256*$a[2] + $a[3]; $sql_getlocid="SELECT locId FROM locip WHERE $ip_num >= start_ip AND $ip_num< = end_ip"; $result=mysql_query($sql_getlocid,$link); $row = mysql_fetch_assoc($result); $locid=$row["locId"]; $sql_getreg="SELECT * FROM lookup WHERE locId=" . $locid; $result=mysql_query($sql_getreg,$link); $row = mysql_fetch_assoc($result); $region=$row["region"]; echo "IP--->” . $REMOTE_ADDR . “”;
echo “Country—>” . $row[“country”] . “”;
echo “Region—>” . $row[“region”] . “”;
echo “City—>” . $row[“city”] . “”;
echo “Longitude—>” . $row[“longitude”] . “”;
echo “Latitude—>” . $row[“latitude”] . “”;
echo “Area Code—>” . $row[“areaCode”] . “”;
echo “Postal Code—>” . $row[“postalcode”] . “”;


This script when run properly should give you details of your own machines IP.
If the GeoIP database doesn’t have correct information about your IP then it’ll
not show some of the data.

To see different results as in how it would behave if someone else from someplace
else tried to access your machine try changing the proxy settings on your system and
accessing the script: Use the proxy list available here:

Remember the free database is 97% accurate for IPs in the USA and approximately 60%
for the rest of the world, which means even though it’ll tell you the country information
correct it might not be able to give you the finer details like city or region.

Please contact Imtiaz at in case you need assistance with your GeoIP setup/usage.

automate subdomain creation + Apache

How to setup a LAMP server with mod_vhost_alias capability.

This document tell you how to setup a webserver so that all you need to do is upload to a ftp folder and tada you have a new subdomain. Read on!

Install MySQL

1. prefereably install using yum or up2date. If they fail then follow
directions on

Build Apache

1. wget
2. tar -zxf httpd-2.2.3.tar.gz
3. cd httpd-2.2.3
4. configure the build using following options


5. make | grep -i error
6. make install
7. /usr/local/apache/bin/apachectl configtest
8. /usr/local/apache/bin/apachectl start

Integrate PHP

1. wget
2. tar -zxf php-5.1.6.tar.gz
3. cd php-5.1.6
4. Configure the build using following command

./configure --prefix=/apache

5. make | grep -i error
6. make install

7. Edit /usr/local/apache/conf/httpd.conf and add following lines at relevant sections
LoadModule php5_module        modules/
AddHandler php5-script php

# Add index.php to your DirectoryIndex line:
DirectoryIndex index.php index.html

AddType text/html       php

# PHP Syntax Coloring
AddType application/x-httpd-php-source phps

8. /usr/local/apache/bin/apachectl configtest
9. /usr/local/apache/bin/apachectl start

Configure mod_vhost_alias

1. Add a user for the vhosts which will be used for running the server and uploading content.
useradd vhost
passwd vhost

2. If it exists, uncomment the Load module line for the vhost_alias module
LoadModule vhost_alias_module libexec/httpd/

3. If it exists, uncomment the AddModule one too:
AddModule mod_vhost_alias.c

4. add the User and Group directives (add these outside of
User vhost
Group vhost

5. Look for the following section:
< Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
< /Directory >
change it to look as under:
< Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
< /Directory >

6. Now add this snippet at the end of the file:
VirtualDocumentRoot /home/vhost/%2+/%1
VirtualScriptAlias /home/vhost/%2+/%1/cgi-bin

7. Add content to the /home/vhost directory
i... ftp to the server using username and password created in step 1
ii... create a folder with the domain name as name eg. /home/vhost/
iii... upload a site for example in the folder www under the
folder for domain will mean you upload content to /home/vhost/

8. /usr/local/apache/bin/apachectl configtest
9. /usr/local/apache/bin/apachectl start

If you don't see any errors, everything should be working.

At this point, you should be able to add folders like /home/vhost/
and access those as