Commit e3900aa9 by Giang Tran

Add deploy guide

parent 894fa076
upstream unicorn {
server unix:/tmp/unicorn.blog.sock fail_timeout=0;
}
server {
listen 80 default deferred;
# server_name example.com;
root /home/deployer/apps/blog/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @unicorn;
location @unicorn {
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;
}
# unicorn.rb sample file
rails_env = ENV['RAILS_ENV'] || 'production'
worker_processes 4
app_name = "airticket_api"
root = "/web/ventura/#{app_name}/current"
working_directory root
listen "#{root}/tmp/airticket_api.sock", :backlog => 64
listen 8088, :tcp_nopush => true
timeout 60
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/#{app_name}.stderr.log"
stdout_path "#{root}/log/#{app_name}.stdout.log"
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
check_client_connection false
before_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
\ No newline at end of file
#!/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/blog/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
# About this guide
This guide is to show you how to deploy your Rails project to server by Unicorn + Nginx.
This can be used with combination with deployment by Capistrano + Bundler.
# Prerequisite
# About this guide
This guide is to show you how to setup fast development environment by using Vagrant.
# About Vagrant
Read this page
https://docs.vagrantup.com/v2/getting-started/index.html
# Install Vagrant
Download Vagrant from
```
vagrant init centos-test-vm
vagrant up
vagrant ssh
```
Be careful of DNS
```
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
DNS1="8.8.8.8"
# Building box for your project
https://blog.engineyard.com/2014/building-a-vagrant-box
#!/usr/bin/env bash
# Verify Git is installed:
if [ ! $(which git) ]; then
echo "Git is not installed, can't continue."
exit 1
fi
if [ -z "${RBENV_ROOT}" ]; then
RBENV_ROOT="$HOME/.rbenv"
fi
# Install rbenv:
if [ ! -d "$RBENV_ROOT" ] ; then
git clone https://github.com/sstephenson/rbenv.git $RBENV_ROOT
else
cd $RBENV_ROOT
if [ ! -d '.git' ]; then
git init
git remote add origin https://github.com/sstephenson/rbenv.git
fi
git pull origin master
fi
# Install plugins:
PLUGINS=(
sstephenson/rbenv-vars
sstephenson/ruby-build
sstephenson/rbenv-default-gems
sstephenson/rbenv-gem-rehash
fesplugas/rbenv-installer
fesplugas/rbenv-bootstrap
rkh/rbenv-update
rkh/rbenv-whatis
rkh/rbenv-use
)
for plugin in ${PLUGINS[@]} ; do
KEY=${plugin%%/*}
VALUE=${plugin#*/}
RBENV_PLUGIN_ROOT="${RBENV_ROOT}/plugins/$VALUE"
if [ ! -d "$RBENV_PLUGIN_ROOT" ] ; then
git clone https://github.com/$KEY/$VALUE.git $RBENV_PLUGIN_ROOT
else
cd $RBENV_PLUGIN_ROOT
echo "Pulling $VALUE updates."
git pull
fi
done
# Show help if `.rbenv` is not in the path:
if [ ! $(which rbenv) ]; then
echo "
Seems you still have not added 'rbenv' to the load path:
# ~/.bash_profile:
export RBENV_ROOT=\"\${HOME}/.rbenv\"
if [ -d \"\${RBENV_ROOT}\" ]; then
export PATH=\"\${RBENV_ROOT}/bin:\${PATH}\"
eval \"\$(rbenv init -)\"
fi
"
fi
# About this guide
Install Ruby development with rbenv
# What is rbenv
Read here
https://github.com/sstephenson/rbenv
Comparison between RVM and rbenv
http://jonathan-jackson.net/rvm-and-rbenv
# Ubuntu, Centos install
Download rbenv-installer and place it to your home folder
```
$ sudo yum --enablerepo=rpmforge,epel,remi -y install git gcc make zlib zlib-devel openssl-develL
$ wget http://gitlab.zigexn.vn/ventura/dev-guide/raw/master/ruby/rbenv-installer
$ sh rbenv-installer
```
Add rbenv environment variables to load at boot time.
```
$ cat << EOF > ~/.bash_profile
export RBENV_ROOT="${HOME}/.rbenv"
if [ -d "${RBENV_ROOT}" ]; then
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"
fi
EOF
$ source ~/.bash_profile
$ rbenv rehash
$ rbenv -v
rbenv 0.4.0
```
### Install Ruby
```
$ rbenv install --list
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5
2.2.0-dev
2.2.0
$ rbenv install 2.1.5
Downloading ruby-2.1.5.tar.gz...
$ rbenv global 2.1.5
$ rbenv rehash
$ ruby -v
2.1.5
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment