LoginSignup
8
6

More than 5 years have passed since last update.

Ruby on Rails 5.1.5 + nginx + unicorn でアプリケーションサーバー構築

Last updated at Posted at 2018-03-02

開発環境

ローカルです
macOS High Sierra
Ruby on Rails 5.1.5
nginx 1.13.9
unicorn 5.4.0

まず

  1. Railsの標準アプリケーションサーバーで起動できるようにする(Gemfileにunicornを入れるの忘れないように)
  2. homebrewでnginxをインストールしておく

unicornの設定

railsプロジェクトのconfig直下にunicorn.rbを作成する

config/unicorn.rb
rails_root = File.expand_path('../../', __FILE__)

worker_processes 2
working_directory rails_root

listen "#{rails_root}/tmp/sockets/unicorn.sock"
pid "#{rails_root}/tmp/pids/unicorn.pid"

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

stderr_path "#{rails_root}/log/unicorn_error.log"
stdout_path "#{rails_root}/log/unicorn.log"

注意点
1. unicorn.sockは/tmp直下に置かないこと
下記のように直下におくと他のプロセスから参照できなくなり、nginxと接続できなくなる。

config/unicorn.rb
listen "#{rails_root}/tmp/unicorn.sock"

エラー

/log/unicorn_error.log
connect() to unix:/Users/username/Desktop/hogehoge/tmp/unicorn.sock failed (2: No such file or directory) while connecting to upstream

unicornを起動させる

$ unicorn_rails -c config/unicorn.rb -E development -D
  1. 確認
$ ps -ef | grep unicorn | grep -v grep 
501 93389     1   0  1:40AM      0:00.01 unicorn_rails master -c config/unicorn.rb -E development -D
501 93390 93389   0  1:40AM      0:02.36 unicorn_rails worker[0] -c config/unicorn.rb -E development -D
501 93391 93389   0  1:40AM      0:02.36 unicorn_rails worker[1] -c config/unicorn.rb -E development -D

workerを確認できたらok
2. 停止
$ kill `cat tmp/pids/unicorn.pid`

注意点
オプションはdevelopmentを指定する
ローカルで、myusqlとか使ってる人がproductionとかにするとpostgresが無いとかGemfileの'pg'がなんたら言われる

nginxの設定

1.編集ファイル作成

$ cd /usr/local/etc/nginx
$ cp nginx.conf.default nginx.conf

2.niginx.confのユーザーがnobodyでコメントアウトされているので、ユーザーネームにする

/usr/local/etc/nginx/nginx.conf

#user nobody;
user  hogepiyo;
worker_processes  1;
   ...
   ...

3.servers直下に新しくconfファイルを作成する

/usr/local/etc/nginx/servers/rails_app.conf

upstream unicorn {
    #railsプロジェクトのunicorn.sockを指定する
    server unix:/Users/username/Desktop/hogehoge/tmp/unicorn.sock;
}

server {
  # 今回はローカル環境なので、localhostを指定する
   # portはご自由に
   listen 8085;
   server_name localhost;
  # railsプロジェクトのpublicを指定
   root /Users/username/Desktop/hogehoge/public;
  
  # logは大切
   access_log /usr/local/var/log/nginx/microposts_access.log;
   error_log /usr/local/var/log/nginx/microposts_error.log;

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

   try_files $uri/index.html $uri @unicorn;
  
  # localhost:8085にアクセスして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;
   }
}

早速動かしてみる

  1. nginx $ brew services start nginx
  2. unicorn $ unicorn_rails -c config/unicorn.rb -E development -D

起動コマンドのユーザー合わせる
unicornをsudo(スーパーユーザー)とかで起動して、nginxをhogepioyoが起動させるとこんな感じで Permission deniedされる。(逆も然り)(sudoコマンドで生成されたファイル(error.logとか)を全部消してから再度起動コマンドを)

/log/unicorn_error.log
connect() to unix:/Users/username/Desktop/hogehoge/tmp/socket/unicorn.sock failed (13: Permission denied) while connecting to upstream,

なので、両方のプロセスのユーザーが一致していることを確認

$ ps aux | grep unicorn
hogepioyo   92509   0.0  0.0  4267768   1020 s005  S+    1:08AM   0:00.00 grep unicorn
hogepioyo   92458   0.0  0.3  4396392  46544   ??  S     1:02AM   0:02.38 unicorn_rails worker[1] -c config/unicorn.rb -E development -D
hogepioyo   92457   0.0  0.3  4389224  46072   ??  S     1:02AM   0:02.38 unicorn_rails worker[0] -c config/unicorn.rb -E development -D
hogepioyo   92456   0.0  0.0  4323240   2080   ??  S     1:02AM   0:00.01 unicorn_rails master -c config/unicorn.rb -E development -D
$ ps aux | grep nginx
hogepioyo   92503   0.0  0.0  4267768   1020 s005  S+    1:07AM   0:00.00 grep nginx
hogepioyo   92421   0.0  0.0  4336720   1112   ??  S    12:59AM   0:00.01 nginx: worker process
hogepioyo   90475   0.0  0.0  4320100   1188   ??  Ss   11:05PM   0:00.03 nginx: master process nginx

最後にnginxで指定したhttp://localhost:8085/にアクセスしてアプリケーションが開いていたら成功。

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