将 Rails 应用程序更改为生产环境

发布于 2024-08-16 05:31:36 字数 69 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

花想c 2024-08-23 05:31:36

现在这将是

rails server -e production

或者,更紧凑

rails s -e production

它适用于 Rails 3+ 项目。

This would now be

rails server -e production

Or, more compact

rails s -e production

It works for rails 3+ projects.

野生奥特曼 2024-08-23 05:31:36

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

通常,您可以进入 Rails 项目 rails s 并获得开发信息您的应用版本位于 http://something.com:3000。生产模式的配置有点棘手。

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

  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 文件中具有此类目录的任何位置:/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=生产 rake Secret - 这将创建一个可以添加到 config/secrets.yml 的 Secret_key。您可以将其复制/粘贴到 config/secrets.yml 中以便运行,但我建议您不要这样做。就我个人而言,我执行此步骤是为了确保其他一切正常工作,然后将其更改回来并稍后获取它。

  6. RAILS_ENV=生产 rake db:migrate

  7. RAILS_ENV=生产 rake 资产:预编译 如果您正在提供静态资产。这会将 js、css、图像文件推送到 /public 文件夹中。

  8. RAILS_ENV=生产轨道 s

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

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

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

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.

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.conf file to reflect your directory structure. You want to point DocumentRoot to your Rails project /public folder Anywhere in the httpd.conf file that has this sort of dir: /var/www/html/your_application/public needs 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_application and 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 can copy/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:precompile if you are serving static assets. This will push js, css, image files into the /public folder.

  8. RAILS_ENV=production rails s

At this point your app should be available at http://something.com/whatever instead of :3000. If not, passenger-memory-stats and see if there an entry like 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.

美男兮 2024-08-23 05:31:36

如果您在 Passenger 上运行,那么默认情况下是在生产环境中运行,在您的 apache 配置中:

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

如果您'如果您只是使用 mongrel 或 webrick 运行本地服务器,您可以这样做:

./script/server -e production

或在 bash 中:

RAILS_ENV=production ./script/server

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

If you're running on Passenger, then the default is to run in production, in your 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:

./script/server -e production

or in 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 answer I gave on that)

夜空下最亮的亮点 2024-08-23 05:31:36

如果mpadi的建议不起作用,请将其添加到配置中/环境.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'

If mipadi's suggestion doesn't work, add this to 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'
夜灵血窟げ 2024-08-23 05:31:36

将环境变量RAILS_ENV更改为生产

Change the environment variable RAILS_ENV to production.

囚你心 2024-08-23 05:31:36
gt; export RAILS_ENV=production
gt; export RAILS_ENV=production
烟酒忠诚 2024-08-23 05:31:36

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

$ script/server -e production

You can also pass the environment to script/server:

$ script/server -e production
死开点丶别碍眼 2024-08-23 05:31:36
rails s -e production

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

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

config.serve_static_assets = true

否则您的资产将不会被加载。

rails s -e production

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

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

config.serve_static_assets = true

Without this your assets will not be loaded.

唔猫 2024-08-23 05:31:36
RAILS_ENV=production rails s

或者

rails s -e production

默认环境是开发。

RAILS_ENV=production rails s

OR

rails s -e production

By default environment is developement.

幽蝶幻影 2024-08-23 05:31:36

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

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

As others have posted: rails server -e production

Or, my personal fave: RAILS_ENV=production rails s

给不了的爱 2024-08-23 05:31:36

Rails 3

,将 Rails.env = ActiveSupport::StringInquirer.new('product') 添加到 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

In Rails 3

Adding Rails.env = ActiveSupport::StringInquirer.new('production') into the application.rb and rails s will work same as 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
相守太难 2024-08-23 05:31:36

通过“rails server -e production”在生产环境中运行rails server并不是一种好方法,因为这样rails作为单线程应用程序运行,并且一次只能响应一个HTTP请求。

关于 Rails 生产环境的最佳文章是 生产环境 - Rails 3

It is not a good way to run rails server in production environment by "rails server -e production", because then rails runs as a single-threaded application, and can only respond to one HTTP request at a time.

The best article about production environment for rails is Production Environments - Rails 3

定格我的天空 2024-08-23 05:31:36

对于默认服务器:rails s -e production

对于costum服务器端口:rails s -p [port] -e production,例如。导轨 s -p 3002 -e 生产

for default server : rails s -e production

for costum server port : rails s -p [port] -e production, eg. rails s -p 3002 -e production

情未る 2024-08-23 05:31:36

默认情况下,服务器在开发环境中运行:$rails s

如果在生产环境中运行:$rails s -e production$ RAILS_ENV=productrails s

By default server runs on development environment: $ rails s

If you're running on production environment: $ rails s -e production or $ RAILS_ENV=production rails s

勿忘初心 2024-08-23 05:31:36

请确保您已在environment.rb 文件中完成以下操作。

ENV['RAILS_ENV'] ||= '生产'

如果您的应用程序在共享托管环境或 phushion乘客中运行,您可能需要在 .httaccess (公共文件夹内)中进行更改并将模式设置为生产。

Please make sure you have done below in your environment.rb file.

ENV['RAILS_ENV'] ||= 'production'

If you application runs in shared hosting environment or phushion passenger, you might need to need make changes in .httaccess (inside public folder) and set mode as production.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文