将Rails应用程序更改为生产

Adl*_*ado 188 development-environment ruby-on-rails production-environment

如何更改我的Rails应用程序以在生产模式下运行?是否有配置文件,例如environment.rb,这样做?

Ban*_*get 350

现在就是这样

rails server -e production
Run Code Online (Sandbox Code Playgroud)

或者,更紧凑

rails s -e production
Run Code Online (Sandbox Code Playgroud)

它适用于rails 3+项目.

  • 或简短版本`rails s -e production` (33认同)
  • 那么像Heroku这样的云服务呢?如何在它们上运行`server -e production`? (2认同)
  • `echo"export RAILS_ENV = production">>〜/ .bash_profile` (2认同)

etu*_*usm 70

如何使用Apache和Phusion Passenger在生产模式下设置和运行Rails 4应用程序(循序渐进):

通常,您可以rails shttp://something.com:3000上输入您的Rails项目,并获得应用程序的开发版本.生产模式配置起来有点棘手.

我已经搞砸了一段时间了,所以我想我会为新手(比如我自己)写这篇文章.有一些小调整遍布互联网,并认为这可能更容易.

  1. 请参阅本指南以了解服务器的核心设置(CentOS 6,但它应适用于几乎所有Linux版本):https://www.digitalocean.com/community/tutorials/how-to-setup-a-rails-4 -app与- apache的和-乘客上的centos -6-

  2. 绝对确定在设置Passenger之后,您已经编辑了/etc/httpd/conf/httpd.conf文件以反映您的目录结构.您希望将DocumentRoot指向httpd.conf具有此类目录的文件中的Rails项目/公共文件夹 Anywhere :/var/www/html/your_application/public需要更新或一切都会非常令人沮丧.我不能强调这一点.

  3. 重启服务器(或者至少是Apache service httpd restart)-

  4. 输入您的Rails项目文件夹/var/www/html/your_application并开始迁移rake db:migrate.确保存在数据库表,即使您计划稍后添加表(这也是步骤1的一部分).

  5. RAILS_ENV=production rake secret- 这将创建一个可以添加的secret_key config/secrets.yml.你可以将它复制/粘贴到config/secrets.yml中,以便让事情运行起来,尽管我建议你不要这样做.就个人而言,我执行此步骤以确保其他所有工作正常,然后将其更改回来并在以后获取.

  6. RAILS_ENV=production rake db:migrate

  7. RAILS_ENV=production rake assets:precompile如果您正在提供静态资产.这会将js,css,图像文件推入/public文件夹.

  8. RAILS_ENV=production rails s

此时,您的应用应该可用,http://something.com/whatever而不是:3000.如果没有,passenger-memory-stats看看是否有像这样的条目908 469.7 MB 90.9 MB Passenger RackApp: /var/www/html/projectname

我可能错过了一些令人发指的事情,但这在过去对我有用.

  • 我觉得这个答案应该迁移到stackoverflow文档. (2认同)

Dan*_*vin 61

如果你在Passenger上运行,那么默认是在生产中运行,在你的apache conf中:

<VirtualHost *:80>
  ServerName application_name.rails.local
  DocumentRoot "/Users/rails/application_name/public"
  RailsEnv production ## This is the default
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

如果您只是使用mongrel或webrick运行本地服务器,则可以执行以下操作:

./script/server -e production
Run Code Online (Sandbox Code Playgroud)

或者在bash中:

RAILS_ENV=production ./script/server
Run Code Online (Sandbox Code Playgroud)

实际上覆盖enviornment.rb中的RAILS_ENV常量应该是你的最后手段,因为它可能不会保持设置(请参阅我给出的另一个答案)


Pet*_*ete 54

如果mipadi的建议不起作用,请将其添加到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'
Run Code Online (Sandbox Code Playgroud)


mip*_*adi 19

将环境变量更改RAILS_ENVproduction.

  • 环境变量位于何处? (3认同)

Evo*_*lve 19

$> export RAILS_ENV=production
Run Code Online (Sandbox Code Playgroud)


foz*_*foz 18

您还可以将环境传递给脚本/服务器:

$ script/server -e production
Run Code Online (Sandbox Code Playgroud)


pra*_*ann 12

rails s -e production
Run Code Online (Sandbox Code Playgroud)

这将使用RAILS_ENV= 运行服务器'production'.

除此之外,你必须设置资产路径 production.rb

config.serve_static_assets = true
Run Code Online (Sandbox Code Playgroud)

没有这个,您的资产将不会被加载.


pun*_*t18 7

RAILS_ENV=production rails s
Run Code Online (Sandbox Code Playgroud)

要么

rails s -e production
Run Code Online (Sandbox Code Playgroud)

默认环境是发展.


ale*_*1sz 5

正如其他人发布的: rails server -e production

或者,我个人最喜欢的: RAILS_ENV=production rails s