【问题标题】:How can I use unicorn as "rails s"?如何将独角兽用作“rails s”?
【发布时间】:2013-03-29 08:12:12
【问题描述】:

一个新的 Rails 项目的Gemfile 显示:

# Use unicorn as the app server
gem 'unicorn'

rails s --help 显示:

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

然而,做:

rails s unicorn

我明白了:

/Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/handler.rb:63:in `require': cannot load such file -- rack/handler/unicorn (LoadError)
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/handler.rb:63:in `try_require'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/handler.rb:16:in `get'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/rack-1.4.5/lib/rack/server.rb:272:in `server'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands/server.rb:59:in `start'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands.rb:55:in `block in <top (required)>'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
from /Users/patrick/.rvm/gems/ruby-1.9.3-head@keynote/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

我过去在其他项目中使用过 unicorn,但总是必须运行 unicorn 命令并指定一个配置文件,这有点麻烦。我想知道如何使用rails s... 来简单地使其工作

这可能吗?

【问题讨论】:

标签: ruby-on-rails ruby unicorn


【解决方案1】:

看起来@Dogbert 提到的unicorn-rails gem 实际上可以用来使Unicorn 成为rails server 处理程序。

只需在Gemfile 中包含gem "unicorn-rails"(对于Rails 4.2.4,gem "rack-handlers"),运行bundle install 来安装gem,然后就可以运行了:

$ rails server unicorn

虽然一旦安装了unicorn-rails,Unicorn 应该是默认的应用服务器,所以你也可以只运行rails server,它应该使用 Unicorn(假设你的 Gemfile 中没有 Thin 或 Mongrel,在在这种情况下,它们可能会发生冲突,您可能希望删除不使用的那些)。

【讨论】:

  • 我发现 gemfile 有 'unicorn' 而不是 'unicorn_rails' 作为建议非常有趣。我还发现这个网页上写着很有趣:“unicorn_rails 是为了让 Rails 的 pre-Rack 版本的用户更容易过渡。手册页鼓励 Rails 3 用户改用普通的 unicorn。” blog.engineyard.com/2010/…
  • 您在哪个 Gemfile 中看到 unicorn?我建议将 unicorn-rails gem 添加到您的 Gemfile。
  • 对于 Rails 4.2.4,首先需要将 gem 'rack-handlers'gem 'unicorn' 添加到您的 Gemfile 中
【解决方案2】:

更好的选择可能是直接运行独角兽服务器。

bundle exec unicorn -p 3000 # default port is 8080

【讨论】:

  • 如何增加工人数?
【解决方案3】:
gem 'rack-handlers'

rails server unicorn

【讨论】:

  • 使用 rack-handlers 的另一个好处是它会自动加载 config/unicorn.rb 文件。
【解决方案4】:

但是Steven 的回答是最简单的方法。

我通过 rake 任务在开发环境中运行 unicorn

lib/tasks/dev_unicorn.rake:

task :server do
  # optional port parameter
  port = ENV['PORT'] ? ENV['PORT'] : '3000'
  puts 'start unicorn development'
  # execute unicorn command specifically in development
  # port at 3000 if unspecified
  sh "cd #{Rails.root} && RAILS_ENV=development unicorn -p #{port}"
end
# an alias task
task :s => :server

运行:

rake s

参考http://jing.io

【讨论】:

    【解决方案5】:

    我认为不可能将独角兽用作“rails s”。使用这个 -

    将 gem 'unicorn' 添加到 gem 文件并运行 bundle install。

    然后运行以下任何命令 -

    $独角兽-p 3000

    $ unicorn_rails -p 3000

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-26
      • 2012-09-11
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 2014-08-19
      • 2015-09-27
      • 1970-01-01
      相关资源
      最近更新 更多