Skip to content

Instantly share code, notes, and snippets.

@christianrojas
Last active December 14, 2015 21:39
Show Gist options
  • Save christianrojas/5153047 to your computer and use it in GitHub Desktop.
Save christianrojas/5153047 to your computer and use it in GitHub Desktop.
Setup Ubuntu Server 12.10 64 bits / VPS / Nginx / Unicorn / Postfix / Add a deploy user / rbenv / Ruby 1.9.3-p392
production:
adapter: postgresql
encoding: unicode
database: ADD_DATABASE_NAME
pool: 5
host: ADD_HOST
port: ADD_PORT
username: ADD_USERNAME
password: ADD_PASSWORD
# encoding: UTF-8
require 'capistrano-rbenv'
require "bundler/capistrano"
server "SERVER-IP", :web, :app, :db, primary: true
set :default_environment, {
'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
}
set :application, "APP-NAME"
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]:christianrojas/#{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

VPS Setup Ubuntu 12.10

Update Box

sudo apt-get update
sudo apt-get upgrade -y

Install Dependecies

sudo apt-get install libxslt-dev libxml2-dev python-software-properties git-core vim curl nodejs npm zlib1g-dev build-essential libssl-dev libreadline-dev

Install Nginx

sudo apt-get install nginx -y

Start nginx

sudo /etc/init.d/nginx start

ImageMagick

sudo apt-get install libmagickwand-dev imagemagick

PostgreSQL

apt-get -y update
apt-get -y install postgresql-9.1 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

Add A User Account

sudo adduser deployer

Grant Root Priviliges To A User Account

sudo adduser deployer sudo
su deployer

rbenv

cd
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

ruby-build

mkdir -p ~/.rbenv/plugins
cd ~/.rbenv/plugins
git clone git://github.com/sstephenson/ruby-build.git
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

latest ruby (1.9.3-p448)

rbenv install 1.9.3-p448
rbenv global 1.9.3-p448
ruby -v
gem install bundler --no-ri --no-rdoc
gem install nokogiri
rbenv rehash

get to know github.com

Add in config/deploy.rb file

require 'capistrano-rbenv'

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

Add in Gemfile file

gem 'unicorn'
gem 'capistrano'
gem 'capistrano-rbenv'

after deploy:cold

sudo rm /etc/nginx/sites-enabled/default
sudo service nginx restart
sudo update-rc.d -f unicorn_blog defaults
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment