コアダンプの数だけ強くなれるよ

見習いエンジニアの備忘log

WordpressをNginx + redisキャッシュで高速化する

Wordpressは今までApache + mysql + phpの構成で構築してたけど、NginxとRedisを使ってみたかったで試してみた。

下記を参考にさせていただいた。

qiita.com

www.jeedo.net


構築環境は下記(さくらVPSの1Gプラン)
・CentOS7 (64bit)
・RAM 1G
・仮想コア 2コア (Xeon)


事前準備

必要なパッケージをインストール

# sudo yum install php php-fpm nginx redis mariadb-server


Wordpressのインストール

本家からtarballをダウンロードして展開する
ファイル名については下記で確認しておく

f:id:segmentation-fault:20170717195157p:plain

# wget http://ja.wordpress.org/wordpress-4.8-ja.tar.gz
# mkdir /var/www/wordpress/
# tar zxvf wordpress-4.8-ja.tar.gz -C /var/www/wordpress


Redisキャッシュ対応

必要なファイルをダウンロードして展開する

# cd /var/www/wordpress/
# wget http://uploads.staticjw.com/ji/jim/predis.php
# chmod 644 predis.php
# wget http://www.jeedo.net/downloads/wp-index-redis.zip
# unzip wp-index-redis.php
# chmod 644 wp-index-redis.php

index.phpをwp-index-redis.phpに置き換える。

# mv index.php _index.php # 初期ファイルはリネームしてバックアップ
# mv wp-index-redis.php index.php


Wordpressの準備

mysqlの設定

mysql-serverの起動

# systemctl enable mariadb.service
# systemctl start mariadb.service


MySQLの基本設定(*1)

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.
# (以降はひたすらEnter)


データベースの作成

# mysql -u root -p
MariaDB [(none)]> CRATE DATABASE (お好きなデータベース名)
MariaDB [(none)]> CREATE USER (お好きなユーザ名)@localhost IDENTIFIED BY '(お好きなパスワード)';
MariaDB [(none)]> GRANT ALL ON wordpress.* TO (お好きなユーザ名)@localhost;
MariaDB [(none)]> exit
Bye


作成したデータベース名、ユーザ名、パスワードをwp-config.phpに記載する。

# cp wp-config-sample.php wp-config.php
# vim wp-config.php
# diff -u wp-config-sample.php wp-config.php
--- wp-config-sample.php        2017-06-09 01:38:02.000000000 +0900
+++ wp-config.php       2017-07-09 17:03:16.814058312 +0900
@@ -26,13 +26,13 @@

 // ** MySQL 設定 - この情報はホスティング先から入手してください。 ** //
 /** WordPress のためのデータベース名 */
-define('DB_NAME', 'database_name_here');
+define('DB_NAME', '(お好きなデータベース名)');

 /** MySQL データベースのユーザー名 */
-define('DB_USER', 'username_here');
+define('DB_USER', '(お好きなユーザ名)');

 /** MySQL データベースのパスワード */
-define('DB_PASSWORD', 'password_here');
+define('DB_PASSWORD', '(お好きなパスワード)');

 /** MySQL のホスト名 */
 define('DB_HOST', 'localhost');


PHPの設定

下記コンフィグファイルを編集してuserをnginxに変更する

# cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.orig
# diff -u /etc/php-fpm.d/www.conf.orig  /etc/php-fpm.d/www.conf
--- /etc/php-fpm.d/www.conf.orig        2017-07-09 16:20:28.104075951 +0900
+++ /etc/php-fpm.d/www.conf     2017-07-09 16:20:42.242076156 +0900
@@ -36,9 +36,9 @@
 ; Note: The user is mandatory. If the group is not set, the default user's group
 ;       will be used.
 ; RPM: apache Choosed to be able to access some dir as httpd
-user = apache
+user = nginx
 ; RPM: Keep a group allowed to write in log dir.
-group = apache
+group = nginx

 ; Choose how the process manager will control the number of child processes.
 ; Possible Values:
#
# systemctl enable php-fpm.service
# systemctl start php-fpm.service


Nginxの設定

コンフィグを編集

# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig
# vim /etc/nginx/nginx.conf
# diff -u /etc/nginx/nginx.conf.orig /etc/nginx/nginx.conf
--- /etc/nginx/nginx.conf.orig  2017-07-09 15:09:20.002991898 +0900
+++ /etc/nginx/nginx.conf       2017-07-09 15:03:53.841982845 +0900
@@ -38,13 +38,18 @@
     server {
         listen       80 default_server;
         listen       [::]:80 default_server;
-        server_name  _;
-        root         /usr/share/nginx/html;
+        server_name  (あなたのサーバ名);
+        root         /var/www/wordpress;

         # Load configuration files for the default server block.
         include /etc/nginx/default.d/*.conf;

-        location / {
+        location ~ \.php$ {
+               root            /var/www/wordpress;
+               fastcgi_pass    127.0.0.1:9000;
+               fastcgi_index   index.php;
+               fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
+               include         fastcgi_params;
         }

         error_page 404 /404.html;


/var/www/wordpress配下の所有者をNginxに変更
>||
# chown -R nginx.nginx /var/www/wordpress/


Nginxを起動

# systemctl enable nginx.service
# systemctl start nginx.service


ファイアウォールの設定

ファイアウォールにhttpの通過を許可させる

# firewall-cmd --add-service=http --zone=public --permanen
# firewall-cmd --reload


実行結果

ブラウザでサーバにアクセスしてWordpressのインストールが実行できればOK
アクセス先はhttp://(あなたのサーバ)/wp-admin/install.php

f:id:segmentation-fault:20170922125755p:plain


インストール後にWordpressにログインできれば完了
f:id:segmentation-fault:20170922125805p:plain



実際にWordpressの表示が高速化されたのかは比較対象がないので不明(^_^;)


---------------------
(*1)
mysql_secure_installationコマンド実行で下記エラーが発生。
mysqladminコマンドでパスワードを設定すると解消した。

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
#
# mysqladmin -u root password
New password:
Confirm new password: