gmlv_w
Last Updated: February 25, 2016
·
3.439K
· jamesdullaghan
Photo on 6 5 13 at 8.07 pm

Setup dev env in vagrant w/ rvm pg rails nginx unicorn

Navigate into project folder

cd project_folder

Creates vagrantfile inside project folder

vagrant init precise32 http://files.vagrantup.com/precise32.box

Don't forget to change box name/settings in vagrant file

Boots from vagrantfile in project! Viola!

vagrant up

SSH into vagrant

(Don't know if it's good practice to create roles in dev environment?)

vagrant ssh

Update packages on virtual server

sudo apt-get update

sudo apt-get install curl

install latest stable version of rvm

curl -L get.rvm.io | bash -s stable

load rvm

source ~/.rvm/scripts/rvm

install rvm dependencies

rvm requirements

Install ruby 2.0.0

rvm install 2.0.0

Use 2.0.0 as rvm default

rvm use 2.0.0 --default

install latest version of rubygems if rvm install didn't

rvm rubygems current

install rails gem

gem install rails --no-ri --no-rdoc

Install postgres

sudo apt-get install postgresql postgresql-server-dev-9.1

gem install pg -- --with-pg-config=/usr/bin/pg_config

setup nginx (replace projectname with project name)

sudo apt-get install nginx
nginx -h
cat /etc/init.d/nginx
/etc/init.d/nginx -h
sudo service nginx start
cd /etc/nginx
less nginx.conf
cd sites-enabled
sudo rm default
sudo ln -s /vagrant/config/nginx.conf projectname
sudo service nginx restart

setup unicorn

cd /vagrant
bundle install
bundle exec unicorn -c config/unicorn.rb -D
chmod +x config/unicorn_init.sh
sudo ln -s /vagrant/config/unicorn_init.sh /etc/init.d/unicorn
bundle install --binstubs
sudo service unicorn restart

config/nginx.conf

upstream unicorn {
  server unix:/tmp/unicorn.projectname.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  # server_name projectname.com;
  root /vagrant/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;
}

config/unicorn.rb

working_directory "/vagrant"
pid "/vagrant/tmp/pids/unicorn.pid"
stderr_path "/vagrant/log/unicorn.log"
stdout_path "/vagrant/log/unicorn.log"

listen "/tmp/unicorn.projectname.sock"
worker_processes 2
timeout 30

config/unicorn_init.sh

#!/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:
# Remember -E production flag for production & sudo -c "$CMD" - user so it's not run as root!
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/vagrant
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="$APP_ROOT/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
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
        su -c "$CMD" - vagrant
        ;;
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"
        su -c "$CMD" - vagrant
        ;;
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"
        su -c "$CMD" - vagrant
        ;;
reopen-logs)
        sig USR1
        ;;
*)
        echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
        exit 1
        ;;
esac

Resources from Railscasts/vagrant documentation.
For use if puppet or chef is a little over your head.

Say Thanks
Respond