178

如何更改我的Rails應用程序以在生產模式下運行?例如,是否有配置文件(例如environment.rb)來執行此操作?將Rails應用程序更改爲生產

+0

看來第二個答案有更多的選票,你會願意給這個快速審查,並接受第二個答案,除非你有任何問題。只會幫助未來的遊客。 Thx :) – Evolve 2017-08-02 23:06:33

回答

19

將環境變量RAILS_ENV更改爲production

+1

其中是環境變量的位置? – BKSpurgeon 2015-10-07 05:12:57

52

如果mipadi's suggestion不起作用,將它添加到到config/environment.rb

# force Rails into production mode when       
# you don't control web/app server and can't set it the proper way     
ENV['RAILS_ENV'] ||= 'production' 
+0

謝謝!!!幫了我很多。 – 2009-12-22 21:35:30

+0

Heroku等雲服務如何?願'ENV ['RAILS_ENV'] || ='production''也可以應用於它們嗎? – Green 2013-04-29 01:51:29

+0

它沒有在軌道上工作4 – franzlorenzon 2014-07-17 12:45:15

18

您還可以將環境傳遞給腳本/服務器:

$ script/server -e production 
61

如果你正在運行的Passenger,那麼默認是在生產中運行,在你的apache conf中:

<VirtualHost *:80> 
    ServerName application_name.rails.local 
    DocumentRoot "/Users/rails/application_name/public" 
    RailsEnv production ## This is the default 
</VirtualHost> 

如果你只是運行具有雜種或使用WEBrick本地服務器,你可以這樣做:

./script/server -e production 

或在bash:

RAILS_ENV=production ./script/server 

實際上覆蓋在enviornment.rb的RAILS_ENV恆也許應該是你的最後一招,因爲它可能不會留一套(見another answer我給上)

341

現在,這將是

rails server -e production 

,或者更緊湊

rails s -e production 

它適用於軌道3+項目。

+30

或簡短的版本'軌道s -e生產' – 2012-08-21 11:06:14

+1

像Heroku的雲服務呢?如何在它們上運行'server -e production'? – Green 2013-04-29 01:48:58

+0

雲服務通常有指定環境的選項,但在其中,「生產」總是默認設置。 – 2013-07-01 16:52:04

2

的Rails 3

添加Rails.env = ActiveSupport::StringInquirer.new('production')到application.rb中和rails s將工作一樣rails server -e production

module BlacklistAdmin 
    class Application < Rails::Application 

    config.encoding = "utf-8" 
    Rails.env = ActiveSupport::StringInquirer.new('production') 

    config.filter_parameters += [:password] 
    end 
end 
2

這是不是運行在生產環境中軌服務器的好方法「rails server -e production」,因爲然後rails作爲單線程應用程序運行,並且一次只能響應一個HTTP請求。

有關生產環境中軌的最好的文章是Production Environments - Rails 3

+3

鏈接腐爛...可用在wayback機器雖然:[鏈接](http://web.archive.org/web/20121022083645/http: ofps.oreilly.com/titles/9780596521424/production_id35801033.html) – rosuav 2013-03-20 02:53:16

17
$> export RAILS_ENV=production 
0

請確保您已在environment.rb文件中以下步驟進行。

ENV [「RAILS_ENV」] || =「生產」

如果在共享託管環境或phushion乘客你運行應用程序,你可能需要需要作出.httaccess(改變公共文件夾內)並將模式設置爲生產。

1

爲默認的服務器:導軌小號-e生產

爲costum服務器端口:軌道S - P m [口] -e生產,例如。軌道S - P m 3002 -e生產

12
rails s -e production 

這將運行服務器RAILS_ENV = 'production'

除了這個,你必須設置資產路徑production.rb

config.serve_static_assets = true 

沒有這個你的資產將不會被加載。

+0

對不起...當投票意向upvote。做出輕微的修改,以糾正錯誤和upvote。 – tomd 2015-09-29 23:37:40

5

正如其他已經公佈:rails server -e production

或者說,我個人最喜歡的:RAILS_ENV=productionrails s

4
RAILS_ENV=production rails s 

OR

rails s -e production 

缺省環境的研究與開發。

相關問題