How to display country code in the Apache log using the IP2Location Apache module

How to display country code in the Apache log using the IP2Location Apache module

Intro

Apache is a popular open-source web server being used in both the Linux and Windows environments. In this article, we will demonstrate how to display country code to your Apache log via the IP2Location Apache Module for the Debian Linux environment.

Pre-requisites

We will assume that you have a functioning Apache 2.4 web server. We will not cover the installation of Apache 2.4 as there are many guides on the web for that. You will need to install the apache2-dev package if you don’t have it installed.

sudo apt-get install apache2-dev

 

The last pre-requisite is the IP2Location BIN database file which you can get from:

https://www.ip2location.com/database/ip2location (commercial)

or

https://lite.ip2location.com/ip2location-lite (free LITE version, less accurate)

Download and decompress the BIN file into a folder called ip2location.

Installation

First of all, we need to get the IP2Location C library via the below link:

https://github.com/chrislim2888/IP2Location-C-Library/archive/master.zip

Download the above and decompress in a folder called ip2location.

Navigate to the IP2Location-C-Library-master sub-folder.

cd IP2Location-C-Library-master

Now, compile the IP2Location C library by running the below commands.

sudo autoreconf -i -v --force
sudo ./configure
sudo make
sudo make install

Next, get the IP2Location Apache module via the below link:

https://github.com/ip2location/ip2location-apache/archive/master.zip

Download the above and decompress into the ip2location folder from above.

In your ip2location folder, you should now have 2 sub-folders called ip2location-apache-master and IP2Location-C-Library-master. Navigate to the ip2location-apache-master folder.

cd ../ip2location-apache-master

Now, we will compile both the IP2Location C library and the IP2Location Apache module.

sudo apxs2 -i -a -L /usr/local/lib/ -I ../IP2Location-C-Library-master/libIP2Location/ -l IP2Location -c mod_ip2location.c

Configuration

Add following lines into the /etc/apache2/apache2.conf file. Modify the IP2LocationDBFile parameter for your own folder and BIN filename.

<IfModule mod_ip2location.c>
  IP2LocationEnable On
  IP2LocationDetectProxy Off
  IP2LocationSetmode ENV
  IP2LocationDBFile "/home/admin/ip2location/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN"
</IfModule>

In that file, look for the row below:

LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

Change it to look like the below:

LogFormat "%{IP2LOCATION_COUNTRY_SHORT}e %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

Then save the file. The above format modification adds another field to the front of the log entry to show the country code for the geolocation information of that IP address.

Restart Apache for the above settings to take effect

Run the following command to restart.

sudo systemctl restart apache2

Open your browser and navigate to your webpage

Your log should now have an entry like the below:

MY 175.138.99.179 - - [24/Oct/2019:06:08:02 +0000] "GET / HTTP/1.1" 200 3380 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"

In this case, the country code is MY for Malaysia.

Was this article helpful?

Related Articles