ブログのエントリが増えてきた為か、自宅Webサーバのパフォーマンスが微妙に落ちてきた様に感じられた。
・・・ブログエントリの増加以外に何も変わってないので原因はそれで間違いないと思った。
サーバにログインして、メモリ使用量を見たら、スワップを282Mバイトも使った形跡があった。
RAMは512Mバイトの内、ほとんどを食い潰していた。
あーこれはイカン(^^;
以前、RAM容量を256Mバイトから512Mバイトに増設した時には、非力なマシンながらも劇的にパフォーマンスが上がって、スワップなんて使わないようになったのだったが、いつの間にか、各アプリケーションの使用メモリ量が増えていたと見える。
と、云う訳で、パフォーマンス向上の為のささやかな抵抗をしてみる事にした。
============
まずは方針。
パフォーマンスを維持しつつ、メモリ使用量を減らす。
その為には何が出来るだろう。
ムダにメモリを喰っている所は何処か。
常道のチェックポイントを上げると
- Apache2の機能拡張モジュール
- Apache2の予備プロセス数
- MySQL等のキャッシュサイズ
- 使われない常駐アプリケーション
最も「もったいない」のは、Apache2の予備プロセスである。
高アクセスのサイトであればいざ知らず、規定値のままでは最低10プロセスもプリフォークされる。
しかも、最大25プロセスも維持される。
ここを減らそう。
更に、ブログに使っているMovableType4.12の動作速度を上げる為に、Apache2.2にmod_perlを入れよう。
しかしここで気をつけなければならないのは、perlインタプリタモジュールがApacheのプロセス自体に組み込まれることにより、全Apache子プロセスのサイズが増えてしまう事だ。
充分にメモリ使用量を計算しないと、逆にスワップ等でパフォーマンスが落ちてしまう。
それと、mod_perlを使用する場合、動的Webページのみならず、静的ページにおいても、1ページ分の処理が終わる(又はKeepAliveTimeOutに達する)まで、処理割込ができなくなるという短所がある。
KeepAliveTimeOutは短めに設定しないと、接続しにくいサイトとなってしまう。
この為、mod_perlを使う場合は、KeepAlive Offとするのが「デフォルト」である。
============
実際の設定は以下の通り。
(OSはFreeBSD6.3, Apache2.2.9, Perl5.8.8)
1.mod_perl(2.0.4)の導入
# cd /usr/ports/www/mod_perl2/
# make install
2./usr/local/etc/apache22/httpd.confの編集(差分ファイルの内容)
--- ./httpd.conf.20080712_old 2008-07-06 23:03:18.000000000 +0900
+++ ./httpd.conf 2008-07-12 12:57:25.000000000 +0900
@@ -100,6 +100,7 @@
LoadModule alias_module libexec/apache22/mod_alias.so
LoadModule rewrite_module libexec/apache22/mod_rewrite.so
LoadModule php4_module libexec/apache22/libphp4.so
+LoadModule perl_module libexec/apache22/mod_perl.so
3./usr/local/etc/apache22/extra/httpd-default.confの編集(差分ファイルの内容)
--- ./httpd-default.conf.20080712_old 2008-01-23 22:22:14.000000000 +0900
+++ ./httpd-default.conf 2008-07-12 12:14:48.000000000 +0900
@@ -20,13 +20,13 @@
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
-MaxKeepAliveRequests 100
+MaxKeepAliveRequests 40
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
-KeepAliveTimeout 5
+KeepAliveTimeout 7
#
# UseCanonicalName: Determines how Apache constructs self-referencing
@@ -74,5 +74,5 @@
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
-#HostnameLookups Off
-HostnameLookups On
+HostnameLookups Off
+#HostnameLookups On
4./usr/local/etc/apache22/extra/httpd-mpm.confの編集(差分ファイルの内容)
--- ./httpd-mpm.conf.20080712_old 2008-07-06 21:01:51.000000000 +0900
+++ ./httpd-mpm.conf 2008-07-12 15:11:12.000000000 +0900
@@ -34,11 +34,11 @@
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
- StartServers 5
- MinSpareServers 5
- MaxSpareServers 10
- MaxClients 150
+ StartServers 2
+ MinSpareServers 2
+ MaxSpareServers 4
+ MaxClients 30
# worker MPM
@@ -50,11 +50,11 @@
# MaxRequestsPerChild: maximum number of requests a server process serves
StartServers 2
- MaxClients 150
+ MaxClients 30
MinSpareThreads 25
- MaxSpareThreads 75
+ MaxSpareThreads 48
ThreadsPerChild 25
5.Apacheのリスタート
# /usr/local/etc/rc.d/apache22 restart
============
実際に動かしてみた感想としては、まあまあかなぁ、と。
1ページを表示し終わるまでの時間は確実に短くなったと感じるのだが、表示し始めるまでに一呼吸置くように思える。
ま、今後も少しずつパラメータを調整するとしましょう。






コメントする