雑記まみむメモ

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

UbuntuにNginxをインストールする

必要なものを更新

apt-get update
apt-get upgrade

Nginxをインストール

sudo apt-get install nginx ←インストール sudo service nginx start ←サービス開始

php5-fpmをインストール

sudo apt-get install php5-fpm

cacheディレクトリを作成

mkdir -p /var/cache/nginx/cache

nginx.confの編集

vi /etc/nginx/nginx.conf
http {
  # 省略
       
  proxy_cache_path /var/cache/nginx/cache levels=1 keys_zone=zone:4m inactive=7d max_size=50m;
 
  # 省略
}

defaultを編集

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default_

vi /etc/nginx/sites-available/default

upstream backend {
    server 127.0.0.1:8080;
}

server {
    listen       80;
    server_name  localhost.com; # サーバー名を設定
    root   /var/www/wordpress; # ドキュメントルートを設定
    index  index.php index.html index.htm; # index file タイプの優先順を設定

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

#    location / {
#        if (!-e $request_filename) {
#          rewrite ^/(.+)#  /index.php?q=$1 last;
#          break;
#        }
#    }

    location ~* /wp-config.php {
        deny all;
    }

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

set $do_not_cache 0;
if ($request_method != GET) {
set $do_not_cache 1;
}
if ($uri !~* ".(jpg|png|gif|jpeg|css|js|swf|pdf|html|htm)$") {
set $do_not_cache 1;
}


    # PHP FPM との接続を行うため 拡張子phpに対して処理します。
    location ~ \.php$ {
       # fastcgi_pass            127.0.0.1:9000;
        fastcgi_pass            unix:/var/run/php5-fpm.sock;
        fastcgi_index           index.php;
        fastcgi_param           SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include                 fastcgi_params;

        #proxy
        proxy_cache_bypass $do_not_cache;
        proxy_cache zone;
        proxy_cache_key $scheme$proxy_host$uri$is_args$args;
        proxy_cache_valid  200 1d;
    }
}

server {
    listen       8080;
    server_name  _;

    #access_log  /var/log/nginx/access_8080.log  main;
    access_log  /var/log/nginx/access_8080.log;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }

    location ~ .php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_pass_header "X-Accel-Redirect";
        fastcgi_pass_header "X-Accel-Expires";
    }
}

nginx再読み込み

service nginx reload

php5-fpmのphp.iniを編集

cp /etc/php5/fpm/php.ini /etc/php5/fpm/php.ini_ vi /etc/php5/fpm/php.ini

date.timezone = Asia/Tokyo
cgi.fix_pathinfo=0

php5-fpmを再読み込み

service php5-fpm reload

php5-fpmのconfを編集

vi /etc/php5/fpm/pool.d/www.conf

listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = static

/etc/init.d/php5-fpm restart
sysv-rc-conf php5-fpm on ←できない場合 sudo apt-get install sysv-rc-conf
/etc/init.d/nginx restart
sysv-rc-conf nginx on