Skip to content

Instantly share code, notes, and snippets.

@pacohigh
Forked from PelagicDev/puma_systemd_setup.md
Last active May 16, 2020 10:35
Show Gist options
  • Save pacohigh/8da4c2dc638e4278546ba13d63151ccd to your computer and use it in GitHub Desktop.
Save pacohigh/8da4c2dc638e4278546ba13d63151ccd to your computer and use it in GitHub Desktop.
Ubuntu Rails & Puma Server setup

Follow this guide until it gets to the upstart parts and then follow the steps below (https://github.com/puma/puma/blob/master/docs/systemd.md):

Setup Rails App:

Add to Gemfile:

group :development do
  gem 'capistrano', '~> 3.6'
  gem 'capistrano-rails', '~> 1.1'
  gem 'capistrano-rvm'
  gem 'capistrano3-puma'
  gem 'capistrano3-nginx'
  gem 'capistrano-upload-config'
end
  • run bundle install
  • run cap install
  • Add the following to Capfile.rb:
# Load DSL and set up stages
require "capistrano/setup"
 
# Include default deployment tasks
require "capistrano/deploy"
 
# Include capistrano-rails
require 'capistrano/rails'
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/migrations'
require 'capistrano/rails/assets'
require 'capistrano/nginx'
require 'capistrano/puma'
require 'capistrano/puma/nginx'
require 'capistrano/upload-config'
  • Edit config/deploy.rb:
# config valid only for current version of Capistrano
lock '3.6.0'
 
set :application, 'YOUR_APP_NAME'
set :repo_url, '<YOUR_GIT_REPOSITORY>'
# set :branch, 'master'
 
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
 
# Default deploy_to directory is /var/www/your_app_name
# set :deploy_to, '/var/www/your_app_name'
 
# Default value for :scm is :git
# set :scm, :git
 
# Default value for :format is :airbrussh.
# set :format, :airbrussh
 
# You can configure the Airbrussh format using :format_options.
# These are the defaults.
# set :format_options, command_output: true, log_file: 'log/capistrano.log', color: :auto, truncate: :auto
 
# Default value for :pty is false
# set :pty, true
 
# Default value for :linked_files is []
# append :linked_files, 'config/database.yml', 'config/secrets.yml'
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml', 'config/puma.rb')
 
# Default value for linked_dirs is []
# append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'public/system'
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads')
 
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
 
# Default value for keep_releases is 5
# set :keep_releases, 5
 
# Puma:
set :puma_conf, "#{shared_path}/config/puma.rb"
 
namespace :deploy do
  before 'check:linked_files', 'puma:config'
  before 'check:linked_files', 'puma:nginx_config'
  after 'puma:smart_restart', 'nginx:restart'
end

Now run the following command to generate template files: rails g capistrano:nginx_puma:config

Setup Production Server with Nginx & remove the default site

  • sudo apt-get install nginx
  • sudo rm /etc/nginx/sites-available/default

Create the puma.service

  • cd /etc/systemd/system/multi-user.target.wants/
  • run systemctl enable puma.service
  • edit the file, sudo vi puma.service, and add:
[Unit]
Description=Puma HTTP Server
After=network.target

# Uncomment for socket activation (see below)
# Requires=puma.socket

[Service]
Type=simple

User=deploy

WorkingDirectory=/home/deploy/apps/{APP_NAME}/current

ExecStart=/home/deploy/.rbenv/shims/bundle exec puma -C config/puma.rb
Restart=always

[Install]
WantedBy=multi-user.target
  • and now start the service: systemctl start puma.service

An other way to create the puma.service working with rvm

[Unit]
Description=Puma HTTP Server
After=network.target

# Uncomment for socket activation (see below)
# Requires=puma.socket

[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple

# Preferably configure a non-privileged user
#User=

# The path to the your application code root directory.
# Example /home/username/myapp
WorkingDirectory=/<YOUR_APP_FULLPATH>

# Helpful for debugging socket activation, etc.
# Environment=PUMA_DEBUG=1

# Alternatively, create a binstub with `bundle binstubs puma --path ./sbin` in the WorkingDirectory
ExecStart=/<FULLPATH>/puma -C /<YOUR_APP_FULLPATH>/puma.rb config.ru

# Variant: Rails start.
#ExecStart=/bin/bash -lc 'cd /<YOUR_APP_FULLPATH> && bundle exec puma -C /<YOUR_APP_FULLPATH>/puma.rb'

# Variant: Rails start.
# ExecStart=/<FULLPATH>/puma -C /<YOUR_APP_FULLPATH>/puma.rb config.ru

# Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub
# Variant: Specify directives inline.
# ExecStart=/<FULLPATH>/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem


Restart=always

[Install]
WantedBy=multi-user.target
  • and now start the service: systemctl start puma.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment