将 Rails 应用程序更改为生产环境
如何更改我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
现在这将是
或者,更紧凑
它适用于 Rails 3+ 项目。
This would now be
Or, more compact
It works for rails 3+ projects.
如何使用 Apache 和 Phusion Passenger 在生产模式下(逐步)设置和运行 Rails 4 应用程序:
通常,您可以进入 Rails 项目
rails s
并获得开发信息您的应用版本位于 http://something.com:3000。生产模式的配置有点棘手。我已经搞砸了一段时间了,所以我想我应该为新手(比如我自己)写这篇文章。有一些小调整在互联网上传播,并且认为这可能会更容易。
请参阅本指南了解服务器的核心设置(CentOS 6,但它应该适用于几乎所有 Linux 版本):https://www.digitalocean.com/community/tutorials/how-to-setup -a-rails-4-app-with-apache-and-passenger-on-centos-6
绝对确定在设置 Passenger 后您已编辑
/etc/httpd/conf/httpd.conf
文件以反映您的目录结构。 您希望将 DocumentRoot 指向 Rails 项目 /public 文件夹httpd.conf
文件中具有此类目录的任何位置:/var/www/html/ your_application/public
需要更新,否则一切都会变得非常令人沮丧。我不能再强调这一点。重新启动服务器(或至少重新启动 Apache -
service httpd restart
)输入您的 Rails 项目文件夹
/var/www/html/your_application
并使用rake db:migrate
开始迁移。确保数据库表存在,即使您计划稍后添加表(这也是步骤 1 的一部分)。RAILS_ENV=生产 rake Secret
- 这将创建一个可以添加到config/secrets.yml
的 Secret_key。您可以将其复制/粘贴到 config/secrets.yml 中以便运行,但我建议您不要这样做。就我个人而言,我执行此步骤是为了确保其他一切正常工作,然后将其更改回来并稍后获取它。RAILS_ENV=生产 rake db:migrate
RAILS_ENV=生产 rake 资产:预编译 如果您正在提供静态资产。这会将 js、css、图像文件推送到
/public
文件夹中。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.
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
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 thehttpd.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.Reboot the server (or Apache at the very least -
service httpd restart
)Enter your Rails project folder
/var/www/html/your_application
and start the migration withrake db:migrate
. Make certain that a database table exists, even if you plan on adding tables later (this is also part of step 1).RAILS_ENV=production rake secret
- this will create a secret_key that you can add toconfig/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.RAILS_ENV=production rake db:migrate
RAILS_ENV=production rake assets:precompile
if you are serving static assets. This will push js, css, image files into the/public
folder.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 like908 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.
如果您在 Passenger 上运行,那么默认情况下是在生产环境中运行,在您的 apache 配置中:
如果您'如果您只是使用 mongrel 或 webrick 运行本地服务器,您可以这样做:
或在 bash 中:
实际上覆盖 eniornment.rb 中的 RAILS_ENV 常量可能应该是您的最后手段,因为它可能不会保持设置状态(请参阅 我对此给出的另一个答案 )
If you're running on Passenger, then the default is to run in production, in your apache conf:
If you're just running a local server with mongrel or webrick, you can do:
or in bash:
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)
如果mpadi的建议不起作用,请将其添加到配置中/环境.rb
If mipadi's suggestion doesn't work, add this to config/environment.rb
将环境变量
RAILS_ENV
更改为生产
。Change the environment variable
RAILS_ENV
toproduction
.您还可以将环境传递给脚本/服务器:
You can also pass the environment to script/server:
这将使用
RAILS_ENV
='product'
运行服务器。除此之外,您还必须在
生产.rb
中设置资产路径,否则您的资产将不会被加载。
This will run the server with
RAILS_ENV
='production'
.Apart from this you have to set the assets path in
production.rb
Without this your assets will not be loaded.
或者
默认环境是开发。
OR
By default environment is developement.
正如其他人发布的:
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
在 Rails 3 中
,将
Rails.env = ActiveSupport::StringInquirer.new('product')
添加到 application.rb 和rails 中s
的工作方式与rails server -e production
相同In Rails 3
Adding
Rails.env = ActiveSupport::StringInquirer.new('production')
into the application.rb andrails s
will work same asrails server -e production
通过“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
对于默认服务器: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
默认情况下,服务器在开发环境中运行:
$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
请确保您已在environment.rb 文件中完成以下操作。
如果您的应用程序在共享托管环境或 phushion乘客中运行,您可能需要在 .httaccess (公共文件夹内)中进行更改并将模式设置为生产。
Please make sure you have done below in your environment.rb file.
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.