CentOS 6.3 (さくらVPS) に Nginx + php-fpm + MySQL の WordPress 環境を構築する手順

さくら VPS – CentOS6.3 に Nginx + php-fpm + MySQL で WordPress 環境を構築する手順をご紹介します。

WordPress | ワードプレス

Nginx のインストール

Nginx リポジトリを登録

# vim /etc/yum.repos.d/nginx.repo
 
[nginx]
name=nginx
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=0

インストール可能な Nginx のバージョン確認

# yum info --enablerepo=nginx nginx

Nginx のインストール

# yum -y install --enablerepo=nginx nginx

Nginx が正常にインストールされたことを確認

# nginx -v
nginx version: nginx/1.4.2

Nginx の各種設定

# vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  2;
 
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    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  65;
 
    # gzip
    gzip  on;
    gzip_http_version 1.0;
    gzip_vary         on;
    gzip_comp_level   6;
    gzip_types        text/xml text/css application/xhtml+xml application/xml application/rss+xml application/atom_xml application/x-javascript application/x-httpd-php;
    gzip_disable      "MSIE [1-6]\.";
 
    # reverse proxy
    proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=czone:4m max_size=50m inactive=120m;
    proxy_temp_path   /var/tmp/nginx;
    proxy_cache_key   "$scheme://$host$request_uri";
    proxy_set_header  Host               $host;
    proxy_set_header  X-Real-IP          $remote_addr;
    proxy_set_header  X-Forwarded-Host   $host;
    proxy_set_header  X-Forwarded-Server $host;
    proxy_set_header  X-Forwarded-For    $proxy_add_x_forwarded_for;
 
    upstream backend {
        ip_hash;
        server 127.0.0.1:8080;
    }
 
    include /etc/nginx/conf.d/*.conf;
}
# vim /etc/nginx/conf.d/virtual.conf
server {
 
    listen      80;
    server_name example.com;
    root        /var/www/vhosts/example.com/www;
    access_log  /var/log/nginx/example.com/access.log   main;
    error_log   /var/log/nginx/example.com/error.log;
    client_max_body_size 36M;
 
    location /wp-admin { proxy_pass http://backend; }
    location ~ .*\.php { proxy_pass http://backend; }
    location / {
        set $mobile "";
        if ($http_user_agent ~* '(DoCoMo|J-PHONE|Vodafone|MOT-|UP\.Browser|DDIPOCKET|ASTEL|PDXGW|Palmscape|Xiino|sharp pda browser|Windows CE|L-mode|WILLCOM|SoftBank|Semulator|Vemulator|J-EMULATOR|emobile|mixi-mobile-converter)') {
            set $mobile "@ktai";
        }
        if ($http_user_agent ~* '(iPhone|iPod|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry)') {
            set $mobile "@mobile";
        }
        if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
            set $do_not_cache 1;
        }
 
        proxy_no_cache     $do_not_cache;
        proxy_cache_bypass $do_not_cache;
        proxy_cache czone;
        proxy_cache_key "$scheme://$host$request_uri$is_args$args$mobile";
        proxy_cache_valid  200 301 302 60m;
        proxy_cache_valid  404 5m;
        proxy_cache_use_stale  error timeout invalid_header updating
                               http_500 http_502 http_503 http_504;
        proxy_pass http://backend;
        proxy_redirect http://example.com:8080/ /;
    }
}
 
server {
 
    listen      8080;
    server_name example.com;
    root        /var/www/vhosts/example.com/www;
    access_log  /var/log/nginx/example.com/access.log   main;
    error_log   /var/log/nginx/example.com/error.log;
    client_max_body_size 36M;
 
    location / {
        index  index.php index.html index.htm;
        # static files
        if (-f $request_filename) {
            expires 14d;
            break;
        }
        # request to index.php
        if (!-e $request_filename) {
            rewrite ^(.+)$  /index.php?q=$1 last;
        }
    }
 
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        include        fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  /var/www/vhosts/example.com/www/$fastcgi_script_name;
    }
}

log ディレクトリを作成します。

mkdir /var/log/nginx/example.com

conf ファイルを編集後、文法チェックを行います。

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Port8080 を開放するためにファイアーウォールの設定に追記します。

# vim /etc/sysconfig/iptables
 
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080  -j ACCEPT

Nginx の自動起動設定

chkconfig nginx on

MySQL のインストール

次に MySQL をインストールします。

インストール可能な MySQL のバージョン確認

# yum info mysql-server

MySQL server のインストール

yum のパッケージは mysql ではなく、mysql-server なので間違えないように。

# yum -y install --enablerepo=remi mysql-server

MySQL 設定ファイルの修正

# cp /etc/my.cnf /etc/my.cnf.default
# vim /etc/my.cnf
 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
 
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
 
# Settings user and group are ignored when systemd is used (fedora >= 15).
# If you need to run mysqld under different user or group, 
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd
user=mysql
 
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
 
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
 
# Semisynchronous Replication
# http://dev.mysql.com/doc/refman/5.5/en/replication-semisync.html
# uncomment next line on MASTER
;plugin-load=rpl_semi_sync_master=semisync_master.so
# uncomment next line on SLAVE
;plugin-load=rpl_semi_sync_slave=semisync_slave.so
 
# Others options for Semisynchronous Replication
;rpl_semi_sync_master_enabled=1
;rpl_semi_sync_master_timeout=10
;rpl_semi_sync_slave_enabled=1
 
# http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html
;performance_schema
 
character-set-server=utf8
 
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
 
 
# ここから下を追記する
 
[client]
default-character-set=utf8
 
[mysql]
default-character-set=utf8
 
[mysqldump]
default-character-set=utf8

MySQL の起動&自動起動の設定

service mysqld start
chkconfig mysqld on

MySQL のパスワード設定

# mysqladmin -u root password
# mysqladmin -u root -h hostname password

PHP のインストール・初期設定

最後に PHP をインストールして、初期設定します。

インストール可能な PHP のバージョン確認

# yum info php php-mbstring php-mysql php-pear php-gd php-pecl-apc php-fpm

PHP のインストール

# yum -y install php php-mbstring php-mysql php-pear php-gd php-pecl-apc php-fpm

PHP が正常にインストールされたことを確認する。

# php -v
PHP 5.3.15 (cli) (built: Jul 20 2012 12:50:06) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

php.ini の設定変更

# vim /etc/php.ini
expose_php = Off
 
error_log = /var/log/php_errors.log
 
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = pass
mbstring.http_output = pass
mbstring.encoding_translation = Off
 
[Date]
date.timezone = Asia/Tokyo

log ファイルの作成

# touch /var/log/php_error.log
# chmod 757 /var/log/php_error.log

APC の設定

mkdir -p /var/www/vhosts/example.com
cp /usr/share/doc/php-pecl-apc-3.1.10/apc.php /var/www/vhosts/example.com/
vim /var/www/vhosts/example.com/apc.php 
 
defaults('ADMIN_USERNAME','user');
defaults('ADMIN_PASSWORD','password');

Fast CGI のインストール確認

php-fpm が正常にインストールされたことを確認する。

# php-fpm -v
PHP 5.3.15 (fpm-fcgi) (built: Jul 20 2012 12:52:37)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

php-fpm の各種設定

# vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
 
pm = dynamic
pm.max_children =  30
pm.start_servers = 11
pm.min_spare_servers = 10
pm.max_spare_servers = 15
pm.max_requests = 500
 
php_admin_flag[expose_php] = off

php-fpm を起動します。

service php-fpm start

php-fpm の自動起動設定を有効にします。

chkconfig php-fpm on
# netstat -ltn | grep -E '(80|9000)'
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN

以上です。

参考情報

こちらの設定の方が良いかも?