Skip to content

Instantly share code, notes, and snippets.

@PelagicDev
Last active February 25, 2023 20:53
Show Gist options
  • Save PelagicDev/2e3079bd0f1df80f2826dde6b44d3e96 to your computer and use it in GitHub Desktop.
Save PelagicDev/2e3079bd0f1df80f2826dde6b44d3e96 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 (http://codepany.com/blog/rails-5-puma-capistrano-nginx-jungle-upstart/):

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, 'snowcookie_mvp_backend'
set :repo_url, 'git@bitbucket.org:snowcookieMath/sc_backend_mvp.git'
# set :branch, 'master'
 
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
 
# Default deploy_to directory is /var/www/my_app_name
# set :deploy_to, '/var/www/my_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
@deHelden
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment