如何使用配置文件运行Rails Puma服务器,使用命令'rails s puma'

15

我可以使用rails s puma或者puma在Rails中运行Puma服务器。

根据这个回答,运行rails s puma使服务器意识到Rails环境。它会显示运行puma的错误等信息。

我想像下面这样设置一个配置文件:

config/puma.rb

workers Integer(ENV['PUMA_WORKERS'] || 3)
threads Integer(ENV['MIN_THREADS']  || 1), Integer(ENV['MAX_THREADS'] || 16)

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

...

如果我运行puma -C config/puma.rb一切都可以工作。但是,如果我运行rails s puma,我无法弄清楚如何向puma提供选项。我已经尝试了以下方法:

rails s puma                     # Puma server works but no config file is passed in.
rails s puma -C config/puma.rb   # Invalid option -C
rails s puma -c config/puma.rb   # Undefined method 'workers'. So rails is
                                 # trying to use the config instead of puma?

根据 puma 文档,我还尝试将配置文件放置在 config/puma/development.rb

非常感谢您的帮助 :)

4个回答

13

7
我发现使用Foreman(https://github.com/ddollar/foreman)是一个不错的解决方法,并且还提供了额外的灵活性。
Heroku为这个写了一篇很好的指南(https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server)。以下是一个非常快速的入门步骤。 步骤1:安装Foreman。下面是Mac OS X的示例,完整的指南在Foreman网站上。
$ brew install foreman

第二步:将以下内容添加到您的Gemfile中:
gem 'puma'

步骤 3:创建一个名为 Procfile 的文件:

web: bundle exec puma -C config/puma.rb

步骤 4: 现在使用以下方式启动您的应用程序

$ foreman start

00:36:05 web.1  | started with pid 19869
00:36:05 web.1  | [19869] Puma starting in cluster mode...
00:36:05 web.1  | [19869] * Version 2.11.1 (ruby 2.2.1-p85), codename: Intrepid Squirrel
00:36:05 web.1  | [19869] * Min threads: 1, max threads: 1
00:36:05 web.1  | [19869] * Environment: development
00:36:05 web.1  | [19869] * Process workers: 1
00:36:05 web.1  | [19869] * Preloading application
00:36:07 web.1  | [19869] * Listening on tcp://0.0.0.0:3000
00:36:07 web.1  | [19869] Use Ctrl-C to stop
00:36:07 web.1  | [19869] - Worker 0 (pid: 19870) booted, phase: 0

Linux:跳过步骤1,在步骤2中将gem 'foreman'添加到Gemfile。 - Andre Figueiredo

2
很遗憾,您不能这样做。 今天我需要在我的开发环境中使用ssl来使puma正常工作,因此我编辑了我的Rails应用程序(Rails 5)中的config/puma.rb文件,并添加了以下内容:
ssl_bind '127.0.0.1', '3000', {
   key: 'path_to_you_key_file', #/Users/DevRuby/.ssh/server.key
   cert: 'path_to_yout_cert_file', #/Users/DevRuby/.ssh/server.crt
   verify_mode: 'none' #fix errors due to self-signed certificate
}

我在config/environments/development.rb中添加了以下行,以启用将日志发送到STDOUT:

config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))

现在我使用命令#puma来启动我的应用程序,而不是使用#rails s命令,因为它会加载config/puma.rb配置文件中的所有设置。


你好,如何使用Puma启动Rails?我尝试了这个方法,但是只有Puma在运行,Rails没有启动。 - EJAg

0

这只是软件,你可以控制 bin 目录中的 rails 可执行文件,只需更改它以执行你想要的操作并检查它即可。

bin/rails

#!/usr/bin/env ruby
APP_PATH = File.expand_path('../config/application', __dir__)

# We take over the rails s invocation so we can run puma from `rails s` 
# The maintainer refuses to let `rails server` pick up the config for puma
# so we have to call it directly: https://github.com/puma/puma/issues/512
command = ARGV.first&.downcase
exec('SERVER=puma puma') if command == 's' || command == 'server'

require_relative '../config/boot'
require 'rails/commands'

是的,这很丑陋,但你知道什么不丑陋?当所有Rails文档都说只需调用rails server时,记得直接调用Puma。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接