Skip to content

Instantly share code, notes, and snippets.

@carlosvazquez
Forked from PavloBezpalov/1 Gist conventions
Created May 14, 2016 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosvazquez/796a3fd1d3cca54d8a048d9b05969cf5 to your computer and use it in GitHub Desktop.
Save carlosvazquez/796a3fd1d3cca54d8a048d9b05969cf5 to your computer and use it in GitHub Desktop.
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
deployer$ sudo sh -c 'echo "deployer ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/deployer'
ADD TO Gemfile:
group :development do
gem 'capistrano', '~> 3.4'
gem 'capistrano-rvm', '~> 0.1.2'
gem 'capistrano3-nginx', '~> 2.1', '>= 2.1.4'
gem 'capistrano3-puma', '~> 1.2', '>= 1.2.1'
gem 'capistrano-rails', '~> 1.1', '>= 1.1.6'
gem 'capistrano-rails-db', '~> 0.0.2'
gem 'capistrano-rails-console', '~> 1.0', '>= 1.0.2'
gem 'capistrano-upload-config', '~> 0.7.0'
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'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
# config valid only for current version of Capistrano
lock '3.4.0'
set :repo_url, '<<YOUR APP REPO>>'
set :user, 'deployer'
set :application, '<<YOUR APPNAME>>'
server '<<YOUR SERVER>>', user: "#{fetch(:user)}", roles: %w{app db web}, primary: true
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
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 :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
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
cap production nginx:start # Start nginx service
cap production nginx:stop # Stop nginx service
cap production nginx:reload # Reload nginx service
cap production nginx:restart # Restart nginx service
cap production nginx:site:add # Creates the site configuration and upload it to the available folder
cap production nginx:site:disable # Disables the site removing the symbolic link located in the enabled folder
cap production nginx:site:enable # Enables the site creating a symbolic link into the enabled folder
cap production nginx:site:remove # Removes the site removing the configuration file from the available folder
cap production nginx:gzip_static # Compress all js and css files in :nginx_static_dir with gzip
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment