質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

1623閲覧

railsをpumaからunicornに変更するには?

kozica

総合スコア58

unicorn

Unicornは、汎用のRackアプリケーションサーバ。RackとWebサーバーの機能を併せ持ちます。レスポンス処理や、Nginx単体がRackの機能をサポートしていない事から、一般的にはNginx+Unicorn+Railsの構成を取って用います。

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2018/10/12 05:44

編集2018/10/12 07:31

Rails5でpumaからnginxに変更したいのですが、
どうもうまくいきません。
色々なサイトを見ながらやってますが、いまいちわかりません
知恵を貸して頂きたいです。

主に参考にしたサイト
→ https://qiita.com/hiroky_814/items/72a13dbedd36e88ca599

AWS EC2で作業
###やったこと

ruby

1Gemfileでpumaをコメントアウトしunicornを記載 2 $vi Gemfile 3 #gem 'puma' コメントアウト 4 gem 'unicorn' 5 6bundle install実行 7$bundle install --path vendor/bundle 8 9config配下にunicorn.rbを作成し下記内容を記載 10 $vi config/unicorn.rb 11 rails_root = ENV['RAILS_ROOT'] 12 worker_processes Integer(ENV['WEB_CONCURRENCY'] || 1) 13 timeout 300 # タイムアウト 14 preload_app true # 更新時ダウンタイム無し 15 16 # listen 8080 17 listen "#{rails_root}/tmp/unicorn.sock" 18 pid "#{rails_root}/tmp/unicorn.pid" 19 20 # ログの出力 21 stderr_path File.expand_path('log/unicorn.log', rails_root) 22 stdout_path File.expand_path('log/unicorn.log', rails_root) 23 24ここで参考サイトでDockerFileの設定があったがDockFileがどこにあるのかわからずスキップ 25 26development.ebに下記内容を追加 27 $vi config/environments/development.rb 28 config.logger = ActiveSupport::Logger.new(STDOUT) 29 30rails sで起動 31$bundle exec rails s 32$ bundle exec rails s 33=> Booting WEBrick 34=> Rails 5.2.0 application starting in development on http://localhost:3000 35=> Run `rails server -h` for more startup options 36[2018-10-12 14:42:43] INFO WEBrick 1.4.2 37[2018-10-12 14:42:43] INFO ruby 2.5.1 (2018-03-29) [x86_64-linux] 38[2018-10-12 14:42:43] INFO WEBrick::HTTPServer#start: pid=2899 port=3000 39 40しかし、URLにアクセスしても502 nginxが出て反応がない

###回答から追記
エラー内容をみると
in `require': cannot load such file -- rack/handler/unicorn (LoadError)
ここが問題のようです

$ bundle exec rails s unicorn Exiting Traceback (most recent call last): 27: from bin/rails:9:in `<main>' 26: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `require' 25: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:249:in `load_dependency' 24: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `block in require' 23: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require' 22: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi' 21: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register' 20: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi' 19: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require' 18: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/commands.rb:18:in `<main>' 17: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/command.rb:46:in `invoke' 16: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/command/base.rb:65:in `perform' 15: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch' 14: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command' 13: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run' 12: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:142:in `perform' 11: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:142:in `tap' 10: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:147:in `block in perform' 9: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:47:in `start' 8: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:76:in `print_boot_information' 7: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/commands/server/server_command.rb:105:in `use_puma?' 6: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/server.rb:301:in `server' 5: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/handler.rb:16:in `get' 4: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/handler.rb:74:in `try_require' 3: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `require' 2: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:249:in `load_dependency' 1: from /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `block in require' /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require': cannot load such file -- rack/handler/unicorn (LoadError)

###回答からさらに追記
後ろに -D developmentをつけたところ下記エラーが出てきました

$ bundle exec unicorn_rails -c config/unicorn.rb -D development bundler: failed to load command: unicorn_rails (/home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/bin/unicorn_rails) ArgumentError: rackup file (development) not readable /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.1/lib/unicorn/configurator.rb:702:in `parse_rackup_file' /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.1/lib/unicorn/configurator.rb:86:in `reload' /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.1/lib/unicorn/configurator.rb:77:in `initialize' /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:77:in `new' /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:77:in `initialize' /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.1/bin/unicorn_rails:209:in `new' /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.1/bin/unicorn_rails:209:in `<top (required)>' /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/bin/unicorn_rails:23:in `load' /home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/bin/unicorn_rails:23:in `<top (required)>' master failed to start, check stderr log for details

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

$ bundle exec rails s
=> Booting WEBrick

WEBrick が、起動していますね。

rails s --help

Usage: rails server [mongrel, thin, etc] [options]

とありますので、

bundle exec rails s unicorn

でどうでしょうか?


個人的には...

bundle exec unicorn_rails -c config/unicorn.rb

を使うことが多いです。


(追記)

稼働中の Rails の Gemfile を確認して見たら、下記の2行を追加していました。

gem 'unicorn' gem 'unicorn-rails'

gem 'unicorn-rails' が入っていなければ、追加してください。


ターミナルに こんなメッセージが出力されます。

$ bundle exec rails s unicorn => Booting Unicorn => Rails 5.1.6 application starting in production on http://0.0.0.0:3000 => Run `rails server -h` for more startup options I, [2018-10-12T16:30:50.264894 #4677] INFO -- : listening on addr=0.0.0.0:3000 fd=10 I, [2018-10-12T16:30:50.264982 #4677] INFO -- : worker=0 spawning... I, [2018-10-12T16:30:50.266149 #4677] INFO -- : master process ready I, [2018-10-12T16:30:50.266507 #4710] INFO -- : worker=0 spawned pid=4710 I, [2018-10-12T16:30:50.266647 #4710] INFO -- : worker=0 ready

config/unicorn.rb でログの出力先を指定していると 下記だけの出力になります。

$ bundle exec rails s unicorn => Booting Unicorn => Rails 5.1.6 application starting in production on http://0.0.0.0:3000 => Run `rails server -h` for more startup options

投稿2018/10/12 06:11

編集2018/10/12 07:40
CHERRY

総合スコア25171

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kozica

2018/10/12 06:18

bundle exec rails s unicorn 上記実行すると投稿に追記したエラー内容が出てきました。 $bundle exec unicorn_rails -c config/unicorn.rb $ だとなにも反応がなく上記のように次のコマンド入力待ち状態になりました
CHERRY

2018/10/12 06:23

bundle install で、 unicorn のインストールは正しくできていますか?
kozica

2018/10/12 06:24

$vi log/unicorn.log bundler: failed to load command: unicorn_rails (/home/kkawahara/workspace/working/webex/vendor/bundle/ruby/2.5.0/bin/unicorn_rails) Errno::EADDRINUSE: Address already in use - connect(2) for /tmp/unicorn.sock logみると上記2点が原因なようです
kozica

2018/10/12 06:26

bundle installはエラーなくいけました。 installの際の羅列されるものの中にunicornはしっかり入ってるのも確認してます。
CHERRY

2018/10/12 06:30

> Address already in use - connect(2) for /tmp/unicorn.sock /tmp/unicorn.sock が、既に使われているということですが、別の Rails のアプリが動いていたりしていますか?
CHERRY

2018/10/12 06:33 編集

あれ?  unicorn.rb で、 > listen "#{rails_root}/tmp/unicorn.sock" と設定しているということは、 rails_root が空ということですね。 `rails_root = ENV['RAILS_ROOT']` の行を ` rails_root = ENV['RAILS_ROOT'] || File.expand_path('../../', __FILE__) ` に変更したらどうなりますか?
kozica

2018/10/12 06:35

$ ps ax | grep rails 4732 ? Sl 0:00 unicorn_rails master -c config/unicorn.rb -E development -D 4744 ? Sl 0:00 unicorn_rails worker[0] -c config/unicorn.rb -E development -D 5829 pts/1 S+ 0:00 grep --color=auto rails おそらく以前実行途中でやめたものが残ってたみたいです。 killで上2つを消して、下記コマンドでやり直してますが、処理がなかなか終わらないです、、、 $ bundle exec unicorn_rails -c config/unicorn.rb
kozica

2018/10/12 06:38

rails_rootに設定しなおして $ bundle exec unicorn_rails -c config/unicorn.rb 実行しています。 これが全然処理が終わらないです、、、 そんなに時間かかるものなのでしょうか?
CHERRY

2018/10/12 06:45

普通は、そんなに時間はかからないと思うのですが...
kozica

2018/10/12 06:54

もう20分くらい経ってますが、終わらないのでストップしました。 $bundle exec rails s unicorn だとすぐにエラーが吐き出されるのでそちらを解決したほうがよろしいですかね、、、 in `require': cannot load such file -- rack/handler/unicorn (LoadError)
kozica

2018/10/12 07:10

調べても解決策がわからないのですが、わかりますでしょうか?
CHERRY

2018/10/12 07:16

回答欄にも書きましたが、 gem 'unicorn-rails' も追加してください。
kozica

2018/10/12 07:22

失礼しました。 gemになかったので追記して実行しています。 そして、またすぐに終わらずにずっと実行中となっています、、、
kozica

2018/10/12 07:29

正常に実行完了できるとどのような画面になるのでしょうか? よろしかったらその画面をスクショして頂ける嬉しいです。
CHERRY

2018/10/12 07:41

回答欄に追記しました。ログの設定で、INFO の部分は画面に表示されない場合があります。
kozica

2018/10/12 07:42

もしかして、 $ bundle exec unicorn_rails -c config/unicorn.rb をしても特になにか成功とかログがでてくるわけではないのでしょうか? $ bundle exec unicorn_rails -c config/unicorn.rb $ すぐに次に待機状態になり、rails sをしてみると $ bundle exec rails s => Booting Unicorn => Rails 5.2.0 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options $ となりました。 booting unicornとあるので成功しているのでしょうか?? しかし、仮に成功していても次のコマンド入力待ち状態になりURLにアクセスができません。
kozica

2018/10/12 07:59

あれ?! $ bundle exec rails s unicorn => Booting Unicorn => Rails 5.1.6 application starting in production on http://0.0.0.0:3000 => Run `rails server -h` for more startup options 上記の出力になってますが、URLにアクセスしても 502 Bad Gateway nginx と出るんですよね、、、 これはもうunicorn問題は解決して他の問題ということでしょうか??
kozica

2018/10/12 08:13

Rails 5.1.6 application starting in production on http://0.0.0.0:3000 ここの一文が怪しい気がします、、、 $bundle exec rails -v Rails 5.2.0 となっており5.1.6ではないのでは? また、prodauction on http://0.0.0.0:3000のところでは config/database.ymlのdevelopmentのデータベースしか作成していないので、productionがおかしいのでは? 上記2点が問題点だと思いますがいかがでしょうか?
CHERRY

2018/10/12 08:24

rails s で起動する場合、デフォルトは、development ですが、 RAILS_ENV が定義されているとその環境で起動します。 RAILS_ENV=production 等の定義は、されていますか? `bundle exec gem list | grep rails ` を実行すると どのバージョンの rails がでてきますか? > coffee-rails (4.2.2) > rails (5.1.6) > rails-dom-testing (2.0.3) > rails-html-sanitizer (1.0.4) > sass-rails (5.0.7) > sprockets-rails (3.2.1) > unicorn-rails (2.2.1)
CHERRY

2018/10/12 08:26

> $ bundle exec unicorn_rails -c config/unicorn.rb > をしても特になにか成功とかログがでてくるわけではないのでしょうか? の回答を忘れていました。 unicorn.rbの設定によりますが、ログファイルに出力する設定をしていると画面にログはでないと思います。
kozica

2018/10/12 08:31

$ bundle exec unicorn_rails -c config/unicorn.rb -E development $ bundle exec gem list | grep rails coffee-rails (4.2.2) rails (5.2.0) rails-dom-testing (2.0.3) rails-html-sanitizer (1.0.4) sass-rails (5.0.7) sprockets-rails (3.2.1) unicorn-rails (2.2.1) $ bundle exec rails s unicorn => Booting Unicorn => Rails 5.2.0 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options Rails5.2.0となってますがdevelopmentに一番上のコマンドでしたので、これで正常かと思うのですが、それでもURLにアクセスできません、、、 「xxx.xxx.jp」というURLなのですが、最初は何かプラスで書かなければいけないということはないですよね、、、 なぜアクセスできないのか、、、
kozica

2018/10/12 08:35

on http://localhost:3000 ここが問題なのかもしれないです pumaのときは下記のようになってon http記述がなかったです。 => Rails 5.2.0 application starting in development 当方ポートへの理解が浅くそこが問題だと思いますがどうでしょうか?
kozica

2018/10/12 08:40

やっぱり違ったかもです、、、 pumaは下の方にon httpの記述がありました、、、 => Booting Puma => Rails 5.2.0 application starting in development => Run `rails server -h` for more startup options Puma starting in single mode... * Version 3.11.4 (ruby 2.5.1-p57), codename: Love Song * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://0.0.0.0:3000 * Listening on unix://home/xxxxx/workspace/working/webex puma.sock Use Ctrl-C to stop
CHERRY

2018/10/12 08:42 編集

あ。rails_root の設定を変えて、 #{rails_root}/tmp/unicorn.sock が、` /tmp/unicorn.sock ` から ` Railsディレクトリ/tmp/unicorn.sock ` に変わっていますが、 nginx の設定も同じ unix ソケットを参照するようになっていますか?
kozica

2018/10/12 08:42

$bundle exec rails c Loading development environment (Rails 5.2.0) irb(main):001:0> Entry.first とテーブルが扱えるかもためしましたが問題なく使えました
kozica

2018/10/12 08:42

してないです!!!! やってみます!
kozica

2018/10/12 08:43

すいません、nginxの設定はどこですればよいのでしょうか?、、、
CHERRY

2018/10/12 08:47 編集

linux であれば、/etc/nginx/ の下に 設定ファイルがないでしょうか? /etc/nginx/nginx.conf とか /etc/nginx/conf.d/ドメイン名.conf とか
kozica

2018/10/12 08:48

$ vi /etc/nginx/conf.d/rails.conf upstream webex { #server unix:/home/kkawahara/workspace/working/webex/tmp/sockets/puma.sock; server unix:/home/kkawahara/workspace/working/webex/tmp/sockets/unicorn.sock; } 上記のように変更しましたが、変わらないです。 設定場所がまちがってますでしょうか??
kozica

2018/10/12 08:49

unicornやnginxの再起動が必要とかでしょうか??
kozica

2018/10/12 08:54

アクセスできしたぁぁぁぁぁぁぁぁぁぁぁぁ!!!!!! nginxを再起動が必要でした!!! CHERRYさん、、、未熟者で時間がかかってしまったにも関わらず、見放さずに丁寧に最後まで教えて頂き本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に本当に....... ありがとうございましたぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁ!!!!!!!!
CHERRY

2018/10/12 08:55

あー。/home の中にあるんですね。/home 配下は、nginx は、アクセス権がないので、アクセスできないと思います。 それで、/tmp/unicorn.sock だったのですね。 unicorn.rb を /tmp/unicorn.sock にした方が良さそうですね。
CHERRY

2018/10/12 08:56

あ、再始動だけの問題でしたか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問