APCでPHPを高速化する

はじめに

wordpressがちょっと重いかなって思ったので、お手軽なAPC(Alternative PHP Cache)を導入してみることにした。これは、その時のメモ。

手順

  1. まず、ApacheBenchを使って、APC導入前のベンチマークを測定する。
  2. 次に、APCをさくっとインストール
  3. 最後に、ApacheBenchでベンチマークを再確認。

APCをインストールする

今回はPECLを使ってAPCを導入することにした。PECLはPHPの拡張ライブラリのひとつで、C言語で書かれているのでPEARよりも処理速度が速いらしい。

では、サクっとインストールする。

[root@centos ~]# pecl install APC


downloading APC-3.1.6.tgz ...
Starting to download APC-3.1.6.tgz (148,835 bytes)
.................................done: 148,835 bytes
49 source files, building
running: phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519
Enable per request file info about files used from the APC cache [no] : 
/* そのままEnter */ 

Enable spin locks (EXPERIMENTAL) [no] : 
/* そのままEnter */

Build process completed successfully
Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20060613/apc.so'
install ok: channel://pecl.php.net/APC-3.1.6
configuration option "php_ini" is not set to php.ini location
You should add "extension=apc.so" to php.ini

これでインストール終了。

php.iniの編集

ご丁寧に、何をすべきか書いてくれてる。とりあえず指示されるがままにphp.iniを編集する。

[root@centos ~]# vim /usr/local/lib/php.ini
まず、extension_dirを次のように修正する。
extension_dir = "/usr/local/lib/php/extensions/"

次に、以下を追記
extention=apc.so

これでphp.iniの編集は終わり。

シンボリックリンクの作成

今のままだと、apc.soが
Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20060613/apc.so'
にあることになってるので、/usr/local/lib/php/extensions/にパスを通す。

[root@centos ~]# ln -s /usr/local/lib/php/extensions/no-debug-non-zts-20060613/apc.so /usr/local/lib/php/extensions/

とりあえずこれでOK。

apacheを再起動し、APCが導入できたことを確認する。

[root@centos ~]# /usr/local/apache2/bin/apachectl restart   //再起動

[root@centos ~]# php -i | grep apc     //文字列"apc"を含むPHP情報を表示
apc
apc.cache_by_default => On => On
apc.canonicalize => On => On
apc.coredump_unmap => Off => Off
apc.enable_cli => Off => Off
apc.enabled => On => On
apc.file_md5 => Off => Off
apc.file_update_protection => 2 => 2
apc.filters => no value => no value
apc.gc_ttl => 3600 => 3600
apc.include_once_override => Off => Off
apc.lazy_classes => Off => Off
apc.lazy_functions => Off => Off
apc.max_file_size => 1M => 1M
apc.mmap_file_mask => no value => no value
apc.num_files_hint => 1000 => 1000
apc.preload_path => no value => no value
apc.report_autofilter => Off => Off
apc.rfc1867 => Off => Off
apc.rfc1867_freq => 0 => 0
apc.rfc1867_name => APC_UPLOAD_PROGRESS => APC_UPLOAD_PROGRESS
apc.rfc1867_prefix => upload_ => upload_
apc.rfc1867_ttl => 3600 => 3600
apc.shm_segments => 1 => 1
apc.shm_size => 32M => 32M
apc.slam_defense => On => On
apc.stat => On => On
apc.stat_ctime => Off => Off
apc.ttl => 0 => 0
apc.use_request_time => On => On
apc.user_entries_hint => 4096 => 4096
apc.user_ttl => 0 => 0
apc.write_lock => On => On 

無事にインストールで来ている模様。ちなみに、今回はコマンドラインから確認したが、適当なファイルを作ってphpinfo();で吐き出してもOK。その際はファイルの削除をお忘れなく。

APCのベンチマーク確認

APC導入前のベンチマーク結果

[root@centos ~]# /usr/local/apache2/bin/ab -c 100 -n 100 http://wiki.r-tgw.net/
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/ 

Benchmarking wiki.r-tgw.net (be patient).....done


Server Software:        Apache/2.2.15
Server Hostname:        wiki.r-tgw.net
Server Port:            80

Document Path:          /
Document Length:        15269 bytes

Concurrency Level:      100
Time taken for tests:   36.604123 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      1562000 bytes
HTML transferred:       1526900 bytes
Requests per second:    2.73 [#/sec] (mean)
Time per request:       36604.122 [ms] (mean)
Time per request:       366.041 [ms] (mean, across all concurrent requests)
Transfer rate:          41.66 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       10   40  14.6     47      56
Processing:  1994 18731 9373.2  18832   33419
Waiting:     1587 18447 9352.3  18296   32866
Total:       2046 18772 9363.0  18878   33439

Percentage of the requests served within a certain time (ms)
  50%  18878
  66%  24431
  75%  28133
  80%  28942
  90%  30742
  95%  32629
  98%  33222
  99%  33439
 100%  33439 (longest request)

APC導入後のベンチマーク結果

[root@centos ~]# /usr/local/apache2/bin/ab -c 100 -n 100 http://wiki.r-tgw.net/
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking wiki.r-tgw.net (be patient).....done


Server Software:        Apache/2.2.15
Server Hostname:        wiki.r-tgw.net
Server Port:            80

Document Path:          /
Document Length:        15269 bytes

Concurrency Level:      100
Time taken for tests:   19.6463 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      1566344 bytes
HTML transferred:       1530893 bytes
Requests per second:    5.26 [#/sec] (mean)
Time per request:       19006.462 [ms] (mean)
Time per request:       190.065 [ms] (mean, across all concurrent requests)
Transfer rate:          80.45 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       12   24   9.5     22      44
Processing:  1367 10080 4775.1  11187   17987
Waiting:     1332 9888 4774.0  10974   17921
Total:       1388 10105 4778.7  11207   18030

Percentage of the requests served within a certain time (ms)
  50%  11207
  66%  13763
  75%  14307
  80%  14512
  90%  15450
  95%  16632
  98%  17362
  99%  18030
 100%  18030 (longest request) 

まとめ

ベンチマークの結果、以下のようになった。

項目導入前導入後
Time taken for tests36.604123 seconds19.6463 seconds
Time per request36604.122 [ms] (mean)19006.462 [ms] (mean)

というわけで、ほぼ倍速になりましたよ、というお話。


Tag: APC PHP cache 高速化


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2011-05-30 (月) 02:23:36 (357d)