CentOSにAPCをインストールする方法

APCはPHPの中間コードのキャッシュや最適化を行う拡張モジュールです。基本的にPHPを使うサーバーに全て入れてよいようです。
本日とあるサーバーにインストールしたのでやり方をメモ。
環境
まずは環境ですが、CentOS 4.4 + PHP 5.1.6 です。
事前準備
まず、peclが入っているかどうか調べます。
# pecl list-all apc All packages: ============= Package Latest Local pecl/KTaglib 0.2.0 Library to edit audio properties and tags on MPEG and OGG files pecl/FliteTTS Text to speech voice synthesis for PHP pecl/Ovrimos Ovrimos interface pecl/DTrace 1.0.3 A Solaris Dtrace provider pecl/inclued 0.1.0 Clued-in about your inclueds pecl/zookeeper 0.1.0 PHP extension for interfacing with Apache ZooKeeper … … … …
インストールされているパッケージが表示されれば、このSTEPは読み飛ばしてOKです。
なければインストールます。
# yum install -y php-pear Setting up Install Process Setting up repositories update 100% |=========================| 951 B 00:00 base 100% |=========================| 1.1 kB 00:00 … … …
phpizeもインストールします。
# yum install -y php-devel
apxsも無ければインストール
# yum install -y httpd-devel
これでやっとpeclがインストールできます。
インストール
peclコマンドでインストールできます。
# pecl install APC
無事にインストールが完了すると以下のようなメッセージがでます。
Build process completed successfully Installing '/var/tmp/pear-build-root/install-APC-3.0.19//usr/lib/php/modules/apc.so' install ok: channel://pecl.php.net/APC-3.0.19 You should add "extension=apc.so" to php.ini
ふむふむ。php.iniに追加しろって事ですね。
って事でphp.iniに以下の行を追加。場所はどこでもかまいません。
extension=apc.so
iniファイルを編集したらapacheを再起動します。
これでとりあえず作業は完了です。
phpinfoの画面でapcの項目があるか確認します。

アップグレード
apcのアップグレードは以下のコマンドで。
# pecl upgrade apc
ベンチマーク
ApacheBenchでどのくらい効果が変わったか測定します。
1000件のリクエストを100の同時接続で計測した結果です。
# ab -n 1000 -c 100 http://127.0.0.1/*****/test.php
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 1000 requests
Server Software: Apache/2.0.63
Server Hostname: 127.0.0.1
Document Path: /*****/test.php
Document Length: 1257 bytes
Concurrency Level: 100
Time taken for tests: 13.193656 seconds
Complete requests: 1000
Failed requests: 557
(Connect: 0, Length: 557, Exceptions: 0)
Write errors: 0
Total transferred: 1897728 bytes
HTML transferred: 1504335 bytes
Requests per second: 75.79 [#/sec] (mean)
Time per request: 1319.366 [ms] (mean)
Time per request: 13.194 [ms] (mean, across all concurrent requests)
Transfer rate: 140.45 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 5 17.9 0 80
Processing: 74 923 879.0 498 5931
Waiting: 56 919 880.2 497 5930
Total: 102 929 877.3 502 5931
Percentage of the requests served within a certain time (ms)
50% 502
66% 974
75% 1209
80% 1371
90% 2181
95% 2667
98% 3048
99% 4531
100% 5931 (longest request)
トータルで13秒でした。
ふーむ。早くなってるんですかね。念のためapcを無効にして再度ベンチを図ってみました。
# ab -n 1000 -c 100 http://127.0.0.1/*****/test.php
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 1000 requests
Server Software: Apache/2.0.63
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /*****/test.php
Document Length: 1257 bytes
Concurrency Level: 100
Time taken for tests: 32.801446 seconds
Complete requests: 1000
Failed requests: 3
(Connect: 0, Length: 3, Exceptions: 0)
Write errors: 0
Total transferred: 1651341 bytes
HTML transferred: 1258341 bytes
Requests per second: 30.49 [#/sec] (mean)
Time per request: 3280.145 [ms] (mean)
Time per request: 32.801 [ms] (mean, across all concurrent requests)
Transfer rate: 49.14 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 62 171.8 0 599
Processing: 288 2895 1431.8 2741 22702
Waiting: 8 2886 1441.0 2740 22701
Total: 616 2958 1410.4 2760 23294
Percentage of the requests served within a certain time (ms)
50% 2760
66% 2896
75% 2959
80% 2987
90% 3437
95% 4084
98% 6956
99% 9495
100% 23294 (longest request)
トータルで32秒でした。おお!19秒も違いますね。
まさか、倍以上違うとは、、、
まだ入れてない方はぜひ。
参考
ちなみに高速化モジュールは他にも以下のようなものがあります。
- eAccelerator
- PHPA(PHP Accelerator)
- XCache
今回はyumから入れたいという理由でAPCを採用しました。
導入にあたっては以下の記事を参考にさせていただきました。ありがとうございます。
APC(Alternative PHP Cache)再び – Do You PHP?
PHP(というかWordPress)高速化のためにAPC(Alternatice PHP Cache)入れた – IDEA*IDEA ~ 百式管理人のライフハックブログ
最新情報をお届けします
