Nginx + lsyncd で WordPress を負荷分散させる


ハイパフォーマンスHTTPサーバ Nginx入門
 WordPress  WordPress 
 VPS  VPS 
2使 PHP 




 ( vps1.example.com : 192.168.0.1 )
 


Nginx, Load BalancerPHP FastCGI 

lsyncd ( rsync )



 ( vps2.example.com : 192.168.0.2 )
 


Nginx, PHP FastCGI 

MySQL

rsyncd




 IP使
 ( vps1.example.com ) PHP 
34 


lsyncd 


WordPress  lsyncd 
 lsyncd  rsyncd 

 ( 192.168.0.1 )  /var/www/html / ( 192.168.0.2 )  /var/www/html 


rsync, xinetd  yum 
$ sudo yum -y install rsync xinetd

rsyncd.conf 
$ sudo vi /etc/rsyncd.conf
log file = /var/log/rsyncd.log  ← ログファイル
[site]       ← 任意の名前
 path = /var/www/html   ← コピー先対象ディレクトリ
 hosts allow = 192.168.0.1  ← コピーを許可するホスト(配信元プライマリサーバを指定)
 hosts deny = *
 list = true
 uid = root
 gid = root
 read only = false

xinetd, rsync 
$ sudo /sbin/service xinetd start
$ sudo /sbin/chkconfig xinetd on
$ sudo /sbin/chkconfig rsync on

iptables  ( 192.168.0.1 )  873 
$ sudo vi /etc/sysconfig/iptables
:
-A RH-Firewall-1-INPUT -s 192.168.0.1 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

$ sudo /sbin/service iptables restart

via. CentOS 5  rsync/  Server World


CentOS  yum rpmforge 
$ sudo yum -y install --enablerepo=rpmforge lsyncd


$ sudo vi /etc/sysconfig/lsyncd
IGNORE_START_ERRORS="--stubborn"

lsyncd.conf 
$ sudo cp /usr/share/doc/lsyncd/lrsync.lua /etc/lsyncd.conf
$ sudo vi /etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings = {
 statusFile = "/tmp/lsyncd.stat",
 statusInterval = 1,
 logfile = "/var/log/lsyncd.log",  ← ログファイル
}

sync{
 default.rsync,
 source="/var/www/html/",    ← 配信元ディレクトリ
 target="192.168.0.2::site",    ← 配信先サーバ
 rsyncOps="-az",     ← rsync オプション
 excludeFrom="/etc/rsync_exclude.lst", ← 配信除外フォルダのリスト
}

17IPrsyncd.conf 

WP Super Cache 使 /wp-content/cache DB Cache Reloaded fix 使 /wp-content/tmp/ 
$ sudo vi /etc/rsync_exclude.lst
- /wp-content/cache/**
- /wp-content/tmp/**

lsyncd 
$ sudo /sbin/service lsyncd start
$ sudo /sbin/chkconfig lsyncd on



via. CentOS 5  lsync  Server World

MySQL 


 MySQL ( 192.168.0.2 ) 
iptables  ( 192.168.0.1 )  3306 
$ sudo vi /etc/sysconfig/iptables
:
-A RH-Firewall-1-INPUT -s 192.168.0.1 -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.2 -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

$ sudo /sbin/service iptables restart


WordPress  wordpress  password 
$ mysql -u root -p mysql
mysql> grant all privileges on *.* to wordpress @"192.168.0.1" identified by 'password ' with grant option ;
mysql> grant all privileges on *.* to wordpress @"vps1.example.com" identified by 'password ' with grant option ;
mysql> grant all privileges on *.* to wordpress @"192.168.0.2" identified by 'password ' with grant option ;
mysql> grant all privileges on *.* to wordpress @"vps2.example.com" identified by 'password ' with grant option ;
mysql> quit;

wp-config.php  DB_HOST 
IP
localhost MySQL 

mysql 

Nginx 


Nginx 2
Nginx 
WordPress  nginx  : dogmap.jp


nginx.conf 
$ sudo vi /etc/nginx.conf
:
    server {
        listen      8000;
        server_name vps1.example.com;
        access_log /var/log/nginx/$host.access.log main_x;
        location ~ /\.ht { deny  all; }
        location / {
            root /var/www/html;
            index index.php index.html index.htm;
            if (-f $request_filename) {
                expires 30d;
                break;
            }
            if (!-e $request_filename) {
                rewrite ^(.+)$ /index.php?q=$1 last;
            }
        }
        location ~ \.php$ {
            root /var/www/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
            #fastcgi_param  HTTPS on;
            include        fastcgi_params;
        }
    }
:

 Nginx 
$ sudo /sbin/service nginx restart

iptables  ( 192.168.0.1 )  8000 
$ sudo vi /etc/sysconfig/iptables
:
-A RH-Firewall-1-INPUT -s 192.168.0.1 -m state --state NEW -m tcp -p tcp --dport 8000 -j ACCEPT

$ sudo /sbin/service iptables restart


nginx.conf 2
$ sudo vi /etc/nginx.conf
:
    upstream backend {
        ip_hash;
        server 127.0.0.1:8000;
        server 192.168.0.2:8000;
    }

    upstream admin {
        ip_hash;
        server 127.0.0.1:8000;
    }

    server {
        listen      8000;
        server_name vps1.example.com;
        access_log /var/log/nginx/$host.access.log main_x;
        location ~ /\.ht { deny  all; }
        location / {
            root /var/www/html;
            index index.php index.html index.htm;
            if (-f $request_filename) {
                expires 30d;
                break;
            }
            if (!-e $request_filename) {
                rewrite ^(.+)$ /index.php?q=$1 last;
            }
        }
        location ~ \.php$ {
            root /var/www/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
            #fastcgi_param  HTTPS on;
            include        fastcgi_params;
        }
    }

    server {
        listen       80;
        server_name   vps1.example.com;
        access_log /var/log/nginx/$host.access.log main;

        location /wp-admin { proxy_pass http://admin; }
        location /wp-includes { proxy_pass http://admin; }
        location /wp-login.php { proxy_pass http://admin; }
        location /feed { proxy_pass http://backend; }
        location ~ .*\.php { proxy_pass http://backend; }
        location ~ .*\.sql { deny  all; }
        location ~ /\.ht { deny  all; }
        location ~ .*\.(txt|xml|html?|js|css|gz|ico|jpe?g|gif|png|wmv|flv|swf|mpg) {
            root    /var/www/html;
            index   index.html index.htm;
            if (-f $request_filename) {
                access_log  off;
                ssi     on;
                expires 30d;
                break;
            }
        }
        location / {
            root    /var/www/html;
            index   index.php index.html index.htm;
            if ($http_cookie ~* "comment_author_[^=]*=([^%]+)%7C|wordpress_logged_in_[^=]*=([^%]+)%7C") {
                proxy_pass http://admin;
                break;
            }
            proxy_pass http://backend;
        }
:

5,6 2使
 Nginx 

4547, 6568


 Nginx 
$ sudo /sbin/service nginx restart

 Proxy Cache  Proxy Cache 

Nginx + lsyncd で WordPress を負荷分散させる」への2件のフィードバック

  1. ピンバック: #Wordpress でHyperDBを使ったときの運用の疑問 | 5丁目通信(仮称)

  2. ピンバック: 活動ログ 2011/07/19 « Lifelog « Laddy in

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください