雑記まみむメモ

雑記、メモ、技法、話題の騒動などを紹介します。

ApacheからNginxへ試しに変更!インストールしてみる

wordpressが重い.... Apacheのチューニングとか試してみたけど、対して変わらなかった... 原因はおそらくサーバスペックが足りてないんだと思うけど...

apacheとnginxの比較がここに詳しくあります。 apacheとnginxの比較表とベンチマーク Nginxのリバースプロキシキャッシュを導入したら、応答速度が約30倍になったとかなんとか...

試してみよう...

phpをインストール

yum -y install php-mysql php-common php php-cgi php-fpm php-gd php-mbstring

php-fpmを設定

;vi /etc/php-fpm.d/www.conf
;user = apache
user = nginx
#php-fpmのサービス実行ユーザ、グループを指定
;group = apache
group = nginx
#php-fpmのサービスのプロセス数を定量とするように設定
;pm = dynamic
pm = static
#php-fpmの最大子プロセス数を設定
;pm.max_children = 50
pm.max_children = 3
#php-fpmが受け付ける最大要求数を設定
;pm.max_requests = 500
pm.max_requests = 500

Nginxをインストールする。

#centos6
$ rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# nginx をインストールする
$ yum -y install nginx

Nginx設定

基本設定ファイル:/etc/nginx/nginx.conf FastCGIデフォルトパラメータ設定ファイル:/etc/nginx/fastcgi_params 仮想ホスト設定ファイル:/etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost; # サーバー名を設定

    #charset koi8-r;
    access_log  /var/log/nginx/localhost/access.log main;
    error_log  /var/log/nginx/localhost/error.log warn;

#    location / {
        root   /usr/share/nginx/html; # ドキュメントルートを設定
        index  index.php index.html index.htm; # index file タイプの優先順を設定
#    }

    try_files $uri $uri/ /index.php?q=$uri&$args; # WordPressのリダイレクト設定

    # PHP FPM との接続を行うため 拡張子phpに対して処理します。
#    location ~ \.php$ {
        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;

    # 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;
    #}

# ログ出力先ディレクトリを作成
$ mkdir /var/log/nginx/localhost
# ログ出力先ディレクトリの所有者をnginxとする
$ chown nginx. /var/log/nginx/localhost

php-fpmデーモンを起動

/etc/init.d/php-fpm start
# 自動起動設定
chkconfig php-fpm on

Nginxのデーモン起動

/etc/init.d/nginx start

apacheを停止しない場合のログ

Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

apache停止

/etc/init.d/httpd stop

Nginx起動

/etc/init.d/nginx start

体感的に早くなった気がする.