超高速で省メモリ!Nginx + WordPressの作り方(設定と移行編)

2016/06/16

masuto151008-00
前回に引き続きはNginx + WordPress環境の導入方法を紹介します。

 

目次

Nginx コンフィグ設定

最初にNginx本体を設定を行います。その次に各ウェブサイト向けの設定を行います。以下のコマンドを実行してNginx本体のコンフィグファイルを開いてください。

# vi /etc/nginx/nginx.conf

我が家のコンフィグはこちら。参考にしてください。

user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
http {
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 10M;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;

keepalive_timeout 5;

gzip_static on;
gzip on;
gzip_http_version 1.0;
gzip_vary on;
gzip_min_length 1024;
gzip_comp_level 6;
gzip_types text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/x-javascript
application/xml
application/xml+rss;
gzip_disable "MSIE [1-6]\.";
gzip_disable "Mozilla/4";
gzip_proxied any;
gzip_buffers 16 8k;
# Supported from http://oki2a24.com/2014/06/16/change-from-apache-to-nginx-with-proxy-cache/

include /etc/nginx/conf.d/*.conf;
}

続いてウェブサイト向けの設定です。今回はWordpress向けの設定になります。
以下のコマンドを実行してWordpress向けのコンフィグファイルを設定します。

# vi /etc/nginx/conf.d/default.conf

我が家のコンフィグはこちら。灰色の部分はデフォルトのままになります。参考にしてください。

server {
listen 80;
server_name ホームページドメイン;
# server_name localhost;

root /var/www/html/wordpress;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

location / {
if (!-e $request_filename) {
rewrite ^.+?(/wp-.*) $1 last;
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ /index.php last;
}
#root /usr/share/nginx/html;
#index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
# root html;
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

以上でNginxの設定は完了です。

 

PHP-FPM コンフィグ設定

続いてPHP-FPMの設定を行います。
以下のコマンドを実行してコンフィグファイルを開いて下さい。

# vi /etc/php-fpm.d/www.conf

我が家のコンフィグはこちら。灰色の部分はデフォルトのままになります。参考にしてください。

[www]
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses on a
; specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock

; Set listen(2) backlog. A value of '-1' means unlimited.
; Default Value: -1
;listen.backlog = -1

; List of ipv4 addresses of FastCGI clients which are allowed to connect.
; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
; must be separated by a comma. If this value is left blank, connections will be
; accepted from any ip address.
; Default Value: any
listen.allowed_clients = 127.0.0.1

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
; mode is set to 0666
;listen.owner = nobody
;listen.group = nobody
listen.group = nginx
listen.owner = nginx
listen.mode = 0666

; Unix user/group of processes
; 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

以下はデフォルト設定のままで問題無いです。保存してviを抜けて下さい。

以上でPHP-FPMの設定は完了です。

 

Nginx + PHP-FPMの自動起動設定

いつものようにOS起動時に Nginx と PHP-FPM の自動設定を行います。
以下のコマンドを実行して下さい。

# chkconfig nginx on
# chkconfig php-fpm on

ちゃんと設定変更されたか確認しましょう。
アプリ名が 3:on となっていればOKです。

grepでand検索を行うには -e オプションを使います。かなり便利なので覚えておくと良いです!
# chkconfig --list | grep -e nginx -e php-fpm

これでNginx + PHP-FPM の環境は整いました。あとは WordPress を Apache からNginxへ切り替える作業が必要です。

Apache から Nginx へ切替

おおまかな流れとしては以下の順序でやっていけば切替ができます。
ウェブサイトの停止時間としては10~30分程度です。

  1. 稼働中の Apache を停止
  2. WordPressディレクトリのアクセス権を変更
  3. Nginx起動
  4. 動作確認

では、この順番通りに以下のコマンドを実行して下さい。
実行結果は省略しますが、基本管理者権限のアカウントであれば問題なく実行できると思います。

  1. # /etc/rc.d/init.d/httpd stop
  2. # chown -R nginx:nginx /var/www/html/wordpress/ ※wordpressディレクトリを指定
  3. # /etc/rc.d/init.d/nginx start
  4. Webブラウザを使って動作確認

これでWordpress + Nginx の設定編が終わりです。

お疲れ様でした。



関連記事

シェア訴求文言

-CentOS
-, , , ,