開発したアプリなど一覧

WordPress を APC + WP Super Cache で簡単に高速化してみる。

アフィリエイトリンクを含む場合があります

WordPress 素の状態ではページを開くたびにデータベースにアクセスしにいったりPHPスクリプトが動いたりしてるのでわりと遅いです。(といっても俺のブログぐらいのアクセス数だと全く気になりませんが。。。)

かといってサーバを複数台用意するようなお金のかかる事はしたくないし、mod_proxy + mod_cache のような機構は試した事が無いので今回は無し。これは今実験中だけど。

というわけで今回は簡単にできる APC の導入、それから WordPress のプラグインである、WP Super Cache を使用する事にした。

計測には ApacheBench を使用しました。

今回 ab コマンドで発行するコマンドは以下の通り。

$ ab -n 1000 -c 10 -A hoge:password http://example.com/index.php

簡単に言うと、同時アクセス数 10 で合計 1000 のアクセスを http://example.com/index.php に対して行う。BASIC認証が必要なのでそれも指定。

まず何もなしの状態

$ ab -n 1000 -c 10 -A hoge:password http://example.com/index.php

Concurrency Level: 10 Time taken for tests: 181.571127 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Non-2xx responses: 1000 Total transferred: 317000 bytes HTML transferred: 0 bytes Requests per second: 5.51 [#/sec] (mean) Time per request: 1815.711 [ms] (mean) Time per request: 181.571 [ms] (mean, across all concurrent requests) Transfer rate: 1.70 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 12 12 1.2 12 32 Processing: 952 1800 365.5 1712 3619 Waiting: 952 1799 365.4 1712 3618 Total: 964 1812 365.5 1724 3631

Percentage of the requests served within a certain time (ms) 50% 1724 66% 1880 75% 2000 80% 2077 90% 2250 95% 2440 98% 2908 99% 3172 100% 3631 (longest request)

秒間5回、全部終わるのに3分ぐらいかかってます。さすがに遅いですね。そりゃ毎回DBアクセスしたりしてるもんね。

というわけで APC を入れてみる。

php-apc 導入

PHP APC をいれてみます。Alternative PHP Cache です。PHP の中間コードのキャッシュ・最適化を行うためのものらしいです。もう Cache と書いてあるだけで早くなりそうです。さっそく入れてみましょう。

$ sudo apt-get install php-apc $ sudo vim /etc/php/apc.inc extension=apc.so

$ sudo apache restart

ubuntu はインストールが楽でいいですね。いや php-apc ぐらい yum でもあるだろうけど。

Apache 再起動したら phpinfo で APC をちゃんと読んでいるか確認。確認できたら再度 ab を行います。

$ ab -n 1000 -c 10 -A hoge:password http://example.com/index.php

Concurrency Level: 10 Time taken for tests: 101.128370 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Non-2xx responses: 1000 Total transferred: 317000 bytes HTML transferred: 0 bytes Requests per second: 9.89 [#/sec] (mean) Time per request: 1011.284 [ms] (mean) Time per request: 101.128 [ms] (mean, across all concurrent requests) Transfer rate: 3.06 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 12 12 1.3 12 33 Processing: 588 997 235.2 950 2306 Waiting: 588 996 235.1 948 2305 Total: 600 1009 235.2 962 2318

Percentage of the requests served within a certain time (ms) 50% 962 66% 1039 75% 1099 80% 1141 90% 1315 95% 1420 98% 1584 99% 2120 100% 2318 (longest request)

倍近く早くなりました。元が遅かったのもありますがわりと効果ありますね。

WP Super Cache

APCだけでは不十分。というか毎回データベースアクセスするとか無駄ですよね、Wordpress がキャッシュはけばもっと早くなるはずです。

というわけで WordPress でキャッシュといえば WP Super Cache、これをインストールします。

WordPress は管理画面上でプラグインをインストールできる機能がありますが、自分のサーバは FTP も FTPS も使えないのでサーバ上で直接 wget , unzip してます。だれですか FTP 使えなくしたの? 俺ですね、はい。

$ cd ~/example.com/wp/wp-content/plugins/ $ wget http://downloads.wordpress.org/plugin/wp-super-cache.0.9.9.9.zip $ unzip wp-super-cache.0.9.9.9.zip

unzip したら wordpress の管理画面に入ってプラグインを有効にします。
設定は
「キャッシュファイルの提供に PHP を利用する。」
にチェックを入れただけで他は初期値です

設定してトップページのソースコードの一番下を見てキャッシュが生成されているのを確認したらまた ab コマンドを打ちます。

$ ab -n 1000 -c 10 -A hoge:password http://example.com/

Concurrency Level: 10 Time taken for tests: 9.993960 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 41006000 bytes HTML transferred: 40701000 bytes Requests per second: 100.06 [#/sec] (mean) Time per request: 99.940 [ms] (mean) Time per request: 9.994 [ms] (mean, across all concurrent requests) Transfer rate: 4006.82 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 12 17 12.9 13 139 Processing: 55 81 29.5 72 315 Waiting: 15 32 19.3 26 164 Total: 68 99 33.4 89 339 Percentage of the requests served within a certain time (ms) 50% 89 66% 98 75% 105 80% 112 90% 142 95% 164 98% 206 99% 233 100% 339 (longest request)

さすがに速い。ab の引数変えたら Requests per second なんかはもっと早くなりましたが他と実行環境そろえないと比較にならないので。

このぐらい速ければある程度アクセスが集中してもきっとどうにかなるでしょう。

エラーが出る?

APC と WP Super Cache を両方いれてしばらくしたらエラーが出たので、ちょっと修正した。
APC と WP Super Cache が競合する?

Sponsored Link

コメント

タイトルとURLをコピーしました