LoginSignup
9
11

More than 3 years have passed since last update.

Unicornを再起動する

Posted at

前提

RailsアプリをEC2にデプロイし、Unicornをアプリケーションサーバーとして使用しています。

開発環境でしたら、rails sで起動すれば良かったのですが、本番環境はサーバーの起動&停止方法が変わったので、メモとしてまとめました。

サーバーを停止したい場合の手順

Unicornの起動確認

ps -ef | grep unicorn | grep -v grep
vinaka     15533     1  0 08:02 ?        00:00:01 unicorn_rails master -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production                                                                                    
vinaka     15537 15533  0 08:03 ?        00:00:00 unicorn_rails worker[0] -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production                                                                                 
vinaka     15538 15533  0 08:03 ?        00:00:00 unicorn_rails worker[1] -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production 

master、worker[0]、worker[1]
三つ出てきた場合、Unicornが起動しているようです。

killする

今確認した、三つのうちの一番上の番号(master)15533killします(毎回番号が変わるので都度ps -ef | grep unicorn | grep -v grepで番号を確認)

kill -9 15533

Unicornの停止確認

ps -ef | grep unicorn | grep -v grep


すると何も表示されないはず。
表示されていないと、Unicornが停止しています。

再度サーバーを立ち上げる時の手順

Unicornの停止確認

ps -ef | grep unicorn | grep -v grep


Unicorn起動!

 bundle exec unicorn_rails -c /var/www/rails/アプリ名/config/unicorn.conf.rb -D -E production 


何も出ないと、ちゃんと起動しているようです。

一応確認もする。

ps -ef | grep unicorn | grep -v grep
vinaka     15740     1  1 08:48 ?        00:00:01 unicorn_rails master -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production                                                                                    
vinaka     15744 15740  0 08:48 ?        00:00:00 unicorn_rails worker[0] -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production                                                                                 
vinaka    15745 15740  0 08:48 ?        00:00:00 unicorn_rails worker[1] -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production  

三つ表示されました!起動されている。
さっきと番号が変わっているはず!

ちなみに

master failed to start, check stderr log for details

とエラーが出た場合は

cat log/unicorn.log

でエラーの内容を確認する。
Unicornのバージョン(5.5以上だとエラーが出るみたいなのでGem fileでバージョン指定するのが良いです。)

Gemfile
group :production, :staging do
    gem 'unicorn', '5.4.1'
end

また

unicorn_rails -c /var/www/rails/アプリ名/config/unicorn.conf.rb -D -E production

だと怒られることがあるので、bundle execをつけてあげた方が無難。

bundle exec unicorn_rails -c /var/www/rails/アプリ名/config/unicorn.conf.rb -D -E production

nginxも再起動

Unicornが無事に立ち上がったら、nginxも再起動してあげて完了です。

sudo nginx -s reload

終わりです。

9
11
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
9
11