われプログラミングする、ゆえにバグあり

私だって価値を創造してみたいのです

Rails + Unicorn + Nginx

Unicorn + Nginx で Railsアプリケーションを動かすまでの簡単な手順のメモです。

環境を確認

$ cat /etc/redhat-release
 # -> CentOS release 6.4 (Final)
$ ruby -v
 # -> ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]
$ rails -v
 # -> Rails 4.0.3

 

アプリケーションの雛形を作成

$ rails new rails_demo -d mysql --skip-bundle

移動しておきます

$ cd rails_demo

 

Gemfile を編集

$ vim Gemfile

# アンコメント - 19行目あたり
gem 'therubyracer', platforms: :ruby

# アンコメント - 39行目あたり
gem 'unicorn'

bundle install を実行しておきます

$ bundle install --path vendor/bundle

 
db:create を実行しておきます

$ bundle exec rake db:create

 

Unicorn の設定

$ vim config/unicorn.rb

参考 https://gist.github.com/nragaz/472104

worker_processes 2
listen '/tmp/unicorn.sock'
rails_root = `pwd`.gsub("\n", "")
pid         "#{rails_root}/tmp/pids/unicorn.pid"
stderr_path "#{rails_root}/log/unicorn.log"
stdout_path "#{rails_root}/log/unicorn.log"

 

Nginx の設定

$ sudo vim /etc/nginx/conf.d/default.conf

upstream unicorn {
  server unix:/tmp/unicorn.sock;
}

server {
  listen 80 default_server;
  server_name _;

  location / {
    proxy_pass http://unicorn;
  }

  error_page 404 /404.html;
  location = /404.html {
    root /usr/share/nginx/html;
  }

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }
}

 

アプリケーションの起動

$ bundle exec unicorn_rails -c config/unicorn.rb -D
$ sudo service nginx restart

Webブラウザで http://[host_name] を開くと、Railsの「Welcome aboard」の画面が表示されます。