Skip to content

Instantly share code, notes, and snippets.

@raykin
Forked from PavloBezpalov/1 Gist conventions
Last active April 6, 2017 07:39
Show Gist options
  • Save raykin/6a6263b43f2728e7fcad3065f1d68a38 to your computer and use it in GitHub Desktop.
Save raykin/6a6263b43f2728e7fcad3065f1d68a38 to your computer and use it in GitHub Desktop.
Deploy Rails 4.2.6 to VPS(Ubuntu 14.04.4 LTS). Nginx, Puma, Capistrano3, PostgreSQL.
<<USER>> <<APP>> change this variables
root# apt-get update
root# apt-get upgrade
root# adduser rails
root# gpasswd -a rails sudo
root# nano /etc/ssh/sshd_config
root# PermitRootLogin no
root# service ssh restart
root# exit
ssh rails@server
rails$ sudo dpkg-reconfigure tzdata
rails$ sudo apt-get install ntp
rails$ sudo fallocate -l 4G /swapfile
rails$ sudo chmod 600 /swapfile
rails$ sudo mkswap /swapfile
rails$ sudo swapon /swapfile
rails$ sudo sh -c 'echo "/swapfile none swap sw 0 0" >> /etc/fstab'
rails$ sudo apt-get 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
rails$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
rails$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
rails$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
rails$ exec $SHELL
rails$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
rails$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
rails$ exec $SHELL
rails$ rbenv install 2.3.3
rails$ rbenv global 2.3.3
rails$ echo "gem: --no-document" > ~/.gemrc
rails$ gem install bundler
rails$ gem install rails -v 5.0.1
rails$ rbenv rehash
root$ curl -sL https://deb.nodesource.com/setup_7.x | bash -
rails$ sudo apt-get install -y nodejs
rails$ export LANGUAGE=en_US.UTF-8
rails$ export LANG=en_US.UTF-8
rails$ export LC_ALL=en_US.UTF-8
rails$ locale-gen en_US.UTF-8
rails$ sudo dpkg-reconfigure locales
rails$ sudo add-apt-repository ppa:nginx/stable
rails$ sudo apt-get -y update
rails$ sudo apt-get -y install nginx
rails$ sudo rm /etc/nginx/sites-available/default
// we use a different way to install postgresl9.4 with bdr support
jessie is the codename of Debian8
follow https://wiki.postgresql.org/wiki/Apt add repository(don't install postgresl9.4 by apt-get at now)
follow http://bdr-project.org/docs/stable/installation-packages.html to install postgres9.4 with bdr support
/ rails$ sudo apt-get install postgresql postgresql-contrib libpq-dev
/ rails$ sudo pg_createcluster 9.3 main --start
rails$ sudo -u postgres psql
postgres=# \password
postgres=# create user <<USER>> with password '<<PASSWORD>>';
postgres=# ALTER USER <<USER>> CREATEDB;
postgres=# \q
### Deprecated
# is it really useful when using with capistrano?
# this seems not useful for debian 8
# even can't find /var/log/upstart in debina 8
# from https://en.wikipedia.org/wiki/Upstart we can read
# Debian decided that systemd will be the default init system beginning with the jessie release
rails$ wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma-manager.conf
rails$ wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma.conf
# what is it used for?
rails$ nano puma.conf
SET:
setuid rails
setgid rails
SAVE:
ctrl + x
y
enter
rails$ sudo cp puma.conf puma-manager.conf /etc/init
rails$ sudo touch /etc/puma.conf
ADD TO Gemfile:
gem 'puma', '~> 3.2'
group :development do
gem 'capistrano', '3.5.0'
gem 'capistrano-rbenv'
gem 'capistrano-nginx'
gem 'capistrano3-puma'
gem 'capistrano-upload-config'
gem 'capistrano-rails'
gem 'capistrano-rails-db'
gem 'capistrano-rails-console'
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:
rails$ sudo nano /etc/puma.conf
PASTE:
/home/rails/apps/<<YOUR APP>>/current
SAVE:
ctrl + x
y
enter
FINALLY! YOU CAN RESTART SERVER TO CHECK PUMA AUTOSTART!
RUN:
rails$ sudo shutdown -r now
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/nginx'
require 'capistrano/puma'
require 'capistrano/puma/nginx'
require 'capistrano/rbenv'
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>>'
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"
set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.3.0'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :rbenv_roles, :all # default value
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment