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...

这可能吗?

4

5 回答 5

62

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

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

$ rails server unicorn

虽然一旦unicorn-rails安装,Unicorn 应该是默认的应用程序服务器,因此您也可以运行rails server并且它应该使用 Unicorn(假设您Gemfile的 .那些你不使用的)。

于 2013-04-07T07:28:21.193 回答
26

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

bundle exec unicorn -p 3000 # default port is 8080
于 2013-09-01T02:31:07.670 回答
19
gem 'rack-handlers'

rails server unicorn
于 2013-12-11T14:36:35.420 回答
0

然而,答案Steven是最简单的方法。

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

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

于 2014-11-12T15:30:22.690 回答
0

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

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

然后运行以下任何命令 -

$独角兽-p 3000

或者

$ unicorn_rails -p 3000

于 2014-12-28T08:39:36.600 回答