LoginSignup
0
8

More than 5 years have passed since last update.

Nginx + Unicorn + Rails

Posted at

サーバを再起動(起動/停止)するコマンドはシェルスクリプトに追記してまとめて実行できるようにすることをお勧めする。

Railsを入れる

Unicornを入れる

gemでインストール(bundleでも出来るそうな)
RailsのGemfileでコメントインあるいは追記してからbundle installするでも良い。
$ gem install unicorn
Railsのルートディレクトリ下にunicornの設定ファイルを作成

config/unicorn.rb
Unicornのログ出力先を指定
#File.expand_pathはパスを絶対パスに解決するメソッド File.expand_path: https://ruby-doc.org/core-2.2.0/File.html#method-c-expand_path
rails_root = File.expand_path('../../', __FILE__)
#ワーカー数を指定する
worker_processes 2
working_directory rails_root

listen "#{rails_root}/tmp/unicorn.sock" #??「Unicornのプロセスをlistenするアドレストポートを指定」らしいが…
pid "#{rails_root}/tmp/unicorn.pid" #pid fileの位置を指定する

#ログの出力先を指定
stderr_path "#{rails_root}/log/unicorn_error.log"
stdout_path "#{rails_root}/log/unicorn.log"

起動と停止。以下の順で再起動となる

server_restart.sh
# 停止
kill -QUIT `cat tmp/unicorn.pid`
# デーモンとして起動
unicorn_rails -c config/unicorn.rb -E development -D

Nginxを入れる

インストールと設定ファイルの編集

$ brew install nginx
$ cd /usr/local/etc/nginx
$ cp nginx.conf.default nginx.conf
$ cp nginx.conf nginx.conf.YYYYMMDD
$ vi nginx.conf

confファイルに以下を追加する。http{}の中に記述すること。

nginx.conf
upstream unicorn {
        # nginxとunicornの連携
        # unicorn.rbで設定したunicorn.sockを指定
        server unix:{RAILS_PROJECT_ROOT}/tmp/unicorn.sock;
    }

server {
        listen 8081;
        server_name {PROJECTDOMAIN_FOR_LOCAL};

        root {RAILS_PROJECT_ROOT}/public;

        access_log /usr/local/var/log/nginx/{PROJECT_NAME}_access.log;
        error_log /usr/local/var/log/nginx/{PROJECT_NAME}_error.log;

        client_max_body_size 100m;
        error_page 500 502 503 504 /500.html;

        try_files $uri/index.html $uri @unicorn;

        location @unicorn {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_pass http://unicorn;
        }
    } 

NginxとUnicornの設定ファイルをいじる

エラーの原因
rootの文法
tmpの利用法 新利用法?
http://serverfault.com/a/464025

サーバを再起動するシェルスクリプトを用意する

server_restart.sh
nginx
nginx #sudo必要

参考

*よく参照した: http://qiita.com/itosho/items/e0c2290f8079e740030b
bundle installからunicorn。Unicornの詳細もある:http://qiita.com/Salinger/items/5350b23f8b4e0dcdbe23
(^見ておきたい)
NginxとPHP: http://qiita.com/makotok7/items/2ca1c50d37cb2f8f3972
参考になりそうだが、他の方法で実行できてしまった:http://qiita.com/larufa/items/00ec6802ab7c1377114d

0
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
8