LoginSignup
1
0

More than 1 year has passed since last update.

unicorn起動時に発生した(Errno::ENOENT)の解消法

Posted at

前提

Ruby on Railsで開発したWebアプリをEC2にデプロイしようとしています。

・環境:Mac OS(M1) Big Sur 11.4
・Ruby -v:3.0.2

発生した事象

unicornを起動しようとした以下のコマンドをEC2内で叩きました。

$ RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -D

するとすると…
以下のように怒られてしまいました。

/home/ユーザ名/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unicorn-6.0.0/lib/unicorn/configurator.rb:85:in `read': No such file or directory @ rb_sysopen - config/unicorn.rb (Errno::ENOENT)
    from /home/ユーザ名/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unicorn-6.0.0/lib/unicorn/configurator.rb:85:in `reload'
    from /home/ユーザ名/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unicorn-6.0.0/lib/unicorn/configurator.rb:78:in `initialize'
    from /home/ユーザ名/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unicorn-6.0.0/lib/unicorn/http_server.rb:78:in `new'
    from /home/ユーザ名/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unicorn-6.0.0/lib/unicorn/http_server.rb:78:in `initialize'
    from /home/ユーザ名/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unicorn-6.0.0/bin/unicorn_rails:209:in `new'
    from /home/ユーザ名/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unicorn-6.0.0/bin/unicorn_rails:209:in `<top (required)>'
    from /home/ユーザ名/.rbenv/versions/3.0.2/bin/unicorn_rails:23:in `load'
    from /home/ユーザ名/.rbenv/versions/3.0.2/bin/unicorn_rails:23:in `<main>'
master failed to start, check stderr log for details

一番最後に記述されているmaster failed to start, check stderr log for detailsと書いてあるのでstderr.logを確認してみることにしましょう。

$ cd /var/www/アプリ名/log
$ ls
capistrano.log  production.log

あれ? stderr.logなんて無いじゃん。ついでに言うとstdout.logも無いじゃん。

解決方法

結論 → 必要なファイルがなかった

そう言えばこの辺の記載は/config/unicorn.rbに記載していた気がするので、確認してみることに。

app_path = File.expand_path('../../', __FILE__)
worker_processes 2
working_directory app_path
stderr_path File.expand_path('../../log/unicorn/stderr.log', __FILE__)
stdout_path File.expand_path('../../log/unicorn/stdout.log', __FILE__)
timeout 30
listen File.expand_path('../../tmp/sockets/unicorn.sock', __FILE__)
pid File.expand_path('../../log/unicorn/unicorn.pid', __FILE__)

preload_app true

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
      Process.kill 'QUIT', File.read(old_pid).to_i
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
end

after_fork do |_server, _worker|
  defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection
end

上から4行目と5行目に書かれてましたね…
と言うことで該当の2つのファイルと2つのファイルを格納しているunicornディレクトリを作ります。

$ mkdir unicorn
$ cd /unicorn
$ touch stderr.log
$ touch stdout.log
$ ls

上記のように/var/www/アプリ名/log上でunicornディレクトリを作成し、/unicornに移動した後に2つのファイルを作成すると以下のようにあるべき2つのファイルが/var/www/アプリ名/log/unicorn内に作成されます。

stderr.log  stdout.log

これで再び、最初のコマンドを叩くことで無事に起動することができました。

$ RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -D

反省点

・まずはエラー内容を見る
・次にログを見る

よく言われることだと思いますが、本当にこれに尽きると言うのが現実。

1
0
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
1
0