MariaDBのインストール
MySQLではなくMariaDBを使います。MariaDBはMySQL作者によるMySQL互換DBです。例によってOracleと上手くいかなかったようでMySQLをフォークして開発がスタートしました。最近ではMySQL側にもフィードバックしているみたいです。MariaDBの方がなにかと優秀みたいなのでMariaDBを私は常用しています(結局MariaDB特有の機能は使ってないのですが)。
ちなみに、MariaもMyも作者の娘さんだそうです。下記3枚目のスライドに写真が載ってます。
この写真をみると結構年離れた姉妹ですよね。何かあったのかな…と想像していまいます。
とにかくインストールします。下記ページから環境に合ったリポジトリを設定してaptからMariaDBをインストールします。
Setting up MariaDB Repositories
バージョンを選択していくと打つべきコマンドが表示されるので、それを打てばインストール完了です。
関連パッケージのインストール
その他のパッケージをインストールします。
$ sudo apt-get install nginx php-fpm php-mysql php-mysql php-xmlrpc php-intl php-xsl unzip
php-xmlrpc、php-intl、php-xslはWordpressのXML RPCを動かすのに必要です(Jetpackなどで使います)。これが入ってないと、Jetpackの連携時に「parse error. not well formed」とか「The Jetpack server could not communicate with your site's XML-RPC URL」などと出てきます。
unzipもバックアップ系ライブラリで使用されたりしますが、これも入ってないとエラーメッセージすら出ないので分かりにくいです。Wordpressが悪いのかphpが悪いのか、必要なモジュールがインストールされていない場合のエラーメッセージが不親切ですね。
Nginxの設定
$ sudo vi /etc/nginx/nginx.conf
FastCGIのキャッシュ設定を追加。キャッシュパスは任意。
http { # キャッシュ設定を追加 fastcgi_cache_path /var/cache/nginx/wordpress levels=1:2 keys_zone=wpcache:30m max_size=512M inactive=600m; # .... }
FastCGI用のディレクトリを作成。これをやらないとnginx起動時にディレクトリが作れないとエラーになる。
$ sudo mkdir /var/cache/nginx
続いてサイトの設定。
$ sudo vi /etc/nginx/sites-available/default
日本語でコメント書いてあるところを設定していってください。
server{ # .... # Add index.php to the list if you are using PHP index index.html index.htm index.php index.nginx-debian.html; # ←注釈にある通り index.phpを追加 # ... location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # ↓Wordpressのパーマリンクを使うためにindex.phpのargumentに変換してあげる try_files $uri $uri/ /index.php?q=$uri&$args =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # With php7.0-fpm: ↓コメントを外す fastcgi_pass unix:/run/php/php7.0-fpm.sock; # ↓ここからずーっとキャッシュ除外設定。POSTや管理系のphpに関してはキャッシュしない。 set $do_not_cache 0; if ($request_method = POST) { set $do_not_cache 1; } if ($query_string != "") { set $do_not_cache 1; } if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $do_not_cache 1; } if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") { set $do_not_cache 1; } fastcgi_cache wpcache; fastcgi_cache_key "$request_method:$scheme://$host$request_uri"; fastcgi_cache_valid 200 60m; fastcgi_no_cache $do_not_cache; fastcgi_cache_bypass $do_not_cache; add_header X-F-Cache $upstream_cache_status; # ↓後述するNginx Cache Controllerのために必要 fastcgi_pass_header "X-Accel-Redirect"; fastcgi_pass_header "X-Accel-Expires"; } }
MariaDB ユーザ作成
MariaDBにユーザとスキーマを作ります。
$ mysql -u root create database <データベース名> create user <ユーザ名> grant all privileges on <データベース名>.* to <ユーザ名>@'locahost' identified by '<パスワード>'
WordPressのインストール
WordPressの最新バージョンを落としてきて、nginxで設定した公開ディレクトリ(デフォルトは/var/www/html/)に展開します。nginxはwww-data権限で動くのでwww-dataにオーナーを設定しとくのを忘れずに。
$ cd /var/www/html $ sudo wget https://ja.wordpress.org/wordpress-4.7-ja.zip $ sudo unzip wordpress-4.7-ja.zip // ←wordpressディレクトリが出来る $ sudo chown -R www-data:www-data wordpress
あとはブラウザからhttp://localhost/wordpressにアクセスすれば設定画面が出ると思います。設定画面が出たら、基本情報と先ほど設定したMySQLユーザを設定します。
Nginx Cache Controllerのインストール
サイト構築を進めていくとキャッシュを消したいこともたびたびあるでしょうから、キャッシュを消すためのプラグインを入れておきます。Nginx Cache Controllerというやつです。Wordpressの管理画面からインストールして、設定したキャッシュパス(本記事では/var/cache/nginx/wordpress)を入力しておきます。
パフォーマンス測定
$ ab -n 1000 -c 40 http://localhost/wordpress/ Concurrency Level: 40 Time taken for tests: 0.328 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 17037160 bytes HTML transferred: 16809000 bytes Requests per second: 3048.26 [#/sec] (mean) Time per request: 13.122 [ms] (mean) Time per request: 0.328 [ms] (mean, across all concurrent requests) Transfer rate: 50716.50 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.2 0 2 Processing: 0 8 41.0 0 326 Waiting: 0 8 41.0 0 326 Total: 0 8 41.1 0 326 Percentage of the requests served within a certain time (ms) 50% 0 66% 0 75% 1 80% 1 90% 1 95% 3 98% 201 99% 270 100% 326 (longest request)
ローカルで測定して秒間3048アクセスです。同じ設定をしてさくらVPS 2G HDDプランに展開したこのブログにリモートからテストして秒間250アクセスくらいでした。
よほど大きなサイトを運営するので無い限りこのくらいのパフォーマンスが出れば十分でしょう。
参考サイト
「NginxのFastCGIキャッシュ」を導入して「1127倍」まで高速化する──「WordPress“1000倍”高速化」チューニング総仕上げ