Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use puma's configuration file?

I was following this guide it documents the puma.rb file that is stored inside the app's config directory.

The guide is a bit flakey, but here's what I assume the puma.rb file does. Instead of running crazy commands such as this to get puma running on a specified socket:

bundle exec puma -e production -b unix:///var/run/my_app.sock

You can just specify the port, pid, session and other parameters in the puma.rb file like this:

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

threads 4,4

bind  "/home/starkers/Documents/alpha/tmp/socket"
pidfile "/home/starkers/Documents/alpha/tmp/pid"
state_path "/home/starkers/Documents/alpha/tmp/state"

activate_control_app

And then you could cd into the app's root and run a simple command like

'puma'

and the parameters set in puma.rb would be followed. Unfortunately that doesn't seem to work for me.

At least, I ran puma inside the root of a tiny test app, and no .sock file appeared in /home/starkers/Documents/alpha/tmp/sockets so does that mean it isn't working?

How do I get this working? I am on a local development machine, so could that cause this error somehow? Is there a parameter I need to pass in when running

puma ?

like image 201
Starkers Avatar asked Nov 13 '13 05:11

Starkers


People also ask

Where is Puma config file?

By default, if no configuration file is specified, Puma will look for a configuration file at config/puma. rb. If an environment is specified, either via the -e and --environment flags, or through the RACK_ENV environment variable, the default file location will be config/puma/environment_name. rb.

How do I start a puma server?

Puma has a built-in status and control app that can be used to query and control Puma. Puma will start the control server on localhost port 9293. All requests to the control server will need to include control token (in this case, token=foo ) as a query parameter. This allows for simple authentication.

How does PUMA work?

Puma is a threaded Ruby HTTP application server processing requests across a TCP and/or UNIX socket. Puma processes (there can be one or many) accept connections from the socket via a thread (in the Reactor class).


4 Answers

I was also stuck trying to find documentation on the config file for puma but I did find the all-in-one config.ru file useful. I've formatted it here for future reference:

# The directory to operate out of.
# The default is the current directory.

directory '/u/apps/lolcat'

# Load “path” as a rackup file.
# The default is “config.ru”.

rackup '/u/apps/lolcat/config.ru'

# Set the environment in which the rack's app will run. The value must be a string.
# The default is “development”.

environment 'production'

# Daemonize the server into the background. Highly suggest that
# this be combined with “pidfile” and “stdout_redirect”.
# The default is “false”.

daemonize
daemonize false

# Store the pid of the server in the file at “path”.

pidfile '/u/apps/lolcat/tmp/pids/puma.pid'

# Use “path” as the file to store the server info state. This is
# used by “pumactl” to query and control the server.

state_path '/u/apps/lolcat/tmp/pids/puma.state'

# Redirect STDOUT and STDERR to files specified. The 3rd parameter
# (“append”) specifies whether the output is appended, the default is
# “false”.

stdout_redirect '/u/apps/lolcat/log/stdout', '/u/apps/lolcat/log/stderr'
stdout_redirect '/u/apps/lolcat/log/stdout', '/u/apps/lolcat/log/stderr', true

# Disable request logging.
# The default is “false”.

quiet

# Configure “min” to be the minimum number of threads to use to answer
# requests and “max” the maximum.
# The default is “0, 16”.

threads 0, 16

# Bind the server to “url”. “tcp://”, “unix://” and “ssl://” are the only
# accepted protocols.
# The default is “tcp://0.0.0.0:9292”.

bind 'tcp://0.0.0.0:9292'
bind 'unix:///var/run/puma.sock'
bind 'unix:///var/run/puma.sock?umask=0777'
bind 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'

# Listens on port 7001
# The default is 9292
port 7001

# Instead of “bind 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'” you
# can also use the “ssl_bind” option.

 ssl_bind '127.0.0.1', '9292', { key: path_to_key, cert: path_to_cert }

# Code to run before doing a restart. This code should
# close log files, database connections, etc.

# This can be called multiple times to add code each time.

on_restart do
  puts 'On restart...'
end

# Command to use to restart puma. This should be just how to
# load puma itself (ie. 'ruby -Ilib bin/puma'), not the arguments
# to puma, as those are the same as the original process.

restart_command '/u/app/lolcat/bin/restart_puma'

# === Cluster mode ===

# How many worker processes to run.
# The default is “0”.

workers 2

# Code to run when a worker boots to setup the process before booting
# the app.
# This can be called multiple times to add hooks.

on_worker_boot do
  puts 'On worker boot...'
end

# === Puma control rack application ===

# Start the puma control rack application on “url”. This application can
# be communicated with to control the main server. Additionally, you can
# provide an authentication token, so all requests to the control server
# will need to include that token as a query parameter. This allows for
# simple authentication.

# Check out https://github.com/puma/puma/blob/master/lib/puma/app/status.rb
# to see what the app has available.

activate_control_app 'unix:///var/run/pumactl.sock'
activate_control_app 'unix:///var/run/pumactl.sock', { auth_token: '12345' }
activate_control_app 'unix:///var/run/pumactl.sock', { no_token: true }

Those settings would then go in a ruby file (e.g. config/puma.rb) and then as Starkers says, you can run it with

puma -C config/puma.rb

like image 180
Richard Nienaber Avatar answered Oct 18 '22 22:10

Richard Nienaber


Update: The original answer is no longer correct for Puma versions since 2019: Puma added a fallback mechanism, so both locations are checked now. ( https://github.com/puma/puma/pull/1885)

Puma first looks for configuration at config/puma/<environment_name>.rb, and then falls back to config/puma.rb.

Outdated answer:

If there is an environment defined - which is the case in your example - the configuration file is read from config/puma/[environment].rb and not config/puma.rb.

Just move your config/puma.rb to config/puma/production.rb and it should work.

Read the Puma documentation for more details: Configuration file

like image 24
Daniel Rikowski Avatar answered Oct 18 '22 22:10

Daniel Rikowski


This will work:

puma -C config/puma.rb
like image 6
Starkers Avatar answered Oct 18 '22 22:10

Starkers


You need to tell puma where to find your rackup file you can do it by putting this in your config:

rackup DefaultRackup

It looks like a fix for this is merged into master: https://github.com/puma/puma/pull/271

like image 1
Schneems Avatar answered Oct 18 '22 20:10

Schneems