WordPress を導入してからサーバーのチューニングじみた事ばかり
している気がしないでもないが、今回は体感できるチューニングってことで、
PHP のアクセラレーターである eAccelerator を導入した。

で、メモメモ。(長い YO)

以前、デスクトップなマシンでサーバーを構築していたいときは APC という
アクセラレーターを入れて PHP の高速化を図っていたわけなんだけど、
APC はインスコがめんどい…。

そこで色々みてたんだけど、PHPA っていうのも結構いけてるようで。
これ実際いれてみたんだけど PHP5 には未対応とのことで、なんら意味のない
物だった。だけどやたら導入は簡単だったので、PHP4 を使ってる人は
PHPA でもいいんでないかなという感じ。
肝心な PHP5 に対応しているアクセラレーターってのは eAccelerator か APC
という選択肢しかないみたい。
上にも書いたけど APC はインスコがメンドイという体験をしただけに今回は
新たに eAccelerator で行ってみようジャマイカということで突撃した。

先ずは eAccelerator 本体のソースを取ってこんといけないねということで
公式サイトより最新の Stable な物を取得してきましょう。
2007/06/30 現在のバージョンは 0.95 となっている。

適当なところへ落としてゴニョゴニョしてあげる。
以下、うちの場合のオペ。

# wget http://jaist.dl.sourceforge.net/sourceforge/eaccelerator/eaccelerator-0.9.5.tar.bz2 
# tar jxvf eaccelerator-0.9.5.tar.bz2 
# cd eaccelerator-0.9.5 
# phpize 
Configuring for: 
PHP Api Version:         20041225 
Zend Module Api No:      20060613 
Zend Extension Api No:   220060519 
# ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/bin/php-config 
(略 
# make 
(略 
# make test 
Build complete. 
 
Don't forget to run 'make test'. 
 
bogus test name tests/ 
# make install 
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20060613/ 
                                  (このパス以下にモジュールがコピーされるので憶えておく) 
# vi /usr/local/lib/php.ini 
(このファイル末尾に以下の内容を追記するだけ) 
[eaccelerator] 
zend_extension="/usr/local/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" 
eaccelerator.shm_size = "32" 
eaccelerator.cache_dir = "/tmp/eaccelerator" 
eaccelerator.enable = "1" 
eaccelerator.optimizer = "1" 
eaccelerator.check_mtime = "1" 
eaccelerator.debug = "0" 
eaccelerator.filter = "" 
eaccelerator.shm_max = "0" 
eaccelerator.shm_ttl = "0" 
eaccelerator.shm_prune_period = "0" 
eaccelerator.shm_only = "0" 
eaccelerator.compress = "1" 
eaccelerator.compress_level = "9" 
eaccelerator.keys     = "shm_and_disk" 
eaccelerator.sessions = "shm_and_disk" 
eaccelerator.content  = "shm_and_disk" 
eaccelerator.allowed_admin_path = "control.php を使うならここに設置している絶対パスを入れる" 
eaccelerator.log_file = "ログとるならここにファイル名を含む絶対パスを入れる" 
(:wq して終わる) 
 
# mkdir /tmp/eaccelerator 
# cp ./control.php /path/to/control.php 
# chown www:www /path/to/control.php 
# vi /path/to/control.php 
/** config **/ 
$user = "ユーザ名"; 
$pw = "パスワード"; 
/** /config **/ 
(ユーザ名とパスだけ設定して :wq して終わり) 
# php -v 
PHP 5.2.3 (cli) (built: Jun 15 2007 02:28:58) 
Copyright (c) 1997-2007 The PHP Group 
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies 
    with eAccelerator v0.9.5, Copyright (c) 2004-2006 eAccelerator, by eAccelerator

ここまで出来たら Apache を再起動かけて完了と。
動作確認で「control.php」を設置したパスにブラウザからアクセスして、設定したユーザとパス入力後、
コントロール画面が出てなんのエラーも無ければ ok!

最初自分は「eaccelerator.allowed_admin_path」の設定が抜けてたのでエラー多発
してうーーーんと悩んだ。
基本的に展開したソースに含まれる「eaccelerator.ini」の内容を参考にというかほぼまんま
記述すれば良い。
変更を加えるのは、
・zend_extension (アクセラレーターモジュールへのパス)
・eaccelerator.shm_size (キャッシュメモリの容量。うちは 32MB にした。)
・eaccelerator.log_file (ログが見てみたいだけで設定してみた。)
・eaccelerator.allowed_admin_path (control.php を使うので設定した)
この 4 つで ok。

参考 eaccelerator.ini からコメント省いたデフォルトな設定やつ。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[eaccelerator] 
zend_extension="/usr/lib/php4/eaccelerator.so" 
eaccelerator.shm_size = "0" 
eaccelerator.cache_dir = "/tmp/eaccelerator" 
eaccelerator.enable = "1" 
eaccelerator.optimizer = "1" 
eaccelerator.debug = 0 
eaccelerator.log_file = "/var/log/httpd/eaccelerator_log" 
eaccelerator.name_space = "" 
eaccelerator.check_mtime = "1" 
eaccelerator.filter = "" 
eaccelerator.shm_max = "0" 
eaccelerator.shm_ttl = "0" 
eaccelerator.shm_prune_period = "0" 
eaccelerator.shm_only = "0" 
eaccelerator.compress = "1" 
eaccelerator.compress_level = "9" 
eaccelerator.keys     = "shm_and_disk" 
eaccelerator.sessions = "shm_and_disk" 
eaccelerator.content  = "shm_and_disk" 
eaccelerator.allowed_admin_path = ""

そんで効果の程を見る。
control.php からキャッシュを OFF にして ApacheBench で集中アクセスした結果。

Server Software:        Apache/2.0.59 
Server Hostname:        bucci.bp7.org 
Server Port:            80 
 
Document Path:          / 
Document Length:        32042 bytes 
 
Concurrency Level:      16 
Time taken for tests:   129.859922 seconds 
Complete requests:      100 
Failed requests:        1 
   (Connect: 0, Length: 1, Exceptions: 0) 
Write errors:           0 
Total transferred:      3355970 bytes 
HTML transferred:       3326078 bytes 
Requests per second:    0.77 [#/sec] (mean) 
Time per request:       20777.588 [ms] (mean) 
Time per request:       1298.599 [ms] (mean, across all concurrent requests) 
Transfer rate:          25.23 [Kbytes/sec] received 
 
Connection Times (ms) 
              min  mean[+/-sd] median   max 
Connect:        0   20  54.2      0     201 
Processing:  1060 18316 14499.1  17409   52110 
Waiting:      693 8583 7525.0   6390   28989 
Total:       1060 18337 14504.9  17409   52110 
 
Percentage of the requests served within a certain time (ms) 
  50%  17409 
  66%  25505 
  75%  31046 
  80%  34619 
  90%  38532 
  95%  42864 
  98%  45833 
  99%  52110 
 100%  52110 (longest request)

次にキャッシュ ON モード

Server Software:        Apache/2.0.59 
Server Hostname:        bucci.bp7.org 
Server Port:            80 
 
Document Path:          / 
Document Length:        32042 bytes 
 
Concurrency Level:      16 
Time taken for tests:   78.882311 seconds 
Complete requests:      100 
Failed requests:        0 
Write errors:           0 
Total transferred:      3558342 bytes 
HTML transferred:       3526487 bytes 
Requests per second:    1.27 [#/sec] (mean) 
Time per request:       12621.169 [ms] (mean) 
Time per request:       788.823 [ms] (mean, across all concurrent requests) 
Transfer rate:          44.04 [Kbytes/sec] received 
 
Connection Times (ms) 
              min  mean[+/-sd] median   max 
Connect:        0    9  23.7      0      81 
Processing:   677 11397 5153.4  10183   32789 
Waiting:      316 1429 2636.9    348   20063 
Total:        677 11407 5161.0  10183   32869 
 
Percentage of the requests served within a certain time (ms) 
  50%  10183 
  66%  13159 
  75%  14577 
  80%  15350 
  90%  17961 
  95%  19860 
  98%  24094 
  99%  32869 
 100%  32869 (longest request)

「Requests per second」を比較すると約 1.6 倍の向上となっている。
その他の項目を見ても大体そんな感じ。
ただ、注目したところは前者だとエラーでリクエスト 1 個とりこぼしてるけど
後者のキャッシュ ON だと全て仕事をこなせているってな所。
ちなみにこの記事を投稿する画面は、表示するまでの速度が 3 倍も速くなっている。

ってのもアレ、WordPress ってのはページの表示にはダイナミックパブリッシングって物を
しているので「ブラウザ→ PHP → MySQL → PHP → 表示」という流れになっている。
MySQL へのアクセスが主体となるような作りだから劇的な速度向上には繋がらない
という結果となった。

完全 PHP な「半死半生の某コンテンツ」に同様のテストを行ったところ、
ON : Requests per second: 3.85 [#/sec] (mean)
OFF : Requests per second: 2.01 [#/sec] (mean)
ということで、約 1.9 倍の速度 UP となりますた。

既にこの eAccelerator の PHP モジュールは稼働しているので、ブログ閲覧も
体感できる速度 UP している状態。
めでたしめでたし。

最後に書くのもアレなんだけど、高速化というのは PHP が実行時にコードをコンパイルしながら
データを出力していく物なんだけど、この時生成される中間コンパイルコードをキャッシュする事で
次回呼びだしはコンパイルせず、中間コンパイルコードから呼び出すことで高速化するという物でふ。
キャッシュはオンメモリで行われると同時にディスク上にも保存される。
その為、うちの環境ではスワップアウトかまされてます orz

WordPress 以外にも PHP で記述された大規模な CGI でも効果大だとおもわれ。
OpenPNE とか XOOPS とか phpBB とかね。

Related posts:

  1. CentOS 4 : CapsLock と Control キーを入れ替える。

 返信する


*

以下のHTML タグと属性が利用できます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <img localsrc="" alt=""> <pre lang="" line="" escaped="" highlight="">

   
59 queries. 0.318 seconds.