简体   繁体   中英

Using Puma with Rails application

My configuration:

Rails 5.2.3
Ruby 2.6.3
rbenv
puma
capistrano
nginx
Ubuntu 18.04

I've been reading up on how to get all this setup and configured for deployment, but I don't seem to be able to find one place, that's got a clear and complete guide.

One of the things that confuse me the most is Puma. Some of the guides, do not mention anything about configuring Puma on the server, and all the instructions, reference the Puma Gem. A couple of other guides, discuss setting up and configuring Puma on the server, and modifications in the /etc/init folder.

Do I need to setup and configure Puma on the server, separately, before I can start using it for my Rails application?

No you do not need to set up and configure Puma on the server. Puma is a gem. So if you list it in your application's Gemfile, when you run bundle install for your Rails application, Puma will be installed automatically.

Any configuration for puma should go in your application, under config/puma.rb .

Does this answer your question?

When running Rails locally, just use config/puma.rb for your Puma config, and run Puma similar to the following:

RAILS_LOG_TO_STDOUT=1 bin/pumactl -F config/puma.rb start

The above will run Puma in the foreground. I personally use Overmind in development to run Puma, Webpacker, Sidekiq, and sometimes ngrok. That means I only need one iTerm tab for all of those.

To get bin/pumactl , run bin/bundle binstubs puma .

You could also run Puma using bin/rails server , but Puma recommends not doing that because not all of Puma's configuration options are available through that method.

A sample Puma config (based on what the capistrano3-puma gem produces [ https://github.com/seuros/capistrano-puma/blob/v4.0.0/lib/capistrano/templates/puma.rb.erb] ):

# frozen_string_literal: true
# config/puma.rb

require 'dotenv/load' # load a .env file for environment variables; gem 'dotenv-rails'
require 'nenv' # nicer ENV handling; gem 'nenv'

directory(File.expand_path('..', __dir__))
rackup(File.expand_path('../config.ru', __dir__))
environment(Nenv.rack_env || Nenv.rails_env || 'development')
# or without nenv
#environment(ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development')

threads(0, (Nenv.max_threads || 16).to_i)
# or without nenv
#threads(0, (ENV['MAX_THREADS'] || 16).to_i)

port((Nenv.port || 3_000).to_i)
# or without nenv
#port((ENV['PORT'] || 3_000).to_i)

workers 0

restart_command 'bundle exec puma'

prune_bundler

on_restart do
  puts('Refreshing Gemfile...') # rubocop:disable Rails/Output
  Nenv.bundle_gemfile = File.expand_path('../Gemfile', __dir__)
  # or without nenv
  #ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', __dir__)
end

For handling Puma using Capistrano, I recommend the capistrano3-puma gem ( https://github.com/seuros/capistrano-puma ). This gem has various tasks, one of which is puma:config which will create a puma.rb file in the shared directory. You would then add puma.rb to the :linked_files Capistrano config option:

# config/deploy.rb
append(:linked_files, 'puma.rb')

As for setting up a server (one you deploy to), you want to install Nginx and set that up to serve your Rails app. Again, the capistrano3-puma gem can come in handy for this, as it includes an Nginx plugin which provides a task to upload a config to your server. If you don't want to use that plugin, then I'd at least recommend looking at the template ( https://github.com/seuros/capistrano-puma/blob/v4.0.0/lib/capistrano/templates/nginx_conf.erb ) for that config, and then adapting it to your needs.

As @matt-v-from-toronto mentioned, Puma is installed as part of your app, and not separately on the server. Doing a bin/bundle install , or similar, will install Puma along with all of your other gems listed in your Gemfile .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM