Alecriar Studioの中の人の技術メモ

横浜の個人事業主が日々の技術的な情報をつづります

GeoIPデータベースを提供するGeoLite2ライセンス変更の対応

ライセンス形態の変更

alecriarstudio.hatenablog.com

以前の記事で、ApacheでGeoIPデータベースを利用する方法を紹介しました。その後、GeoIPデータベースの提供元であるMaxMindでライセンスの変更があり、フリーの利用であってもアカウント登録が必須になったようなので、今回はそのやり方を記事にします。

アカウント登録

まず、以下の GeoLite2 データベースのサイトへアクセスします。

dev.maxmind.com

f:id:alecriarstudio:20200309162417p:plain
GeoLite2 Free Downloadable Databases

画面下部にある「SIGN UP FOR GEOLITE2」というボタンを押します。

f:id:alecriarstudio:20200309162535p:plain
GeoLite2 Sign Up

ユーザー情報登録画面になりますので、「Full name(名前)」と「Company(会社名)」、「Industry(職業)」、「Country(国)」、「Intended use(使用目的)」、「Email address(メールアドレス)」を入力します。これらの情報は必須項目です。

「Email address」は確実に送受信できるメールアドレスを入力します。このメールアドレスはユーザー名としても利用されます。

すべて入力後、「Continue」ボタンを押せば、入力したメールアドレス宛てにすぐに以下のようなメールが届きます。

f:id:alecriarstudio:20200309162950p:plain
Welcome to MaxMind

メール本文中に「To log in, you must first create a password here.」と書かれていますが、こちらでまずパスワードの設定が必要です。「here」はリンクになっていますのでクリックするとパスワード作成画面に変遷します。

f:id:alecriarstudio:20200309163402p:plain
Set Password

自身で考えたパスワードを「Password」と「Confirm Password」の両方に記入し、「Reset password」を押します。

f:id:alecriarstudio:20200309163549p:plain
Password Set

パスワードが設定され、ログイン画面へのリンクが表示されます。「Click here to log in」を選択します。

f:id:alecriarstudio:20200309163749p:plain
Login Form

ログイン画面が表示されますので、「Username」に登録時に使用したメールアドレスを記入し、「Password」に今しがた設定したパスワードを入力し「Login」ボタンを押します。

f:id:alecriarstudio:20200309163918p:plain
Account Summary

ログインに成功すれば管理画面が表示されます。

ここで左メニュー下部にある「My License Key」を選択します。

f:id:alecriarstudio:20200309164240p:plain
License Keys

「Generate new license key」ボタンを押します。

f:id:alecriarstudio:20200309164440p:plain
Confirm generation of new license key

「License key description」にはライセンスキーの名前を自由に入力します。そのライセンスキーをどこで使用するかなど、識別しやすい名前にすると良いでしょう。

その下の設問は geoipupdate で使用するための設定ファイルを生成するものです。特にどちらでも構いませんが、ここでは生成するようにした画面例です。

最後に「Confirm」ボタンを押します。

f:id:alecriarstudio:20200309165030p:plain
New license key successfully created

新たなライセンスキーが生成されます。ここで発行された「Account/User ID」と「License key」は後ほど使用します。

「Download Config」ボタンを押せば geoipupdate で使用するための設定ファイル GeoIP.conf がダウンロードできますので、そちらを後の項目で説明する設定ファイルとして使用することもできます。

geoipupdate の設定

以下は Arch Linux での設定例です。

geoipupdate インストール

GeoIPデータベースを Arch Linux で利用するためのツール、 geoipupdate を以下のコマンドでインストールします。

$ pikaur -S geoipupdate

※pikaur はAURヘルパー。 yayやyaourtなどの他のAURヘルパーでも可

/etc/GeoIP.conf

設定ファイル /etc/GeoIP.conf を以下のように編集します。

/etc/GeoIP.conf

# Please see https://dev.maxmind.com/geoip/geoipupdate/ for instructions
# on setting up geoipupdate, including information on how to download a
# pre-filled GeoIP.conf file.

# Replace YOUR_ACCOUNT_ID_HERE and YOUR_LICENSE_KEY_HERE with an active account
# ID and license key combination associated with your MaxMind account. These
# are available from https://www.maxmind.com/en/my_license_key.
AccountID [アカウント登録で取得した「Account/User ID」を指定]
LicenseKey [アカウント登録で取得した「License key」を指定]

# Enter the edition IDs of the databases you would like to update.
# Multiple edition IDs are separated by spaces.
EditionIDs GeoLite2-ASN GeoLite2-City GeoLite2-Country

# The remaining settings are OPTIONAL.

# The directory to store the database files. Defaults to /var/lib/GeoIP
# DatabaseDirectory /var/lib/GeoIP

# The server to use. Defaults to "updates.maxmind.com".
# Host updates.maxmind.com

# The proxy host name or IP address. You may optionally specify a
# port number, e.g., 127.0.0.1:8888. If no port number is specified, 1080
# will be used.
# Proxy 127.0.0.1:8888

# The user name and password to use with your proxy server.
# ProxyUserPassword username:password

# Whether to preserve modification times of files downloaded from the server.
# Defaults to "0".
# PreserveFileTimes 0

# The lock file to use. This ensures only one geoipupdate process can run at a
# time.
# Note: Once created, this lockfile is not removed from the filesystem.
# Defaults to ".geoipupdate.lock" under the DatabaseDirectory.
# LockFile /var/lib/GeoIP/.geoipupdate.lock

geoipupdate の実行

動作確認のため、geoipupdate を手動で実行します。

$ sudo geoipupdate -v

実行後、/var/lib/GeoIP 以下に以下のファイルが存在していれば、正常な動作です。

  • GeoLite2-ASN.mmdb
  • GeoLite2-City.mmdb
  • GeoLite2-Country.mmdb

これらのファイルを Apache で使用する方法については、以下の記事をご参照ください。

alecriarstudio.hatenablog.com

定期的にGeoIPデータベースを最新にする

systemdタイマーを使用します。geoipupdate パッケージに含まれています。

$ sudo systemctl start geoipupdate.timer
$ sudo systemctl enable geoipupdate.timer