我该如何将unicorn用作"rails s"命令?

53

一个新的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命令并指定配置文件,这有点麻烦。我想知道如何通过使用rails s...来使其简单地工作。

这种方式可行吗?


我认为unicorn不能直接从rails server中调用。请查看此链接 - https://github.com/samuelkadolph/unicorn-rails - Dogbert
2
对我来说,只需键入“unicorn”即可,与“rails s”键入相同的按键次数。 - mind.blank
5个回答

61

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

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

$ rails server unicorn

虽然一旦安装了 unicorn-rails,Unicorn 应该将成为默认的应用服务器,因此您也可以只运行 rails server,它应该会使用 Unicorn(假设您的 Gemfile 中没有 Thin 或 Mongrel,否则它们可能会发生冲突,您可能需要删除未使用的那些)。


5
我认为很有趣的是,Gemfile 建议使用 'unicorn' 而不是 'unicorn_rails'。我还发现这个网页上写着:"unicorn_rails 是为了让之前使用早期版本 Rails 的用户更容易过渡而创建的。manpage 鼓励 Rails 3 用户使用普通的 unicorn。"。http://blog.engineyard.com/2010/everything-you-need-to-know-about-unicorn - patrick
1
你在哪个 Gemfile 文件中看到了 unicorn这个 gem?我建议将 unicorn-rails gem 添加到你的 Gemfile 中。 - Stuart M
1
对于Rails 4.2.4,首先需要在您的Gemfile中添加gem 'rack-handlers'gem 'unicorn' - Evan
给无意中发现这个信息的人:unicorn-railsrack 版本 >=3 上会出现问题。这个宝石似乎在目前这个时间点上没有维护。 - undefined

27

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

bundle exec unicorn -p 3000 # default port is 8080

如何增加工作线程? - Huzaifa Saifuddin

19
gem 'rack-handlers'

rails server unicorn

1
使用rack-handlers的另一个美妙之处在于它自动加载config/unicorn.rb文件。 - Eric Caron

0

然而,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


0

我认为不可能将独角兽用作“rails s”。请使用以下方法 -

在gem文件中添加gem 'unicorn',然后运行bundle install。

然后运行以下任何一个命令 -

$ unicorn -p 3000

或者

$ unicorn_rails -p 3000


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接