问题
I am able to run a puma server in rails using either rails s puma
or just puma
.
According to this answer, running rails s puma
makes the server aware of the rails environment. It shows server errors etc that running puma
alone does not.
I want to set a config file like so:
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'
...
If I run puma -C config/puma.rb
everything works. However if I run rails s puma
I can't work out how to give options to puma. I have tried the following:
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?
I have also tried putting the config file at config/puma/development.rb
as per the puma docs.
Appreciate any help on this :)
回答1:
It is not possible to use rails s puma
to load your puma configuration file, as confirmed here https://github.com/puma/puma/issues/512, you might want to take a look at a similar question here How do I get 'puma' to start, automatically, when I run `rails server` (like Thin does) where this is discussed
回答2:
I have found that using Foreman (https://github.com/ddollar/foreman) is a nice workaround for this, and gives additional flexibility as well.
Heroku has written a nice guide for this ( https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server ). A very quick getting started is below.
Step 1: Install Foreman. Example for Mac OS X below, full guide on the Foreman site
$ brew install foreman
Step 2: Add this to your Gemfile:
gem 'puma'
Step 3: Create a file called Procfile:
web: bundle exec puma -C config/puma.rb
Step 4: Now start your application by using
$ 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
回答3:
Unfortunately you cannot. Today I had to get Puma working with ssl on my dev environment, so I edited the file config/puma.rb in my rails application(Rails 5) and added:
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
}
And added to my config/environments/development.rb the following line to enable logs to be sent to STDOUT:
config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
And instead of starting my application using #rails s , I'm using now the command #puma which loads all the settings in the config/puma.rb configuration file.
来源:https://stackoverflow.com/questions/25225444/how-to-run-rails-puma-server-with-config-file-using-rails-s-puma