さくらの VPS に登録してみた(4) nginx + unicorn

unicorn

$ gem install unicorn
$ rvmsudo unicorn_rails -p 80

すると起動します。WEBrick とはここでお別れ。

ところで Rails3 は Rack アプリケーションなので、unicorn_rails を使う必要はありません。Rails.root で

$ unicorn

するだけでちゃんと立ち上がります。でも

  • RAILS_RELATIVE_URL_ROOT を渡せる
  • daemon として起動したとき、tmp/pids/unicorn.pid に pid を吐いてくれる
  • tmp/cache/, tmp/pids/, tmp/sessions/, tmp/sockets/ を自動生成

と幾つか便利なところがあるので unicorn_rails を使えばいいと思います。

unicorn の config ですけど、実際にアプリを公開するときは preload_app を true にしたり worker_processes を増やしたりログ出したりとかしますけど今はいいや。起動スクリプトも特に書かず

$ unicorn_rails -D

しておしまい。デフォルトだと 8080 ポートです。

nginx

EPEL にお世話になりっぱなしですね(笑)

$ sudo yum install nginx

今まで Debian しか使ってこなかったので sites-available や sites-enabled が無いと落ち着かないんですよね。なのでデフォルトのサーバは別に移してしまって、sites-enabled を読み込むようにします。

$ sudo vi /etc/nginx/nginx.conf
--- nginx.conf.orig     2010-09-15 01:36:13.000000000 +0900
+++ nginx.conf  2010-09-15 01:51:38.000000000 +0900
@@ -62,60 +62,9 @@
     keepalive_timeout  65;

     #gzip  on;
-
-    #
-    # The default server
-    #
-    server {
-        listen       80;
-        server_name  _;
-
-        #charset koi8-r;
-
-        #access_log  logs/host.access.log  main;
-
-        location / {
-            root   /usr/share/nginx/html;
-            index  index.html index.htm;
-        }
-
-        error_page  404              /404.html;
-        location = /404.html {
-            root   /usr/share/nginx/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;
-        #}
-
-        # deny access to .htaccess files, if Apache's document root
-        # concurs with nginx's one
-        #
-        #location ~ /\.ht {
-        #    deny  all;
-        #}
-    }

     # Load config files from the /etc/nginx/conf.d directory
     include /etc/nginx/conf.d/*.conf;
+    include /etc/nginx/sites-enabled/*;

 }

デフォルトのサーバ設定はこっちに転記。

$ sudo mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled
$ sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
$ sudo vi /etc/nginx/sites-available/default
#
# The default server
#
server {
    listen       80;
    server_name  _;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

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

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/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;
    #}

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

あとは demo アプリ用ですね。

$ sudo ln -s /etc/nginx/sites-available/demo /etc/nginx/sites-enabled/demo
$ sudo vi /etc/nginx/sites-available/demo
upstream demo.example.com {
  server 127.0.0.1:8080;
}
server {
  listen 80;
  server_name  demo.example.com;
  location / {
    proxy_pass http://demo.example.com;
    allow all;
  }
}

nginx を再起動すると、nginx + unicorn + rails3 + ruby1.9.2 + mysql が全部繋がって表示されます。

$ sudo /etc/init.d/nginx restart

あとはアプリを開発するだけですねー。さて、何を作りましょうか。