LoginSignup
6
6

More than 5 years have passed since last update.

RoR+ Nginx + Unicornで起動する。

Last updated at Posted at 2016-06-20

敬意

 -普段RoRで自分で整える環境はRoR Passengerを使うことが多かったのですがNginxが動いている環境で何かしたいと思い作ってみました。
ただそれだけ....
初心者がちょっと難しそうなことをしたなと思う程度で眺めてください。

環境

-centos 7
-rails 4.2.6
-nginx 1.6.3

vagrantの環境は入ってるもので話を進めていきます。

CentOSのインストール

 $ vagrant box add rails_env https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box
 $ vagrant init rails_env
 $ vagrant up

railsのインストール

sudo gem install rails

-少々時間がかかります。

nginxのインストール

sudo yum install nginx

railsプロジェクトの作成

rails new project名
cd project名

Gemfileの編集 インストール

gem 'unicorn'
bundle install

起動確認

bundle exec rails server -b ip番号

さぁ、用意が整いました。

unicornファイルを編集

config/unicorn.rb
@app_path = 'プロジェクトまでのディレクトリ'

worker_processes 2
working_directory "#{@app_path}/"

preload_app true

timeout 30

# socketファイルを生成するディレクトリ
listen "/tmp/unicorn.sock", :backlog => 64
#プロセスIDを生成するディレクトリ
pid "/tmp/unicorn.pid"

#logの場所を指定
stderr_path "#{@app_path}/log/unicorn.stderr.log"
stdout_path "#{@app_path}/log/unicorn.stdout.log"

# USR2シグナルを受けると古いプロセスを止める。
#必要に応じて変えてください
#worker_processes 2 , timeout 10

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!

  old_pid = "#{server.config[:pid]}.oldbin"
    if old_pid != server.pid
      begin
        sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
        Process.kill(sig, File.read(old_pid).to_i)
      rescue Errno::ENOENT, Errno::ESRCH
      end
    end
    sleep 1
  end
after_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end

これでUnicronの設定は完了です。

次は、nginxですね

#最大コネクション数の設定 デフォルトは512でした
events {
  worker_connections  1024;
}
http {

    upstream unicorn_server {
        # unicorn.rbで設定したsockpathを指定
        server unix:/tmp/unicorn.sock
        fail_timeout=0;
    }
    server {
        // 
        listen 80;
        client_max_body_size 4G;
        server_name _;

        keepalive_timeout 5;

        # railsまでのpathを指定
        root rails_project/public;

        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;

            # If you don't find the filename in the static files
            # Then request it from the unicorn server
            if (!-f $request_filename) {
                proxy_pass http://unicorn_server;
                break;
            }
        }

        error_page 500 502 503 504 /500.html;
        location = /500.html {
            root /hoge/vagrant/hage/public;
        }
    }
}

unicornを起動

bundle exec unicorn_rails -c config/unicorn.rb -E development -D

nginxを起動

sudo nginx

スクリーンショット 0028-06-20 22.58.27.png

無事起動できましたね。

chefとかdocker等で立てるのは落ちているものを拾うだけでいい感じはしますが手動で立てたら結構時間かかりました。どのように動くのかを把握するには一番ですね お疲れ様でした。

6
6
1

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
6
6