2013-04-07 56 views
48

一個新的Rails項目的Gemfile中顯示:我如何使用獨角獸作爲「rails s」?

# Use unicorn as the app server 
gem 'unicorn' 

軌小號--help顯示:

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

然而,這樣做的:

rails s unicorn 

我得到:

/Users/patrick/.rvm/gems/[email protected]/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/[email protected]/gems/rack-1.4.5/lib/rack/handler.rb:63:in `try_require' 
from /Users/patrick/.rvm/gems/[email protected]/gems/rack-1.4.5/lib/rack/handler.rb:16:in `get' 
from /Users/patrick/.rvm/gems/[email protected]/gems/rack-1.4.5/lib/rack/server.rb:272:in `server' 
from /Users/patrick/.rvm/gems/[email protected]/gems/railties-3.2.13/lib/rails/commands/server.rb:59:in `start' 
from /Users/patrick/.rvm/gems/[email protected]/gems/railties-3.2.13/lib/rails/commands.rb:55:in `block in <top (required)>' 
from /Users/patrick/.rvm/gems/[email protected]/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap' 
from /Users/patrick/.rvm/gems/[email protected]/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>' 

I'v e過去在其他項目中使用了獨角獸,但總是必須運行unicorn命令並指定一個有點痛苦的配置文件。我想知道如何通過使用「rails s」來簡化它的工作......這可能嗎?

+0

我不認爲'unicorn'可以被稱爲從'rails server'直接。檢查了這一點 - https://github.com/samuelkadolph/unicorn-rails – Dogbert 2013-04-07 04:38:03

+2

只需鍵入'獨角獸'爲我工作,相同數量的擊鍵作爲'軌道' – 2013-04-07 05:10:07

回答

57

它看起來像@Dogbert提到的unicorn-rails寶石實際上可以用來製造獨角獸rails server處理程序。

只需提供gem "unicorn-rails"(併爲Rails 4.2.4,gem "rack-handlers")在Gemfile,運行bundle install安裝寶石,那麼你可以運行:

$ rails server unicorn 

雖然曾經unicorn-rails安裝,獨角獸應該是默認的應用程序服務器,所以你也可以只運行rails server,它應該使用麒麟(假設你不也有薄或雜種在Gemfile,在這種情況下,他們可能會發生衝突,你可能想要刪除你不使用的那些) 。

+5

我覺得非常有趣的是,gemfile有'獨角獸'寧比'unicorn_rails'作爲一個建議。我還覺得有趣的是,這個網頁上寫道:「unicorn_rails對Rails之前的Rack版本的用戶來說是一個更簡單的過渡。該手冊鼓勵Rails 3用戶使用普通獨角獸。」 http://blog.engineyard.com/2010/everything-you-need-to-know-about-unicorn – patrick 2013-04-08 05:40:43

+1

您在哪個Gemfile中看到了「獨角獸」?我建議在你的Gemfile中加入'unicorn-rails' gem。 – 2013-04-08 05:46:12

+4

這可以標記爲接受或您有任何進一步的擔憂? – 2013-09-21 17:05:49

24

一個更好的選擇可能只是直接運行麒麟服務器。

bundle exec unicorn -p 3000 # default port is 8080 
+0

我如何增加工人? – 2017-09-07 13:08:13

16
gem 'rack-handlers' 

rails server unicorn 
+0

使用機架處理程序的另一個優點是它自動載入config/unicorn.rb文件。 – 2013-12-27 23:35:05

+0

這解決了我的問題。非常感謝你 – 2014-01-24 03:05:58

+1

謝謝你。 – Elvn 2015-04-03 16:10:42

0

但是通過Steven答案是做最簡單的方法。

的lib /任務/ 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

我通過一個rake任務在開發環境中運行unicorn

1

我不認爲有可能使用獨角獸作爲'rails s'。使用此 -

添加寶石「麒麟」寶石的文件並運行捆綁安裝。

,然後運行下列任何命令 -

$麒麟-p 3000

$ unicorn_rails -p 3000

+0

目前還不清楚這是如何回答這個問題的? – 2014-12-28 08:56:32

+0

我編輯了我的答案,我認爲這會有所幫助。 – prashant 2014-12-28 16:34:35