Ruby-on-rails 将 Rails 应用程序更改为生产

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1949229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:04:13  来源:igfitidea点击:

Change a Rails application to production

ruby-on-railsdevelopment-environmentproduction-environment

提问by Adler Medrado

How can I change my Rails application to run in production mode? Is there a config file, environment.rb for example, to do that?

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

采纳答案by etusm

How to setup and run a Rails 4 app in Production mode (step-by-step) using Apache and Phusion Passenger:

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

Normally you would be able to enter your Rails project, rails s, and get a development version of your app at http://something.com:3000. Production mode is a little trickier to configure.

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

I've been messing around with this for a while, so I figured I'd write this up for the newbies (such as myself). There are a few little tweaks which are spread throughout the internet and figured this might be easier.

我一直在搞这个,所以我想我会为新手(比如我自己)写这篇文章。有一些小的调整在整个互联网上传播,并认为这可能更容易。

  1. Refer to this guide for core setup of the server (CentOS 6, but it should apply to nearly all Linux flavors): https://www.digitalocean.com/community/tutorials/how-to-setup-a-rails-4-app-with-apache-and-passenger-on-centos-6

  2. Make absolute certain that after Passenger is set up you've edited the /etc/httpd/conf/httpd.conffile to reflect your directory structure. You want to point DocumentRoot to your Rails project /public folderAnywhere in the httpd.conffile that has this sort of dir: /var/www/html/your_application/publicneeds to be updated or everything will get very frustrating. I cannot stress this enough.

  3. Reboot the server (or Apache at the very least - service httpd restart)

  4. Enter your Rails project folder /var/www/html/your_applicationand start the migration with rake db:migrate. Make certain that a database table exists, even if you plan on adding tables later (this is also part of step 1).

  5. RAILS_ENV=production rake secret- this will create a secret_key that you can add to config/secrets.yml. You cancopy/paste this into config/secrets.yml for the sake of getting things running, although I'd recommend you don't do this. Personally, I do this step to make sure everything else is working, then change it back and source it later.

  6. RAILS_ENV=production rake db:migrate

  7. RAILS_ENV=production rake assets:precompileif you are serving static assets. This will push js, css, image files into the /publicfolder.

  8. RAILS_ENV=production rails s

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

  2. 确保在设置Passenger 后您已经编辑了/etc/httpd/conf/httpd.conf文件以反映您的目录结构。您想将 DocumentRoot 指向您的 Rails 项目 /public 文件夹httpd.conf具有此类 dir: 的文件中的任何地方:/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

At this point your app should be available at http://something.com/whateverinstead of :3000. If not, passenger-memory-statsand see if there an entry like 908 469.7 MB 90.9 MB Passenger RackApp: /var/www/html/projectname

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

I've probably missed something heinous, but this has worked for me in the past.

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

回答by BandsOnABudget

This would now be

这将是

rails server -e production

Or, more compact

或者,更紧凑

rails s -e production

It works for rails 3+ projects.

它适用于 rails 3+ 项目。

回答by Dan McNevin

If you're running on Passenger, then the default is to run in production, in your apache conf:

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

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

If you're just running a local server with mongrel or webrick, you can do:

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

./script/server -e production

or in bash:

或在 bash 中:

RAILS_ENV=production ./script/server

actually overriding the RAILS_ENV constant in the enviornment.rb should probably be your last resort, as it's probably not going to stay set (see another answerI gave on that)

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

回答by Pete

If mipadi's suggestiondoesn't work, add this to config/environment.rb

如果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'

回答by mipadi

Change the environment variable RAILS_ENVto production.

将环境变量更改RAILS_ENVproduction.

回答by Evolve

$> export RAILS_ENV=production

回答by foz

You can also pass the environment to script/server:

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

$ script/server -e production

回答by Prasanna

rails s -e production

This will run the server with RAILS_ENV= 'production'.

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

Apart from this you have to set the assets path in production.rb

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

config.serve_static_assets = true

Without this your assets will not be loaded.

没有这个,你的资产将不会被加载。

回答by puneet18

RAILS_ENV=production rails s

OR

或者

rails s -e production

By default environment is developement.

默认环境是开发。

回答by alex1sz

As others have posted: rails server -e production

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

Or, my personal fave: RAILS_ENV=productionrails s

或者,我个人的最爱: RAILS_ENV=productionrails s