15

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 :)

Community
  • 1
  • 1
Subtletree
  • 3,169
  • 2
  • 18
  • 25

4 Answers4

13

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

Community
  • 1
  • 1
Robert Christopher
  • 4,940
  • 1
  • 20
  • 21
7

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
Chris Peters
  • 17,918
  • 6
  • 49
  • 65
Jimmy S
  • 117
  • 1
  • 3
2

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.

  • Hi how do you start rails from puma? I tried this but with #puma only puma is running, rails won't. – EJAg Jul 11 '17 at 14:04
0

It's just software, you control the rails executable in your bin directory, just change it to do what you want and check it in.

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'

Yeah it's ugly but you know what is not ugly, having to remember to call puma directly when all the rails docs say just call rails server.

trcarden
  • 881
  • 1
  • 9
  • 17