Skip to content

Instantly share code, notes, and snippets.

@clara101
Last active February 13, 2026 17:37
Show Gist options
  • Select an option

  • Save clara101/7881bc404cdca3daff47b46fa8ac1cbe to your computer and use it in GitHub Desktop.

Select an option

Save clara101/7881bc404cdca3daff47b46fa8ac1cbe to your computer and use it in GitHub Desktop.
Deploy Rails 4 to AWS EC2(Ubuntu 14.04.4 LTS). Nginx, Puma, Capistrano3, PostgreSQL, RVM.
Capistrano::Rails::Db
cap production deploy:db:abort_if_pending_migrations # Run rake db:abort_if_pending_migrations
cap production deploy:db:create # Run rake db:create
cap production deploy:db:drop # Run rake db:drop
cap production deploy:db:migrate # Run rake db:migrate Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)
cap production deploy:db:migrate:down # Run rake db:migrate:down Run the "down" for a given migration VERSION
cap production deploy:db:migrate:redo # Run rake db:migrate:redo Rollback the database one migration and re migrate up (options: STEP=x, VERSION=x)
cap production deploy:db:migrate:reset # Run rake db:migrate:reset Reset your database using your migrations
cap production deploy:db:migrate:status # Run rake db:migrate:status Display status of migrations
cap production deploy:db:migrate:up # Run rake db:migrate:up Run the "up" for a given migration VERSION
cap production deploy:db:reset # Run rake db:reset Drop and recreate the database from db/schema.rb and load the seeds
cap production deploy:db:rollback # Run rake db:rollback Roll the schema back to the previous version (specify steps w/ STEP=n)
cap production deploy:db:seed # Run rake db:seed Load the seed data from db/seed.rb
cap production deploy:db:setup # Run rake db:setup Create the database, load the schema, and initialize with the seed data
cap production deploy:db:version # Run rake db:version Retrieve the current schema version number
Capistrano::Nginx
nginx:setup # creates /etc/nginx/sites-available/APPLICATION.conf and links it to /etc/nginx/sites-enabled/APPLICATION.conf
nginx:stop # invokes service nginx stop on server
nginx:start # invokes service nginx start on server
nginx:restart # invokes service nginx restart on server
nginx:reload # invokes service nginx reload on server
nginx:force-reload # invokes service nginx force-reload on server
nginx:enable_site # creates symlink in sites-enabled directory
nginx:disable_site # removes symlink from sites-enabled directory
Capistrano::Rails::Console
cap production rails:console # This will add a task rails:console
cap production rails:console sandbox=1 # You can also start a sandbox session
For more commands run:
cap -T
root# apt-get update
root# apt-get upgrade
root# adduser deployer
root# gpasswd -a deployer sudo
root# nano /etc/ssh/sshd_config
root# PermitRootLogin no
root# PasswordAuthentication yes
root# service ssh restart
root# exit
ssh deployer@server
Note:
ssh-keygen
cat ssh
nano ssh-authorized-key
deployer$ sudo apt-get -y install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev
deployer$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
deployer$ \curl -sSL https://get.rvm.io | bash -s stable
deployer$ source /home/deployer/.rvm/scripts/rvm
deployer$ rvm install 2.3.0 && rvm use --default 2.3.0
deployer$ echo "gem: --no-document" > ~/.gemrc
deployer$ gem install bundler
deployer$ gem install rails -v 5.0.0.beta3
deployer$ curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
deployer$ sudo apt-get install -y nodejs
deployer$ sudo apt-get install software-properties-common python-software-properties
deployer$ sudo add-apt-repository ppa:nginx/stable
deployer$ sudo apt-get update
deployer$ sudo apt-get -y install nginx
deployer$ sudo rm /etc/nginx/sites-available/default
deployer$ export LANGUAGE=en_US.UTF-8
deployer$ export LANG=en_US.UTF-8
deployer$ export LC_ALL=en_US.UTF-8
deployer$ locale-gen en_US.UTF-8
deployer$ sudo dpkg-reconfigure locales
deployer$ sudo apt-get -y install postgresql postgresql-contrib libpq-dev
deployer$ sudo pg_createcluster 9.3 main --start
deployer$ sudo -u postgres createuser deployer -s
deployer$ sudo -u postgres psql
postgres=# \password deployer
postgres=# \q
postgres=# sudo -u postgres createdb -O urlshortner urlshortner_production
deployer$ wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma-manager.conf
deployer$ wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma.conf
deployer$ nano puma.conf
SET:
setuid deployer
setgid deployer
SAVE:
ctrl + x
y
enter
deployer$ sudo cp puma.conf puma-manager.conf /etc/init
deployer$ sudo touch /etc/puma.conf
deployer$ sudo sh -c 'echo "deployer ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/deployer'
ADD TO Gemfile:
group :development do
gem 'capistrano', '3.5.0'
gem 'capistrano-rvm'
gem 'capistrano-nginx'
gem 'capistrano3-puma'
gem 'capistrano-rails'
gem 'capistrano-rails-db'
gem 'capistrano-rails-console'
gem 'capistrano-upload-config'
gem 'sshkit-sudo'
end
RUN:
your_app$ bundle install
your_app$ cap install
EDIT config/deploy.rb AS ATTACHED deploy.rb AND CHANGE VARIABLES IN IT
----------------------------------------------------
#Copy deploy.rb file:
# config valid only for current version of Capistrano
lock '3.5.0'
set :repo_url, '[email protected]:test/testest.git'
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
set :user, 'deployer'
set :application, 'test'
set :rails_env, 'production'
server '00.00.00.000', user: "#{fetch(:user)}", roles: %w{app db web}, primary: true
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :pty, true
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml', 'config/puma.rb')
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads')
set :config_example_suffix, '.example'
set :config_files, %w{config/database.yml config/secrets.yml}
set :puma_conf, "#{shared_path}/config/puma.rb"
set :puma_rackup, -> { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock" #accept array for multi-bind
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"
set :puma_role, :app
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
set :puma_threads, [0, 8]
set :puma_workers, 0
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_preload_app, false
----------------------------------------------
EDIT Capfile AS ATTACHED Capfile
#Capfile
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/nginx'
require 'capistrano/rvm'
require 'capistrano/puma'
require 'capistrano/puma/nginx'
require 'capistrano/rails/migrations'
require 'capistrano/rails/db'
require 'capistrano/rails/console'
require 'capistrano/upload-config'
require 'sshkit/sudo'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
-------------------------------------------
#Production.rb
server '00.00.00.000', user: 'deployer', roles: %w{web app db}
-------------------------------------------
RUN:
your_app$ cp config/database.yml config/database.yml.example
your_app$ cp config/secrets.yml config/secrets.yml.example
your_app$ cap production config:init
your_app$ echo '/config/database.production.yml' >> .gitignore
your_app$ echo '/config/secrets.production.yml' >> .gitignore
EDIT WITH YOUR PARAMETERS:
/config/database.production.yml
/config/secrets.production.yml
RUN:
your_app$ rails g capistrano:nginx_puma:config
EDIT OR LEAVE AS IS:
config/deploy/templates/nginx_conf.erb
config/deploy/templates/puma.rb.erb
GIT COMMIT AND PUSH CHANGES
RUN:
your_app$ cap production deploy
CONGRATULATION! ALL DONE!
CHECK YOUR RUNNIG SERVER!
WAIT! ONE MORE THING TO DO.
FOR UPSTART PUMA SERVER ADD LINE WITH FULL PATH OF YOUR APP IN /etc/puma.conf.
RUN:
deployer$ sudo nano /etc/puma.conf
PASTE:
/home/deployer/apps/<<YOUR APP>>/current
SAVE:
ctrl + x
y
enter
FINALLY! YOU CAN RESTART SERVER TO CHECK PUMA AUTOSTART!
RUN:
deployer$ sudo shutdown -r now
@reuableahcim
Copy link
Copy Markdown

Love the way you managed puma.

@clara101
Copy link
Copy Markdown
Author

clara101 commented Mar 7, 2017

Thanks reuableahcim !!

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