Skip to content

Instantly share code, notes, and snippets.

@gertig
Created March 31, 2012 20:26
Show Gist options
  • Save gertig/2268145 to your computer and use it in GitHub Desktop.
Save gertig/2268145 to your computer and use it in GitHub Desktop.
VPS Setup and Deployment (Railscasts.com #335)
When building a new instance of Amazon EC2 choose quick-start-1 as the security group not default
#LOCALLY
$ capify .
$ chmod +x config/unicorn_init.sh
$ git add .
$ git commit -m "deployment configs"
If rebuilding an instance don't forget to remove the ssh keys
$ ssh-keygen -R {server_name}
$ ssh-keygen -R {ip_address}
$ ssh-keygen -R server.example.com
Then ssh in as root
# Rackspace
$ ssh [email protected]
# Amazon prefers you to login as ubuntu on 10.04 LTS instead of as root
$ ssh -i key_name.pem [email protected]
Install Nginix
$ apt-get -y update
$ apt-get -y install python-software-properties
$ add-apt-repository ppa:nginx/stable
$ apt-get -y update
$ apt-get -y install nginx
$ service nginx start
Create a new user that will be used for deploying and for running the railsready script
## RACKSPACE
Add the user to the sudoers group or the admin group depending on which group exists, if you need to add a user to a group after creating them use $ useradd -G sudoers deployer
$ adduser deployer
Add the user to sudoers
$ vim /etc/sudoers
# add the following line below "root ALL=(ALL) ALL" :
# deployer ALL=(ALL) ALL
# save file and exit Esc then [Shift - ; - w - !] to force save a readonly file then [Shfit - ; - q] to quit VIM
#disable root login from ssh, so nobody is able to brute force a root login
vim /etc/ssh/sshd_config
#uncomment "PermitRootLogin yes" and change it to "PermitRootLogin no"
/etc/init.d/sshd restart
## AMAZON
$ sudo adduser deployer --ingroup admin
# or leave out --ingroup if the user already exists
# To use the same ssh key for both accounts. Do the following
$ sudo cp -r /home/ubuntu/.ssh /home/deployer/
cd /home/deployer
sudo chown -R deployer:deployer .ssh
#logout and login or su to your new user
$ su deployer
$ cd
Run Railsready script as the deployer user
$ wget --no-check-certificate https://raw.github.com/joshfng/railsready/master/railsready.sh && bash railsready.sh
#Locally
$ cap deploy:setup
$ cap deploy
Delete default nginix welcome screen
$ sudo rm /etc/nginx/sites-enabled/default
$ sudo service nginx restart
$ sudo update-rc.d -f unicorn_api-proxy defaults
apt-get -y update
apt-get -y install curl git-core python-software-properties
# nginx
add-apt-repository ppa:nginx/stable
apt-get -y update
apt-get -y install nginx
service nginx start
# PostgreSQL
add-apt-repository ppa:pitti/postgresql
apt-get -y update
apt-get -y install postgresql libpq-dev
sudo -u postgres psql
# \password
# create user blog with password 'secret';
# create database blog_production owner blog;
# \q
# Postfix
apt-get -y install telnet postfix
# Node.js
add-apt-repository ppa:chris-lea/node.js
apt-get -y update
apt-get -y install nodejs
# Add deployer user
adduser deployer --ingroup admin
su deployer
cd
# rbenv
curl -L https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash
vim .bashrc # add rbenv to the top
. .bashrc
rbenv bootstrap-ubuntu-10-04
rbenv install 1.9.3-p125
rbenv global 1.9.3-p125
gem install bundler --no-ri --no-rdoc
rbenv rehash
# get to know github.com
ssh [email protected]
# after deploy:cold
sudo rm /etc/nginx/sites-enabled/default
sudo service nginx restart
sudo update-rc.d -f unicorn_blog defaults
rails new blog -d postgresql
rails g scaffold article name content:text
# setup Git
mate .gitignore
cp config/database.yml config/database.example.yml
git init
git add .
git commit -m "initial commit"
git remote add origin [email protected]:ryanb/blog.git
git push
# add Capistrano, Unicorn, and nginx config
bundle
capify .
chmod +x config/unicorn_init.sh
git add .
git commit -m "deployment configs"
# ssh setup
cat ~/.ssh/id_rsa.pub | ssh [email protected] 'cat >> ~/.ssh/authorized_keys'
ssh-add # -K on Mac OS X
# deployment
cap deploy:setup
# edit /home/deployer/apps/blog/shared/config/database.yml on server
cap deploy:cold
gem 'unicorn'
gem 'capistrano'
require "bundler/capistrano"
server "72.14.183.209", :web, :app, :db, primary: true
set :application, "blog"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, "git"
set :repository, "[email protected]:ryanb/#{application}.git"
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
after "deploy", "deploy:cleanup" # keep only the last 5 releases
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/unicorn_#{application} #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
run "mkdir -p #{shared_path}/config"
put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment