2

I'm using Rails 5 and the Puma 3 gam on CentOS. Is it possible (and how?) to specify the user and group under which puma runs when it is started? I need to start it with the appropriate user/group so taht my nginx server can connect to it and avoid the

2018/02/26 16:06:47 [crit] 11984#0: *1 connect() to unix:///home/rails/myproject/shared/sockets/puma.sock failed (13: Permission denied) while connecting to upstream, client: 50.244.40.27, server: server_ip, request: "GET / HTTP/1.1", upstream: "http://unix:///home/rails/myproject/shared/sockets/puma.sock:/", host: "server_ip"

errors I'm getting now. I'm starting my puma server like so

[rails@server myproject]$ puma -C config/puma.rb -e production -d

Below is my config/puma.rb file

[rails@server myproject]$ cat config/puma.rb
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum, this matches the default thread size of Active Record.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests, default is 3000.
#
port        ENV.fetch("PORT") { 3000 }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
workers ENV.fetch("WEB_CONCURRENCY") { 4 }

app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"

# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env

# Set up socket location
bind "unix://#{shared_dir}/sockets/puma.sock"

# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true

# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.
#
# preload_app!

# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted this block will be run, if you are using `preload_app!`
# option you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, Ruby
# cannot share connections between processes.
#
on_worker_boot do
  require "active_record"
  ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
  ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
Dave
  • 185
  • 1
  • 7
  • 20
  • Check your audit log. – Michael Hampton Feb 26 '18 at 21:22
  • My audit log will tell me how to set the user/group under which puma starts? – Dave Feb 26 '18 at 21:52
  • First, you need to find out why you get the permission denied error. At the moment you don't even know if setting a user and group will fix the problem (it won't, so you will have to actually fix the problem anyway in addition to setting a user/group). – Michael Hampton Feb 26 '18 at 22:51
  • My issue is definitely permission/user/group related. If I change my nginx user to run as the user "rails", then everything works. However, that will break other applications. So I'd rather change the puma user to run in my "deploy" group but I don't know how to do that. – Dave Feb 26 '18 at 23:32
  • You set `User=` and `Group=` to your desired user and group in your systemd unit. – Michael Hampton Feb 26 '18 at 23:44

0 Answers0