Skip to content

Instantly share code, notes, and snippets.

@sandys
Created May 4, 2012 12:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandys/2594674 to your computer and use it in GitHub Desktop.
Save sandys/2594674 to your computer and use it in GitHub Desktop.
secure and self contained ruby deployment with multiple load-balanced, boot-persistent and highly available thin servers, and a multi-user rvm environment
set httpd port 2812 and # Monit will run on port 2812
allow sss:sss123 #set your username and password to loin to your Monit
# this configuration will kill the child worker processes (_0 and _1) if the master dies/is-killed
# if you stop the master, the workers are also stopped
# TODO: better cpu/mem tuning
check process mainserver
with pidfile /home/ruby1/sinatra-example/tmp/pids/thin_mainserver.pid
group appserver
start program = "/bin/bash -c 'export rvm_path=/home/ruby1/.rvm; . $rvm_path/scripts/rvm; cd /home/ruby1/sinatra-example; $rvm_path/gems/ruby-1.9.2-p320/bin/thin --max-persistent-conns 1024 --timeout 1 -V -p 9999 -P /home/ruby1/sinatra-example/tmp/pids/thin_mainserver.pid -d -R config.ru start'" as uid ruby1
stop program = "/bin/bash -c 'export rvm_path=/home/ruby1/.rvm; . $rvm_path/scripts/rvm; cd /home/ruby1/sinatra-example; $rvm_path/gems/ruby-1.9.2-p320/bin/thin -P /home/ruby1/sinatra-example/tmp/pids/thin_mainserver.pid stop'" as uid ruby1
if failed host localhost port 9999 then restart
if cpu usage > 95% for 3 cycles then restart
check process mainserver_0
with pidfile /home/ruby1/sinatra-example/tmp/pids/thin_mainserver_0.pid
group appserver
start program = "/bin/bash -c 'export rvm_path=/home/ruby1/.rvm; . $rvm_path/scripts/rvm; cd /home/ruby1/sinatra-example; $rvm_path/gems/ruby-1.9.2-p320/bin/thin --max-persistent-conns 1024 --timeout 1 -V -p 10001 -P /home/ruby1/sinatra-example/tmp/pids/thin_mainserver_0.pid -d -R config.ru start'" as uid ruby1
stop program = "/bin/bash -c 'export rvm_path=/home/ruby1/.rvm; . $rvm_path/scripts/rvm; cd /home/ruby1/sinatra-example; $rvm_path/gems/ruby-1.9.2-p320/bin/thin -P /home/ruby1/sinatra-example/tmp/pids/thin_mainserver_0.pid stop'" as uid ruby1
if failed host localhost port 10001 then restart
if cpu usage > 95% for 3 cycles then restart
depends on mainserver
check process mainserver_1
with pidfile /home/ruby1/sinatra-example/tmp/pids/thin_mainserver_1.pid
group appserver
start program = "/bin/bash -c 'export rvm_path=/home/ruby1/.rvm; . $rvm_path/scripts/rvm; cd /home/ruby1/sinatra-example; $rvm_path/gems/ruby-1.9.2-p320/bin/thin --max-persistent-conns 1024 --timeout 1 -V -p 10002 -P /home/ruby1/sinatra-example/tmp/pids/thin_mainserver_1.pid -d -R config.ru start'" as uid ruby1
stop program = "/bin/bash -c 'export rvm_path=/home/ruby1/.rvm; . $rvm_path/scripts/rvm; cd /home/ruby1/sinatra-example; $rvm_path/gems/ruby-1.9.2-p320/bin/thin -P /home/ruby1/sinatra-example/tmp/pids/thin_mainserver_1.pid stop'" as uid ruby1
if failed host localhost port 10002 then restart
if cpu usage > 95% for 3 cycles then restart
depends on mainserver
set daemon 30
set logfile syslog facility log_daemon
set httpd port 2812
check system localhost
include /etc/monit/conf.d/*
# Mostly yanked from:
# http://bit.ly/nginx-unicorn-setup
upstream sinatra-example-app {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
#server unix:/tmp/sinatra_example_app.socket fail_timeout=0;
server 0.0.0.0:9999;
server 0.0.0.0:10001;
server 0.0.0.0:10002;
}
server {
listen 80 default;
#charset koi8-r;
root /home/ruby1/sinatra-example/;
access_log /var/log/nginx/sinatra-example-access.log;
error_log /var/log/nginx/sinatra-example-error.log;
rewrite_log on;
location / {
proxy_pass http://sinatra-example-app;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
index index.html index.htm;
}
# if the request is for a static resource, nginx should serve it directly
# and add a far future expires header to it, making the browser
# cache the resource and navigate faster over the website
}
#add a project user, later also used for PassengerDefaultUser ruby1
adduser --system --home /home/ruby1 --group --shell /bin/zsh --disabled-login ruby1 --gecos 'Ruby Project 1'
# pre-requisites for ruby
sudo apt-get install libpcre3-dev build-essential libssl-dev libxml2 libxml2-dev libxslt-dev ncurses-dev
sudo apt-get install libcurl4-openssl-dev
apt-get install ruby1.8-dev ruby1.9-dev
#install rvm
sudo -u ruby1 -H curl -L get.rvm.io | sudo -u ruby1 -H bash
#add [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" to your ~/.bashrc
#install ruby 1.9.3
sudo -u ruby1 -H bash -c "source ~/.bashrc && rvm install 1.9.2"
#set default ruby
sudo -u ruby1 -H bash -c "source ~/.bashrc && rvm --default --create use 1.9.2"
#installing gems
sudo -u ruby1 -H bash -c "source ~/.bashrc && gem install pg"
sudo -u ruby1 -H bash -c "source ~/.bashrc && gem install --verbose rake"
#etc....
sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx
#install passenger gem
# the real solution to installation of passenger/fastthread gem failing on ubuntu rvm with "failed to build native extension issue
sudo -u ruby1 -H bash -c "source ~/.bashrc && gem install echoe passenger --no-ri --no-rdoc"
#permanently add --no-ri --no-rdoc to gemrc
sudo -u ruby1 -H bash -c "source ~/.bashrc && echo 'gem: --no-rdoc --no-ri' >> ~/.gemrc"
##DEPRECATED ################################################################
#installing nginx with passenger
###env rvm_path=/home/ruby1/.rvm GEM_HOME=/home/ruby1/.rvm/gems/ruby-1.9.2-p320/ /home/ruby1/.rvm/gems/ruby-1.9.2-p320/bin/passenger-install-nginx-module
#i installed nginx to /home/nginx
# change /etc/init.d/nginx lines to
#PATH=/home/nginx/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#DAEMON=/home/nginx/sbin/nginx
#add "user nginx" to the top of /etc/nginx/nginx.conf
####/etc/init.d/nginx start
############################################################################
#installing monit
apt-get install monit
#add relevant monit config files above
#include /etc/monit/conf.d/*
#and change in /etc/default/monit
#startup=1
#installing thin
sudo -u ruby1 -H bash -c "source ~/.bashrc && gem install eventmachine thin"
#restart thin
service monit start
#restart your thin server (in 30sec)
kill `cat /home/ruby1/sinatra-example/tmp/pids/thin.pid`
#or
/usr/sbin/monit -Ic /etc/monit/monitrc start mainserver
/usr/sbin/monit -Ic /etc/monit/monitrc stop mainserver
/usr/sbin/monit -Ic /etc/monit/monitrc restart mainserver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment