Skip to content

Instantly share code, notes, and snippets.

@pinoytech
Created March 23, 2012 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pinoytech/654ea77949b03585cef4 to your computer and use it in GitHub Desktop.
Save pinoytech/654ea77949b03585cef4 to your computer and use it in GitHub Desktop.
Server Setup NGINX/UNICORN/MYSQL
.idea
.DS_Store
load 'deploy'
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy' # remove this line to skip loading any of the default tasks
require "bundler/capistrano"
server "{yourdomainname/ip}", :web, :app, :db, primary: true
set :application, "yourappication"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, "git"
set :repository, "git@domain.net:username/#{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
#========= if you have no app yet START
# Make your app
# if you haven't done so, run this command without quotation marks:
# "cp config/database.yml config/database.example.yml"
# push change to git repo
#========= if you have no app yet END
# add gems/ uncomment lines, Capistrano and Unicorn on Gemfile
group :development do
gem 'capistrano'
end
# Use unicorn as the web server
gem 'unicorn'
# add Capistrano, Unicorn, and nginx config
bundle
capify .
##REPLACE {APP} with application name from deploy.rb on unicorn.rb, unicorn_init.sh, nginx.conf
##REPLACE {yourdomainname/ip} with your domain name or IP nginx.conf and deploy.rb
chmod +x config/unicorn_init.sh
git add .
git commit -m "deployment configs"
# ssh setup
cat ~/.ssh/id_rsa.pub | ssh deployer@yourdomainname.net 'cat >> ~/.ssh/authorized_keys'
ssh-add # -K on Mac OS X
# deployment
cap deploy:setup
# edit /home/deployer/apps/{APP}/shared/config/database.yml on server
#production:
# adapter: postgresql
# encoding: unicode
# database: {APP}_development
# pool: 5
# host: localhost
# username: postgres
# password: password
cap deploy:cold
upstream {APP} {
server unix:/tmp/unicorn.{APP}.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name {yourdomainname/ip};
root /home/deployer/apps/{APP}/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @{APP};
location @{APP} {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
sudo locale-gen
sudo dpkg-reconfigure locales
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
locale-gen en_US.UTF-8
dpkg-reconfigure locales
apt-get -y update
apt-get -y install curl git-core python-software-properties libmagickcore-dev libmagickcore4-extra libgraphviz-dev libgvc5 libmagickwand-dev imagemagick libxslt
apt-get -y install libxml2-dev libxslt1-dev bison openssl libreadline6 libreadline6-dev
apt-get -y install zlib1g zlib1g-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxslt-dev autoconf libc6-dev
apt-get -y install libopenssl-ruby libcurl4-openssl-dev libssl-dev openjdk-6-jre-headless libpcre3 libpcre3-dev
dpkg-reconfigure tzdata
add-apt-repository ppa:nginx/stable
apt-get -y update
apt-get -y install nginx
service nginx start
#====== MySQL ============
apt-get -y install mysql-server mysql-client libmysqlclient-dev libmysqlclient16-dev
mysql -u root -p
# create database {APP}_production;
# grant all on {APP}_production.* to {APP}@localhost identified by 'secret';
# exit
#=========================
#====== PostGres =========
# 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 {APP} with password 'secret';
# create database {APP}_production owner {APP};
# \q
#=========================
#====== for emails =======
# Postfix
apt-get -y install telnet postfix
#====== for emails =======
# 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 git@github.com
# get to know bitbucket.org
ssh git@bitbucket.org
# after deploy:cold
sudo rm /etc/nginx/sites-enabled/default
sudo service nginx restart
sudo update-rc.d -f unicorn_{APP} defaults
root = "/home/deployer/apps/{APP}/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"
listen "/tmp/unicorn.{APP}.sock"
worker_processes 2
timeout 30
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manage unicorn server
# Description: Start, stop, restart unicorn server for a specific application.
### END INIT INFO
set -e
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deployer/apps/{APP}/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=deployer
set -u
OLD_PIN="$PID.oldbin"
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}
run () {
if [ "$(id -un)" = "$AS_USER" ]; then
eval $1
else
su -c "$1" - $AS_USER
fi
}
case "$1" in
start)
sig 0 && echo >&2 "Already running" && exit 0
run "$CMD"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $OLD_PIN && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $OLD_PIN
then
echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
run "$CMD"
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment