Skip to content

Instantly share code, notes, and snippets.

@robink
Last active December 26, 2015 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robink/7219469 to your computer and use it in GitHub Desktop.
Save robink/7219469 to your computer and use it in GitHub Desktop.
Etsy's Deployinator install note in a typical ubuntu/nginx/rvm (system wide install) environment.

Commands

1. Get DEPLOYINATOR

cd /var/www
git clone https://github.com/nectify/deployinator.git

2. Unicorn folder setup

mkdir tmp
mkdir tmp/sockets
mkdir tmp/pids
mkdir log

3. setup unicorn.rb

/var/www/deployinator/unicorn.rb

@dir = "/var/www/deployinator/"

worker_processes 2
working_directory @dir

timeout 30

# Specify path to socket unicorn listens to,
# we will use this in our nginx.conf later
listen "#{@dir}tmp/sockets/unicorn.sock", :backlog => 64
# Set process id path

pid "#{@dir}tmp/pids/unicorn.pid"

# Set log file paths
stderr_path "#{@dir}log/unicorn.stderr.log"
stdout_path "#{@dir}log/unicorn.stdout.log"

4. nginx configuration

Edit /etc/nginx/conf/nginx.conf

The following should be added as a child of http

  # use the socket we configured in our unicorn.rb
  upstream deployinator_unicorn_server {
    server unix:/var/www/deployinator/tmp/sockets/unicorn.sock
        fail_timeout=0;
  }

Append the following as a child of http

  # configure the virtual host
  server {
    # replace with your domain name
    server_name deployinator.nectify.com;
    # replace this with your static Sinatra app files, root + public
    root /path/to/app/public;
    # port to listen for requests on
    listen 80;
    # maximum accepted body size of client request
    client_max_body_size 4G;
    # the server will close connections after this time
    keepalive_timeout 5;

    location / {
      try_files $uri @deployinator_app;
    }

    location @deployinator_app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      # pass to the upstream unicorn server mentioned above
      proxy_pass http://deployinator_unicorn_server;
    }
  }

5. setup unicorn for deployinator to start on reboot (declare as a service)

Create /etc/init/deployinator.conf

description "Start unicorn"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown

respawn
respawn limit 5 20

script
  PIDFILE=/var/www/deployinator/tmp/pids/unicorn.pid
  echo $$ > $PIDFILE
  chown www:www $PIDFILE
  exec su -c "/etc/init.d/deployinator start" www
end script

/etc/init.d/deployinator

#!/bin/sh
set -e
# Example init script, this can be used with nginx, too,
# since nginx and unicorn accept the same signals

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/var/www/deployinator
PID=/var/www/deployinator/tmp/pids/unicorn.pid
ENV=production
CMD="/usr/local/rvm/wrappers/deployinator/unicorn -D -c $APP_ROOT/unicorn.rb -E $ENV"
action="$1"
set -u



old_pid="$PID.oldbin"

cd $APP_ROOT || exit 1

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $old_pid && kill -$1 `cat $old_pid`
}

case $action in
start)
  sig 0 && echo >&2 "Already running" && exit 0
  $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"
  $CMD
  ;;
upgrade)
  if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  then
    n=$TIMEOUT
    while test -s $old_pid && test $n -ge 0
    do
      printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $old_pid
    then
      echo >&2 "$old_pid still exists after $TIMEOUT seconds"
      exit 1
    fi
    exit 0
  fi
  echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  $CMD
  ;;
reopen-logs)
  sig USR1
  ;;
*)
  echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
  exit 1
  ;;
esac

6. Install ruby 1.9.3 on the server (if not already installed)

Check if ruby 1.9.3 is already installed:

rvm list

If not do the following:

rvm install 1.9.3

7. Bundle install

cd /var/www/deployinator
rvm use 1.9.3
bundle install

8. Install unicorn gem for 1.9.3

rvm use 1.9.3
gem install unicorn

9. Start the service

sudo su
service deployinator start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment