CentOS 8 - AWStats GeoIP 적용

GeoIP

공식 웹 사이트 : https://www.maxmind.com/en/home

GeoIP는 MaxMind업체에서 제공하는 IP 데이터베이스입니다. 국가나 또는 세분화된 도시에 할당된 네트워크 IP 대역대 정보를 한 곳에 모아둔 집합체라고 생각하시면 됩니다. GeoIP 데이터를 이용하여 인터넷 방문자 및 관련 콘텐츠 광고를 분석하고 인터넷 트래픽을 효율적으로 관리할 수 있습니다. 웹/메일/방화벽등 특정 국가(도시)에서 접근 및 제한을 할 수도 있어 보안적으로도 여러 솔루션 프로그램 플러그인으로 제공되고 있습니다. 현재는 [GeoIP2] 이름으로 서비스를 제공하고 있습니다.

 

※ 다만 아쉬운 점은 이전까지는 무료로 제공하여 자동 업데이트가 가능했으나 데이터 개인 정보 보호 규정으로

인해 [2019-12-30]부터 회원가입 및 무료 라이선스를 받아야 다운로드 액세스가 가능합니다.

※ GeoIP2는 유료화하여 월별(연간) 일정 비용을 지불해야 자동 업데이트(API 지원) 및 고객지원이 가능

※ 우리가 사용할 데이터베이스는 라이트 버전인 GeoLite2 무료 버전이며 GeoIP2에 필적할 만한 IP 데이터베이스
이지만 보다 정확하지 않을 수 있으며 고객지원이 불가합니다.


방법 1

※ dnf(yum) 이용하여 간단하게 적용이 가능하지만 GeoIP 데이터베이스가 최신 버전이 아닙니다.

AWStats처럼 통계 데이터용으로 이용할 경우에만 사용하시기 바랍니다.

STEP01 → EPEL저장소 및 GeoIP 다운

※ CentOS8에는 기본 저장소에는 GeoIP패키지가 없기 때문에 EPEL 저장소를 추가해야 합니다.

[root@localhost ]# dnf install epel-release

[root@localhost ]# dnf install GeoIP GeoIP-data perl-Geo-IP GeoIP-devel

 

패키지 설치 시 기본 경로

[root@localhost ]# ll /usr/share/GeoIP/

lrwxrwxrwx  1 root root       18 10월 19 02:03 GeoIP.dat -> GeoLiteCountry.dat

-rw-r--r--. 1 root root 56548946  6월  7  2018 GeoLite2-City.mmdb

-rw-r--r--. 1 root root  3423846  6월  7  2018 GeoLite2-Country.mmdb

-rw-r--r--  1 root root  1242574  4월  4  2018 GeoLiteCountry.dat

※ 소프트 링크가 걸려있는 [GeoLiteCountry.dat] 생성 날짜를 보면 아시겠지만 [2018-04-04] IP 데이터베이스입니다.

 

 

STEP02 → AWStats 사용자 Config 파일 추가 수정 및 통계 데이터 업데이트

※ 가상 호스트(여러 개의 도메인 이용 시) 각 Config 파일마다 수정해주시기 바랍니다.

[root@localhost ]# vim /etc/awstats/awstats.foxydog11.com.conf  (추가 및 수정)

 

   1455 # PLUGIN: GeoIP
   1456 # REQUIRED MODULES: Geo::IP or Geo::IP::PurePerl (from Maxmind)
   1457 # PARAMETERS: [GEOIP_STANDARD | GEOIP_MEMORY_CACHE] [/pathto/geoip.dat[+/pathto/override.txt]]

   1458 # DESCRIPTION: Builds a country chart and adds an entry to the hosts
   1459 # table with country name
   1460 # Replace spaces in the path of geoip data file with string "%20".

 

   1461 # [아래 주석(#) 제거 후 GeoIP.dat 파일이 있는 위치 경로를 잡아주면 됩니다.]
   1462 LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat"

 

Config 파일 수정으로 통계 데이터 재 업데이트

[root@localhost ]# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=foxydog11.com

 

 

STEP03 → GeoIP가 적용된 웹에서 통계 페이지 확인

※ 테스트 서버는 누적 통계 데이터 정보가 적어서 실서버의 데이터 스크린샷 화면을 가져왔습니다.

[예시] URL : http://localhost/awstats/awstats.pl?config=foxydog11.com


방법2 (메인)

※ 공식 사이트(MaxMind)에서 최신 무료 GeoLite2 데이터베이스 직접 다운로드하는 방법

※ 회원가입 및 무료 라이선스 획득이 필요하며 데이터베이스 확장자 자체가 [.MMDB]이나 [.CSV]로만 제공을 하기 때문에 일부 웹(Apache, Nginx), AWstats에서는 [.DAT]로만 불러와서 사용할 수 있으므로 변환이 필요합니다.

※ 아쉽게도 CentOS8에서는 아직까지 패키지나 업데이트가 되지 않았으며, 자료도 부족하여 파일 변환은 CentOS7에서 진행하고 파일을 가져오도록 하겠습니다.

 

 

STEP01 → MaxMind 가입 및 무료 라이선스 획득 후 IP 데이터베이스 다운

1-1 가입하기

URL 접속 : https://www.maxmind.com/en/geolite2/signup

 

1-2 메일 수신 확인 및 패스워드 설정

 

1-3 로그인 및 무료 라이선스 획득

URL 접속 : https://www.maxmind.com/en/accounts/current/people/current

[로그인] ▷ [Services(My License Key)] ▷ [Generate ner license key]

[순서대로]

1-4 파일 다운로드

 

 

STEP02 → CentOS7 서버에 GeoLite2 데이터베이스 파일 업로드

[root@localhost ~]# mkdir /usr/share/GeoLite2  (폴더 생성)

[root@localhost ~]# cd /usr/share/GeoLite2  (경로 이동)

[root@localhost GeoLite2 ~]# ll  (파일 확인)

-rw-r--r-- 1 root root 1854155 10월 22 10:08 GeoLite2-Country-CSV_20201020.zip

 

 

STEP03 → CentOS7에서 [.CSV] ▷ [.DAT] 확장자 변경

[root@localhost GeoLite2 ~]# yum install python-ipaddr  (파이썬 IPAddress 설치)

 

※ Github(sherpya) 개발자분께서 변환툴을 제공하고 있습니다.

URL : https://github.com/sherpya/geolite2legacy

 

[root@localhost GeoLite2 ~]# git clone https://github.com/sherpya/geolite2legacy.git  (툴 다운)

Cloning into 'geolite2legacy'...
remote: Enumerating objects: 120, done.

 

[root@localhost GeoLite2]# cd geolite2legacy  (경로 이동)

 

./geolite2legacy.py -i ../변환 할 파일대상(CSV파일 압축상태) -f geoname2fips.csv -o /경로/저장할 이름.dat

[root@localhost GeoLite2]# ./geolite2legacy.py -i ../GeoLite2-Country-CSV_20201020.zip -f geoname2fips.csv -o /usr/share/GeoLite2/GeoIP.dat  (전체 한 줄 명령어)

Database type Country - Blocks IPv4 - Encoding: utf-8

wrote 315362-node trie with 310464 networks (252 distinct labels) in 13 seconds  (정상 처리완료)

 

[root@localhost GeoLite2]# ll /ls /usr/share/GeoLite2/ | grep GeoIP.dat  (파일 생성 확인)

v-rw-r--r-- 1 root root 1892199 10월 22 10:30 GeoIP.dat

 

 

STEP04 → CentOS8에 [.DAT] 파일 업로드 후 테스트

[root@localhost ~]# ll /usr/share/GeoLite2  (업로드)

-rw-r--r-- 1 root root 1892199 10월 21 21:49 GeoIP.dat

 

[root@localhost ~]# vim /etc/awstats/awstats.foxydog11.com.conf  (파일 수정)

   1462 LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoLite2/GeoIP.dat"

 

[root@localhost ]# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=foxydog11.com

(재 업데이트)

 

※ 최종 접속 테스트(정상)

URL : http://localhost/awstats/awstats.pl?config=foxydog11.com

 

 

마치며

MaxMind GeoIP는 한번 가입하고 무료 라이선스를 받으면 다른 오픈소스에도 응용할 수 있는 부분이 많으므로 유용하게 이용 가능합니다.  CentOS8는 아직까지도 자료가 부족하여 오픈소스의 경우는 막히는 경우가 많네요. 구글에서 이 잡듯이 검색을 하지만 꽤 시간이 지났음에도 없는 경우가 많습니다. 공유를 안 하시는 건지 T^T 테스트를 하여 확인이 되면 추후 따로 업데이트하도록 하겠습니다.

Designed by JB FACTORY