高速Webサーバー構築!さくらVPS nginxでのWordPress設定方法とレンサバ3社の読込速度を比較

2013/04/09 21:53

3/14に新しくレンタルサーバを借りまして、Google BloggerからWordPressに移行しました!そこから順調に進める予定だったのですが、いろいろと不慮の事故があり、一時公開停止しておりました、申し訳ありません。

そこで諦めるのも「今後のためにならない」と思い、今回「さくらインターネットのVPS SSD メモリ2G」を使って、サックサク高速サーバー構築にチャレンジしてみました。

あらすじ

あの頃はまだ雪も残り、寒い時期だった・・・そうだな、何から話せばいいのか・・・うーん、まぁ聞いて欲しい。

最初に借りたレンタル先の「みんなのドメイン レンタルサーバ」の転送速度が今ひとつ納得できなかったため、「さくらインターネット スタンダード」で無料お試しを行なっていました。

前者に比べ速くリーズナブルな価格だったのですが、レンタルが切れる前に終了のお知らせメールが来たため、とりあえず他も試してみたいしキャンセルしておこうと思ったのが悲劇の始まりでした。

キャンセルして数時間・・・

(つд⊂)ゴシゴシ (;゚Д゚)データ、滅殺されてるッ!

そうです(涙)

たとえ期限が残っていたとしても、キャンセルしたらすぐ登録解除→αβοοη…_φ(゚∀゚ )アヒャ

一所懸命つくったテンプレート、プラグインの選別や記事・Repositoryなど、バックアップを取る間もありませんでした。
それから3日ほど寝込みまして、三途の川が見えかけたところで現実に帰ってきました。

ただいま"o(〃・ω・〃)o"ってことで

いろいろ試してみましたので、もしレンサバ借りて「サイトやブログを立ちあげたいな」と思ってる方の参考になれば幸いです。

さくらVPS nginxを使ったWordPress構築 – DNS逆引きレコード変更

みんなのドメイン」を使った方法を説明します。はじめに管理パネルを開き、使用するドメインの「ドメイン詳細」→「ネームサーバー変更 」をクリック→「みんなのドメインのネームサーバー」にします。


次に、「DNSレコード編集」を開き、ホスト名「*」「@」「www」に、VPSのIPアドレス「***.***.***.***」を設定、レコードタイプを「A(Address)」と設定し更新。これで完了です。設定が反映されるまで時間が掛かるので、VPSのセットアップを行います。
およそ1時間ほどで反映されると思うので、さくらVPSのコントロールパネルから「ホスト名の変更」をクリックし、ホスト名のところに自分のドメインを入力。エラーなど出なければ、そのドメインでアクセスすることができるようになります。

さくらVPS nginxを使ったWordPress構築 – VPSのセットアップ

さくらのVPSでWordPressを動かそう – 初期設定・セキュリティ設定編 – – UKLab

こちらの記事が丁寧で分かりやすく解説されているので、ここを見ながらセットアップしていきます。

そのままではセットアップ出来なかったため、ポイントを付け加えていきますので、ブラウザを2つ起動して両方見ながら設定してみてください。なお、こちらの方法は、Webソケットに対応したデベロッパー版の「nginx」をソースコードからビルドしてインストールするものになっています。

nginxベースの高速なWordPress環境をお名前.comのVPSで構築 | SourceForge.JP Magazine

簡単な方法の「Stable version(安定版)」で構築する場合は、SourceForgeの記事を参考にしてみてください。

さくらのVPSでWordPressを動かそう – 初期設定・セキュリティ設定編 – ポイント

「vi」の編集画面では「Insert」もしくは「i」キーを押して編集できるようになります。終了する場合、「Esc」を押して「:wq」で上書き保存、「:q」もしくは「:q!(強制終了)」で保存せずに閉じることができます。

全選択は出来ないので、編集モードでは無い時に「ggvG」とキーを押すと、先頭から最終行まで選択できます。

検索は、編集モード以外で「/」キー「検索したい単語」で確認できます(「n」キーで次へ移動)

Poderosaでは、「Alt+c」でコピー 「Alt+v」で貼り付けになるので慣れが必要です。

「7.SSHでのログインを鍵認証でのログインへ変更&ポートを変更する」の項目は、ポート変更以外を最終的に行った方が融通がきく。
「12.root宛のメールを転送する」は、お試し期間の場合、テストメールが送られてこない。
「17.リポジトリの追加」の「epelリポジトリを追加」を実行すると、404 Not Foundとなるため、以下のURLで確認する。
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
「18.iptablesの設定」では最初日本語が文字化けするが、ログインし直すことで正常に表示される。
「19.denyhostsのインストール」では、※例外的に必ずアクセスを許可しておきたいIPアドレスがある場合の部分で自分が利用しているプロバイダのIPアドレスを「アクセス情報【使用中のIPアドレス確認】」で確認し、以下のコマンドで登録しておくといい。

vi /var/lib/denyhosts/allowed-hosts

さくらのVPSでWordPressを動かそう – nginx設定編 – ポイント

nginxをインストールする前に、サーバーとして動くのか確認と必要なパッケージのインストールの意味で、一般的なApache+PHP+MySQL環境による構成でVPS上で動作するか確認するとスムーズに導入出来ます。
nginxベースの高速なWordPress環境をお名前.comのVPSで構築 | 3ページ | SourceForge.JP Magazine

こちらの「WordPress環境に必要なパッケージをインストールする」を実行し、確認が取れたら次のステップに進みます。

「2.パッケージのファイルを揃える」で記述されているものが、Link切れとなっているため以下に変更。

rpm -ivh http://mirror.catn.com/pub/epel/6/SRPMS/nginx-1.0.15-2.el6.src.rpm
記事公開時の最新バージョン(開発者向け)が、v1.3.15なので以下のコマンドで取得。

wget http://nginx.org/download/nginx-1.3.15.tar.gz
「ngx_cache_purge」は、2.1なので、以下のようになる。
wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
「3.specファイルを編集する」では、1つずつ確認しながら行うのが面倒なので、以下を利用。
%define _unpackaged_files_terminate_build 0
%define ngx_cache_purge_version 2.1
%global nginx_user nginx
%global nginx_group %{nginx_user}
%global nginx_home %{_localstatedir}/lib/nginx
%global nginx_home_tmp %{nginx_home}/tmp
%global nginx_logdir %{_localstatedir}/log/nginx
%global nginx_confdir %{_sysconfdir}/nginx
%global nginx_datadir %{_datadir}/nginx
%global nginx_webroot %{nginx_datadir}/html

Name: nginx
Version: 1.3.15
Release: 2%{?dist}

Summary: A high performance web server and reverse proxy server
Group: System Environment/Daemons
# BSD License (two clause)
# http://www.freebsd.org/copyright/freebsd-license.html
License: BSD
URL: http://nginx.org/
Source0: http://nginx.org/download/nginx-%{version}.tar.gz
Source1: nginx.init
Source2: nginx.logrotate
Source3: nginx.conf
Source4: default.conf
Source5: ssl.conf
Source6: virtual.conf
Source7: nginx.sysconfig
Source10: ngx_cache_purge-%{ngx_cache_purge_version}.tar.gz
Source100: index.html
Source101: poweredby.png
Source102: nginx-logo.png
Source103: 404.html
Source104: 50x.html

# removes -Werror in upstream build scripts. -Werror conflicts with
# -D_FORTIFY_SOURCE=2 causing warnings to turn into errors.
#Patch0: nginx-auto-cc-gcc.patch
BuildRequires: GeoIP-devel
BuildRequires: gd-devel
BuildRequires: libxslt-devel
BuildRequires: openssl-devel
BuildRequires: pcre-devel
BuildRequires: perl-devel
BuildRequires: perl(ExtUtils::Embed)
BuildRequires: zlib-devel
Requires: GeoIP
Requires: gd
Requires: openssl
Requires: pcre
Requires: perl(:MODULE_COMPAT_%(eval "`%{__perl} -V:version`"; echo $version))
Requires(pre): shadow-utils
Requires(post): chkconfig
Requires(preun): chkconfig, initscripts
Requires(postun): initscripts
Provides: webserver

%description
Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
IMAP protocols, with a strong focus on high concurrency, performance and low
memory usage.


%prep
%setup -q
#%patch0 -p0
%setup -T -D -a 10
%build
# nginx does not utilize a standard configure script. It has its own
# and the standard configure options cause the nginx configure script
# to error out. This is is also the reason for the DESTDIR environment
# variable.
export LANG='ja_JP.UTF-8'
export DESTDIR=%{buildroot}
./configure
--prefix=%{nginx_datadir}
--sbin-path=%{_sbindir}/nginx
--conf-path=%{nginx_confdir}/nginx.conf
--error-log-path=%{nginx_logdir}/error.log
--http-log-path=%{nginx_logdir}/access.log
--http-client-body-temp-path=%{nginx_home_tmp}/client_body
--http-proxy-temp-path=%{nginx_home_tmp}/proxy
--http-fastcgi-temp-path=%{nginx_home_tmp}/fastcgi
--http-uwsgi-temp-path=%{nginx_home_tmp}/uwsgi
--http-scgi-temp-path=%{nginx_home_tmp}/scgi
--pid-path=%{_localstatedir}/run/nginx.pid
--lock-path=%{_localstatedir}/lock/subsys/nginx
--user=%{nginx_user}
--group=%{nginx_group}
--with-file-aio
--with-ipv6
--with-http_ssl_module
--with-http_realip_module
--with-http_addition_module
--with-http_xslt_module
--with-http_image_filter_module
--with-http_geoip_module
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_degradation_module
--with-http_stub_status_module
--with-http_perl_module
--with-mail
--with-mail_ssl_module
--with-cc-opt="%{optflags} $(pcre-config --cflags)"
--add-module=%{_builddir}/nginx-%{version}/ngx_cache_purge-%{ngx_cache_purge_version}
--with-ld-opt="-Wl,-E" # so the perl module finds its symbols
make %{?_smp_mflags}
mv ngx_cache_purge-%{ngx_cache_purge_version}/CHANGES ngx_cache_purge-%{ngx_cache_purge_version}/CHANGES.ngx_cache_purge
mv ngx_cache_purge-%{ngx_cache_purge_version}/README.md ngx_cache_purge-%{ngx_cache_purge_version}/README.ngx_cache_purge
%install
make install DESTDIR=%{buildroot} INSTALLDIRS=vendor

find %{buildroot} -type f -name .packlist -exec rm -f '{}' ;
find %{buildroot} -type f -name perllocal.pod -exec rm -f '{}' ;
find %{buildroot} -type f -empty -exec rm -f '{}' ;
find %{buildroot} -type f -iname '*.so' -exec chmod 0755 '{}' ;
install -p -D -m 0755 %{SOURCE1}
%{buildroot}%{_initrddir}/nginx
install -p -D -m 0644 %{SOURCE2}
%{buildroot}%{_sysconfdir}/logrotate.d/nginx
install -p -D -m 0644 %{SOURCE7}
%{buildroot}%{_sysconfdir}/sysconfig/nginx

install -p -d -m 0755 %{buildroot}%{nginx_confdir}/conf.d
install -p -d -m 0755 %{buildroot}%{nginx_home_tmp}
install -p -d -m 0755 %{buildroot}%{nginx_logdir}
install -p -d -m 0755 %{buildroot}%{nginx_webroot}
install -p -m 0644 %{SOURCE3}
%{buildroot}%{nginx_confdir}
install -p -m 0644 %{SOURCE4} %{SOURCE5} %{SOURCE6}
%{buildroot}%{nginx_confdir}/conf.d
install -p -m 0644 %{SOURCE100}
%{buildroot}%{nginx_webroot}
install -p -m 0644 %{SOURCE101} %{SOURCE102}
%{buildroot}%{nginx_webroot}
install -p -m 0644 %{SOURCE103} %{SOURCE104}
%{buildroot}%{nginx_webroot}


%pre
if [ $1 -eq 1 ]; then
getent group %{nginx_group} > /dev/null || groupadd -r %{nginx_group}
getent passwd %{nginx_user} > /dev/null ||
useradd -r -d %{nginx_home} -g %{nginx_group}
-s /sbin/nologin -c "Nginx web server" %{nginx_user}
exit 0
fi

%post
if [ $1 == 1 ]; then
/sbin/chkconfig --add %{name}
fi

%preun
if [ $1 = 0 ]; then
/sbin/service %{name} stop >/dev/null 2>&1
/sbin/chkconfig --del %{name}
fi

%postun
if [ $1 == 2 ]; then
/sbin/service %{name} upgrade || :
fi

%files
%doc LICENSE CHANGES README
%doc ngx_cache_purge-%{ngx_cache_purge_version}/CHANGES.ngx_cache_purge ngx_cache_purge-%{ngx_cache_purge_version}/README.ngx_cache_purge
%{nginx_datadir}/
%{_sbindir}/nginx
%{_mandir}/man3/nginx.3pm.gz
%{_initrddir}/nginx
%dir %{nginx_confdir}
%dir %{nginx_confdir}/conf.d
%dir %{nginx_logdir}
%config(noreplace) %{nginx_confdir}/fastcgi.conf
%config(noreplace) %{nginx_confdir}/fastcgi.conf.default
%config(noreplace) %{nginx_confdir}/fastcgi_params
%config(noreplace) %{nginx_confdir}/fastcgi_params.default
%config(noreplace) %{nginx_confdir}/koi-utf
%config(noreplace) %{nginx_confdir}/koi-win
%config(noreplace) %{nginx_confdir}/mime.types
%config(noreplace) %{nginx_confdir}/mime.types.default
%config(noreplace) %{nginx_confdir}/nginx.conf
%config(noreplace) %{nginx_confdir}/nginx.conf.default
%config(noreplace) %{nginx_confdir}/scgi_params
%config(noreplace) %{nginx_confdir}/scgi_params.default
%config(noreplace) %{nginx_confdir}/uwsgi_params
%config(noreplace) %{nginx_confdir}/uwsgi_params.default
%config(noreplace) %{nginx_confdir}/win-utf
%config(noreplace) %{nginx_confdir}/conf.d/*.conf
%config(noreplace) %{_sysconfdir}/logrotate.d/nginx
%config(noreplace) %{_sysconfdir}/sysconfig/nginx
%dir %{perl_vendorarch}/auto/nginx
%{perl_vendorarch}/nginx.pm
%{perl_vendorarch}/auto/nginx/nginx.so
%attr(-,%{nginx_user},%{nginx_group}) %dir %{nginx_home}
%attr(-,%{nginx_user},%{nginx_group}) %dir %{nginx_home_tmp}
「6.設定ファイルを編集する」では、冒頭の部分でファイルの場所が記述されていないため、以下のコマンドで編集。

vi /etc/nginx/nginx.conf
「共通confファイルを作成する」では、以下のファイルを「/etc/nginx/conf.d/common」に配置すればおk

common.zip
「サイト毎のconfファイルを作成する(バックエンド部分)」「Proxy Cache用のディレクトリを作成します。」以下のコマンドを使用。

mkdir -p /var/cache/nginx/proxy_cache
nginxがインストールされたら、起動する前に「/etc/init.d/httpd stop」で起動していたApacheを停止する。

さくらのVPSでWordPressを動かそう – PHP+eAccelerator+php-fpm設定編 – ポイント

「php-fpmの設定ファイルを編集する」を行なっても上手くインストール出来ていないため、空のドキュメントが作成されてしまう。そのため、別途php-fpmをインストールする。
yum install --enablerepo=remi php-cli php-mbstring php-mysql php-pecl-apc php-fpm
上記コマンドを実行してインストール後、手順通りに設定を行う。

「PHPのデフォルトタイムゾーンの設定とeAcceleratorの設定」で、そのまま実行すると「php-pecl-apc-3.1.14-1.el6.remi.1.x86_64」とコンフリクトしているため、eAcceleratorが利用できませんでした(php-pecl-apcをRemoveしても変わらず)そのため、APCを使用します。また、php-develもインストールされなかったので、単体でインストール。
yum -y --enablerepo=remi,epel,rpmforge install php-devel
vi /etc/php.ini
(既存の項目を変更)
expose_php = Off
date.timezone = Asia/Tokyo
memory_limit = 2048M
post_max_size = 512M
upload_max_filesize =256M
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = auto
mbstring.http_output = auto
(一番下に追記)
extension=apc.so
apc.enabled = 1
apc.shm_size = 288M
apc.max_file_size = 144M
apc.gc_ttl = 3600
apc.ttl = 7200
設定をこのようにしました。

参考元:さくらの VPS 設定覚書(4)PHP

さくらのVPSでWordPressを動かそう – MySQL・phpMyAdmin設定編 – ポイント

「Percona MySQLのインストール」でGPGキーとリポジトリファイルのインストール後、Percona MySQL関連のパッケージをインストールするようになっていますが、「mysql-libs-5.5.30-1.el6.remi.x86_64」が原因で「Error: Package: php-mysql-5.4.13-1.el6.remi.x86_64 (@remi)」となり、コンフリクトするためRemoveします。
yum remove -y mysql-libs-5.5.30-1.el6.remi.x86_64
あとは、手順通り
yum install -y --enablerepo=remi Percona-Server-server-55 Percona-Server-client-55 Percona-Server-shared-compat
を実行します。

「MySQLの初期設定」では、「./mysqltuner.pl」を実行する前に「chmod +x ./mysqltuner.pl」とする必要があります。実行は「root」で行います。
「/etc/my.cnf」は、通常のMySQLをRemoveしたため空になっています。以下のコマンドで作成します。

vi /etc/my.cnf
実際に行なってみて、以下のような注意が出ました。

[!!] InnoDB is enabled but isn't being used
[!!] Key buffer hit rate: 73.3% (15 cached / 4 reads)
[!!] Query cache is disabled
[!!] Thread cache is disabled
[!!] Connections aborted: 8%
参考:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
character-set-server=utf8
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
innodb_buffer_pool_size=32M
innodb_flush_method=O_DIRECT
query_cache_size=32M
query_cache_limit=2M
query_cache_min_res_unit=4k
query_cache_type=1
tmp_table_size=32M
max_heap_table_size=32M
table_open_cache=1024
max_allowed_packet=1M
sort_buffer=512K
read_buffer_size=256K
read_rnd_buffer_size=256K
join_buffer_size=256K
key_buffer_size=16M

max_connections=64
thread_cache=64
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
編集した後、「/etc/init.d/mysql restart」で再起動します。

あとは、実際にWordPressを稼働させてからチューニングしていけばいいでしょう。

http://ドメイン名/phpMyAdmin/にアクセスして、phpMyAdminの画面が表示されたあと、「phpMyAdmin 環境保管領域が完全に設定されていないため、いくつかの拡張機能が無効になっています。理由についてはこちらをご覧ください。」と「mcrypt 拡張がありません。PHP の設定をチェックしてみてください。」と表示されましたが、とりあえずそのまま押し通ります。
「さくらのVPSでWordPressを動かそう – メールサーバー設定編 -」は、まだ利用しないため次のステップへ

さくらのVPSでWordPressを動かそう – 番外編 – ポイント

現在のWordPress最新バージョンが、3.5.1なので、以下のようにします。

wget http://ja.wordpress.org/wordpress-3.5.1-ja.tar.gz
tar zxvf wordpress-3.5.1-ja.tar.gz
mkdir -p /var/www/html/ドメイン名
mv ./wordpress/* /var/www/html/ドメイン名/
自分の場合、「wp-config.php」への書き込みが出来ないと表示されたので、WinSCP経由で「wp-config-sample.php」をリネーム→「表示された文字列をコピペ」でインストールができました。
ファイルのパーミッションについては、「アップロードとパーミッションの変更 | WordPressインストールガイド | ドメインとレンタルサーバーのドメインキング」あたりで設定していけばおk!

nginx利用時のテーマ変更について

そのままの状態ですと、「要求された操作を実行するためには、接続情報が必要です」と出て、テーマを変更することが出来ません(FTPの接続が出来ない)そのため、wp-config.php に以下のコードを追記します。
define('FS_METHOD','direct');
それと、wp-content/以下に「upgrade」フォルダの作成、wp-content/themes の所有権を「nginx」に変更しておく必要があります。操作は、WinSCPなどのSSHクライアントを使用すると簡単にできます。
参考元:WordPressで「要求された操作を実行するためには、接続情報が必要です」と出る原因とその対処法メモ – sakaikの日々雑感~(T)編

さくらVPS nginxとレンタルサーバ3社の読込速度を比較

実際にWordPressをInstallしてみて、下記のテーマを適用後のまっさらな状態でページ読込速度を比較してみました。

2winfactor | Theme: Aware(Responsive)
こちらのテーマを購入しまして、最初に使ってみたのですが、おしゃれで機能が豊富な分か、描画スピードが遅いです。高かったのに…(´;ω;`)
測定方法:FireBugコンソールを用いてF5キーで20回ほどリロード、読込が一番早かったものを記載しています。

サーバー/会社表示速度備考
みんなのドメイン5.29s (onload: 5.35s)L010005010
容量10GB/帯域幅上限100GB/月
Linux,MySQL,PHP5(719 円/月)
さくらスタンダード1.67s (onload: 1.94s)容量30GB/500 円/月
(5,000円/年)
ロリポップチカッパプラン2.13s (onload: 2.22s)容量60GB/月額525円~
さくらVPS SSD2G2.23s (onload: 2.25s)CentOS,Apache,PHP,MySQL
3,680円/月
(40,480円/年)
さくらVPS SSD2G+nginx 1.2.71.24s (onload: 1.32s)CentOS,PHP,MySQL
via http://sourceforge.jp/magazine/12/09/12/0517235
さくらVPS SSD2G+nginx 1.3.151.28s (onload: 1.32s)ngx_cache_purge_version 2.1
APC,php-fpm+Percona MySQL
結果:SourceForge.JPで記載されていた方法が1.24s (onload: 1.32s)と一番速かった。今回紹介した方法も 1.28s (onload: 1.32s)と大差は無いが、導入方法を考えると前者の方が圧倒的に楽で、正直なところ検証結果に残念なものを感じた。

結論

  • 速度を重視して色々なことがやりたい方は「さくらVPS
  • 容量豊富でキャンペーンがいい、そこそこ速度が出て簡単に扱えるのが「ロリポップ
  • 容量は少なくなるけど、リーズナブルで安定した速度が出る、管理も楽なのが「さくらスタンダード
  • さくらインターネットでは、ドメイン取得が割高なので、キャンペーンを利用して使いたいのが「お名前.com
  • お名前.com」よりも「.com」のドメイン価格が安い「みんなのドメイン
このような結論になりました。VPSは上級者向けなので、個人的には普通のレンタルサーバと好みのドメインを取得というのが賢い選択かなと思います。無料のブログサービスから踏み込んで、Web上で何かもっと発信したいという方の参考になれば幸いです。


唯一試せなかったのが、「さくらのマネージドサーバー Xeon プラン(SSDモデル)」Intel QuadCore Xeonでメモリ16GB、380GB SSDなので、どのような結果が出るのか気になるところです。