Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Deploy Rails 5.0.0.beta3 to VPS(Ubuntu 14.04.4 LTS). Nginx, Puma, Capistrano3, PostgreSQL, RVM.
<<APP>> change this variables
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# service ssh restart
root# exit
ssh deployer@server
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 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
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
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 Capfile AS ATTACHED Capfile
EDIT config/deploy.rb AS ATTACHED deploy.rb AND CHANGE VARIABLES IN IT
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
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/nginx'
require 'capistrano/puma'
require 'capistrano/puma/nginx'
require 'capistrano/rvm'
require 'capistrano/rails'
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 }
# config valid only for current version of Capistrano
lock '3.5.0'
set :repo_url, '<<YOUR APP REPO>>'
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
set :user, 'deployer'
set :application, '<<YOUR APPNAME>>'
set :rails_env, 'production'
server '<<YOUR SERVER>>', 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"
namespace :deploy do
before 'check:linked_files', 'config:push'
before 'check:linked_files', 'puma:config'
before 'check:linked_files', 'puma:nginx_config'
before 'deploy:migrate', 'deploy:db:create'
after 'puma:smart_restart', 'nginx:restart'
end
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
Using ENV variables:
1) store variables in /etc/environment
2) uncomment line in /etc/init/puma.conf
#. /etc/environment
Upstart on ubuntu 15+:
deployer$ sudo apt-get install upstart-sysv
deployer$ sudo update-initramfs -u
deployer$ reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.