「CentOS7」+「php7.2」+「nginx」 な環境に構築された「wordpress」を「APCu」と「OPcache」を導入した高速化させた際のメモです。
https://www.server-memo.net/memo/wordpress/nginx-install.html
「OPcache」と「APCu」について
「OPcache」と「APCu」はPHPアクセラレータと呼ばれ、PHPを高速化させるため拡張モジュールとなります。
OPcache (コードキャッシュ)
PHPのスクリプトは呼び出されるたびに、「スクリプトの読込」「コンパイル」「中間コード作成」「実行」という流れとなっていますが、「OPcache」は「コンパイル」して生成された「中間コード」をキャッシュしておくことで、「コンパイル」「中間コード作成」を省略し高速化してくれます。
APCu (データキャッシュ)
PHPが動的に作成したデータをキャッシュすることで、ページの表示を早くしてくれます。
インストール
本環境のPHPは「remi」リポジトリでインストールされているので、「OPcache」と「APCu」のインストールには
「remi」リポジトリを使用して下記のパッケージをインストールします。
- OPcache php-opcache
- APCu … php-pecl-apcu
PHPのバージョンによって使用するリポジトリが異なるため、事前に「php -v」等でバージョンを確認しておいてください。
$ php -v PHP 7.2.11 (cli) (built: Oct 10 2018 10:00:29) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
今回はphp7.2でしたので「remi-php72」を使用しますが、環境にあわせてPHP7.1であれば「remi-php71」といったように適宜読み替えて作業を行ってください。
# yum install --enablerepo=remi,remi-php72 php-opcache php-pecl-apcu
インストールの確認
「OPache」については「php -v」でPHPのバージョンを表示させると、「with Zend OPcache v7.2.11, Copyright (c) 1999-2018, by Zend Technologies」という表記が追記されていることが確認できます。
# php -v PHP 7.2.11 (cli) (built: Oct 10 2018 10:00:29) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.11, Copyright (c) 1999-2018, by Zend Technologies
「APCu」については「php -i」コマンドで確認することができるので、表示結果を「|」(パイプ)で「grep」や「egrep」等に渡して表示結果を抜き出すと分かりやすいでしょう。
下記の実行例では「apc」と「opcache」を「egrep」で抜き出して表示させています。
# php -i | egrep 'apc|opcache' Additional .ini files parsed => /etc/php.d/10-opcache.ini, /etc/php.d/40-apcu.ini, apcu MMAP File Mask => /tmp/apc.XXXXXX apc.coredump_unmap => Off => Off apc.enable_cli => Off => Off apc.enabled => On => On apc.entries_hint => 4096 => 4096 apc.gc_ttl => 3600 => 3600 apc.mmap_file_mask => /tmp/apc.XXXXXX => /tmp/apc.XXXXXX apc.preload_path => no value => no value apc.serializer => php => php apc.shm_segments => 1 => 1 apc.shm_size => 32M => 32M apc.slam_defense => On => On apc.smart => 0 => 0 apc.ttl => 0 => 0 apc.use_request_time => On => On apc.writable => /tmp => /tmp opcache.blacklist_filename => /etc/php.d/opcache*.blacklist => /etc/php.d/opcache*.blacklist opcache.consistency_checks => 0 => 0 opcache.dups_fix => Off => Off opcache.enable => On => On opcache.enable_cli => Off => Off opcache.enable_file_override => Off => Off opcache.error_log => no value => no value opcache.file_cache => no value => no value opcache.file_cache_consistency_checks => 1 => 1 opcache.file_cache_only => 0 => 0 opcache.file_update_protection => 2 => 2 opcache.force_restart_timeout => 180 => 180 opcache.huge_code_pages => On => On opcache.inherited_hack => On => On opcache.interned_strings_buffer => 8 => 8 opcache.lockfile_path => /tmp => /tmp opcache.log_verbosity_level => 1 => 1 opcache.max_accelerated_files => 4000 => 4000 opcache.max_file_size => 0 => 0 opcache.max_wasted_percentage => 5 => 5 opcache.memory_consumption => 128 => 128 opcache.opt_debug_level => 0 => 0 opcache.optimization_level => 0x7FFFBFFF => 0x7FFFBFFF opcache.preferred_memory_model => no value => no value opcache.protect_memory => 0 => 0 opcache.restrict_api => no value => no value opcache.revalidate_freq => 2 => 2 opcache.revalidate_path => Off => Off opcache.save_comments => 1 => 1 opcache.use_cwd => On => On opcache.validate_permission => Off => Off opcache.validate_root => Off => Off opcache.validate_timestamps => On => On
設定ファイルのインストール場所
インストールされた「APCu」と「OPcache」の設定ファイルは「php -i」コマンドの表示結果からもわかるように、「/etc/php.d」ディレクトリ内に作成された「40-apcu.ini」「10-opcache.ini」というファイルになります。
Additional .ini files parsed => /etc/php.d/10-opcache.ini, /etc/php.d/40-apcu.ini,
※設定ファイルについては、インストールするバージョンによって数字の部分などが異なる場合があります。
有効化
インストールが完了しましたら、「php-fpm」と「nginx」を再起動してください。
# systemctl restart php-fpm # systemctl restart nginx
これで「wordpress」が高速化されているので効果を確認してみてください。
効果確認
実際にどのくらいの効果があったのか「ab」コマンドを使用して、「APCu」と「OPcahe」の導入前と導入後を下記の2項目で比較してみました。
- Requests per second
- Time per request (mean, across all concurrent requests)
「Requests per second」は1秒間に処理できるリクエストの数で、「Time per request (mean, across all concurrent requests)」は1リクエストの処理に掛かった時間となります。
インストールした直後の状態の「wordpress」(テーマは「Twenty Seventeen」)で、10同時アクセスで合計100リクエストといった内容で計測を行いました。
導入前
「APCu」と「OPcache」の導入前に「ab」コマンドで計測しておいた結果です。
# ab -n 100 -c 10 https://conoha.server-memo.net/ This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking conoha.server-memo.net (be patient).....done Server Software: nginx/1.15.5 Server Hostname: conoha.server-memo.net Server Port: 443 SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128 Document Path: / Document Length: 53629 bytes Concurrency Level: 10 Time taken for tests: 8.985 seconds Complete requests: 100 Failed requests: 0 Write errors: 0 Total transferred: 5386700 bytes HTML transferred: 5362900 bytes Requests per second: 11.13 [#/sec] (mean) Time per request: 898.465 [ms] (mean) Time per request: 89.847 [ms] (mean, across all concurrent requests) Transfer rate: 585.49 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 2 7 15.0 2 74 Processing: 458 759 101.6 752 1345 Waiting: 458 759 101.3 752 1340 Total: 460 767 107.8 754 1351 Percentage of the requests served within a certain time (ms) 50% 754 66% 777 75% 794 80% 814 90% 902 95% 965 98% 1014 99% 1351 100% 1351 (longest request)
導入後
導入後の結果は下記のようになりました。
キャッシュを作成するために、計測前に一度サイトにwebブラウザでアクセスしてから計測しています。
# ab -n 100 -c 10 https://conoha.server-memo.net/ This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking conoha.server-memo.net (be patient).....done Server Software: nginx/1.15.5 Server Hostname: conoha.server-memo.net Server Port: 443 SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128 Document Path: / Document Length: 53629 bytes Concurrency Level: 10 Time taken for tests: 1.933 seconds Complete requests: 100 Failed requests: 0 Write errors: 0 Total transferred: 5386700 bytes HTML transferred: 5362900 bytes Requests per second: 51.74 [#/sec] (mean) Time per request: 193.273 [ms] (mean) Time per request: 19.327 [ms] (mean, across all concurrent requests) Transfer rate: 2721.78 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 2 5 4.5 4 23 Processing: 90 152 37.0 152 354 Waiting: 90 152 37.0 150 353 Total: 95 158 37.8 156 360 Percentage of the requests served within a certain time (ms) 50% 156 66% 172 75% 178 80% 181 90% 189 95% 232 98% 259 99% 360 100% 360 (longest request)
比較結果
「Requests per second」と「Time per request」の値を比較していみると、約4.6倍高速化されたという結果になりました。
「APCu」と「OPcache」の導入自体は非常に簡単なのに、高速化の効果は大きいのでおすすめですよ。
導入前
Requests per second: 11.13 [#/sec] (mean) Time per request: 89.847 [ms] (mean, across all concurrent requests)
導入後
Requests per second: 51.74 [#/sec] (mean) Time per request: 19.327 [ms] (mean, across all concurrent requests)
コメント