Home > 8月, 2009

2009.08.10

LinuxTIPS – ApacheにGeoIP(mod_geoip)導入メモ

IPアドレスから、
どの国、どの都市かまたどのISPかを判別する仕組みの1つにGeoIPというのがある。
ぱっと見てみた感じ、
国レベルでは、ライト版ライセンス(無償)で使用可能
都市、ISPだと提供元のmaxmindに申し込む(有料?)必要がありそうだ。
(企業で使用する場合は、企業ライセンスが必要?)

とりあえず簡単に導入してみる。

1. GeoIPとmod_geoipをインストール
# yum install mod_geoip.i386

- datファイル
/usr/share/GeoIP/GeoIP.dat
このファイルに情報がはいってる?
ちなみに、下記のGeoLiteCountryサイトからだと無償でdatファイルを取得できる。

http://www.maxmind.com/app/geolitecountry

2. 設定ファイル
/etc/GeoIP.conf
都市レベルで特定したい場合は、maxmindに申し込む必要がある。
そのライセンスキーとかを、うえの設定ファイルに書き込む。
国レベルは、設定ファイルはこのままでOK。

geoipupdateコマンドで
datファイルを更新するのにも申し込む必要がある。

3. geoiplookupコマンドで検索してみる
# geoiplookup yahoo.com
GeoIP Country Edition: US, United States
→ yahoo.comはUS。

# geoiplookup mixi.jp
GeoIP Country Edition: JP, Japan
→ mixi.jpをJP

# geoiplookup 66.249.71.177
GeoIP Country Edition: US, United States
→ 逆引きも検索可能。上はGoogle.com。

4. Apache設定
4-1. datファイルをメモリキャッシュさせる
# vi /etc/httpd/conf.d/mod_geoip.conf
GeoIPDBFile /usr/share/GeoIP/GeoIP.dat

GeoIPDBFile /usr/share/GeoIP/GeoIP.dat MemoryCache

MemoryCacheのほかに
MMapCache
IndexCache
CheckCache
がある。

4-2. 中国、ロシアからのアクセスを拒否する
# vi /etc/httpd/conf.d/mod_geoip.conf

例えば、

Order allow,deny
SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry
Deny from env=BlockCountry
Allow from all

4-3. 確認する
1. COUNTORY_CODEをJPにしてみる
2. BlockCountryに設定した国のOpenProxyを探してブラウザーで設定してアクセスしてみる。

っということで、
BLOGなどのコメントスパムなどで海外からのをブロックしたいけど、
IPアドレスで設定するのは面倒臭い!ってときにGeoIPは便利です。

Apacheの設定だけじゃなくて、もっと幅広くしようできます。
Postfixと連携されてSPAMをブロックしたり。

詳しいことは、
/usr/share/doc/mod_geoip-1.2.5/README
参照で!

参考URL: サイトアクセス者の位置情報を取得するGeoIP – SourceForge.JP Magazine


Apache辞典 (DESKTOP REFERENCE)Apache辞典 (DESKTOP REFERENCE)

LinuxTIPS – mysqltunerコマンドでMySQLをお手軽チューニング

1. mysqltunerをインストール
# yum install mysqltuner

2. mysqltunerを実行
# mysqltuner



>> MySQLTuner 0.9.9 – Major Hayden
>> Bug reports, feature requests, and downloads at http://mysqltuner.com/
>> Run with ‘–help’ for additional options and output filtering
Please enter your MySQL administrative login: hogehoge ← ユーザー名
Please enter your MySQL administrative password: arege ← そのパスワード

——– General Statistics ————————————————–
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.0.51a
[OK] Operating on 32-bit architecture with less than 2GB RAM

——– Storage Engine Statistics ——————————————-
[--] Status: -Archive +BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in MyISAM tables: 1M (Tables: 108)
[!!] InnoDB is enabled but isn’t being used
[!!] BDB is enabled but isn’t being used
[!!] Total fragmented tables: 11

——– Performance Metrics ————————————————-
[--] Up for: 15h 27m 23s (5K q [0.106 qps], 1K conn, TX: 18M, RX: 846K)
[--] Reads / Writes: 98% / 2%
[--] Total buffers: 34.0M global + 2.7M per thread (100 max threads)
[OK] Maximum possible memory usage: 302.7M (61% of installed RAM)
[OK] Slow queries: 0% (0/5K)
[OK] Highest usage of available connections: 3% (3/100)
[OK] Key buffer size / total MyISAM indexes: 8.0M/455.0K
[OK] Key buffer hit rate: 99.7% (23K cached / 76 reads)
[!!] Query cache is disabled
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 685 sorts)
[OK] Temporary tables created on disk: 14% (309 on disk / 2K total)
[!!] Thread cache is disabled
[!!] Table cache hit rate: 18% (64 open / 355 opened)
[OK] Open file limit used: 12% (128/1K)
[OK] Table locks acquired immediately: 100% (4K immediate / 4K locks)

——– Recommendations —————————————————–
General recommendations:
Add skip-innodb to MySQL configuration to disable InnoDB
Add skip-bdb to MySQL configuration to disable BDB
Run OPTIMIZE TABLE to defragment tables for better performance
MySQL started within last 24 hours – recommendations may be inaccurate
Enable the slow query log to troubleshoot bad queries
Set thread_cache_size to 4 as a starting value
Increase table_cache gradually to avoid file descriptor limits
Variables to adjust:
query_cache_size (>= 8M)
thread_cache_size (start at 4)
table_cache (> 64)

#


[!!]のところが変更したほうが良いところ。
で、
Recommendations
ってところで変更箇所を詳細に教えてくれる。

[!!] InnoDB is enabled but isn’t being used: InnoDB使ってないのに有効だよ
[!!] BDB is enabled but isn’t being used: BDB使ってないのに有効だよ
[!!] Total fragmented tables: 11: フラグメント起こしてるテーブルが11個あるよ

Add skip-innodb to MySQL configuration to disable InnoDB
→ my.cnfにskip-innodbを追加
Add skip-bdb to MySQL configuration to disable BDB
→ my.cnfにskip-bdbを追加
Run OPTIMIZE TABLE to defragment tables for better performance
→ OPTIMIZE TABLE テーブル名;を実行してテーブルを最適化する。

[!!] Query cache is disabled: Query cacheは無効になっている
[!!] Thread cache is disabled: Thread cache が無効になっている。
[!!] Table cache hit rate: 18% (64 open / 355 opened): テーブルのキャッシュヒット率が低い

query_cache_size (>= 8M)
→ my.cnfにuery_cache_size = 8Mを設定する
thread_cache_size (start at 4)
→ my.cnfにthread_cache_size = 4を設定する
table_cache (> 64)
→ my.cnfにtable_cache = 64を設定する

その他slow query log を設定したほうが良いよとかが書いてある。

自分の環境にあった、MySQLのチューニングがよくわからない場合
実行してみると良いかも。