Skip to content

Instantly share code, notes, and snippets.

@minhajuddin
Created December 14, 2010 12:16
Show Gist options
  • Select an option

  • Save minhajuddin/740335 to your computer and use it in GitHub Desktop.

Select an option

Save minhajuddin/740335 to your computer and use it in GitHub Desktop.
#!/usr/bin/env rackup
require File.expand_path('app', File.dirname(__FILE__))
Ramaze.start(:file => __FILE__, :started => true)
run Ramaze
# Use a trailing '/'
APP_ROOT = "/var/www/apps/my.domain.com/"
# The list of ports which our website is running on. We want to setup God monitoring for each of these ports as each port has a separate master unicorn process. In our case we're just going to use one port
%w{15000}.each do |port|
God.watch do |w|
w.name = "my.domain.com-#{port}"
w.interval = 30.seconds # default
# Omit the trailing '/' after #{APP_ROOT}
# Go into the website root before starting unicorn
w.start = "cd #{APP_ROOT} && unicorn -l #{port} -c #{APP_ROOT}config/unicorn.conf"
# -QUIT = graceful shutdown, waits for workers to finish their current request before finishing
w.stop = "kill -QUIT `cat #{APP_ROOT}tmp/pids/unicorn-master.pid`"
# -USR2 = reexecute the running binary. A separate QUIT should be sent to the original process once the child is verified to be up and running.
w.restart = "kill -USR2 `cat #{APP_ROOT}tmp/pids/unicorn-master.pid`"
w.start_grace = 10.seconds
w.restart_grace = 10.seconds
# User under which to run the process
w.uid = 'www-data'
w.gid = 'www-data'
# Cleanup the pid file (this is needed for processes running as a daemon)
w.behavior(:clean_pid_file)
# Conditions under which to start the process
w.start_if do |start|
start.condition(:process_running) do |c|
c.interval = 5.seconds
c.running = false
end
end
# Conditions under which to restart the process
w.restart_if do |restart|
restart.condition(:memory_usage) do |c|
c.above = 150.megabytes
c.times = [3, 5] # 3 out of 5 intervals
end
restart.condition(:cpu_usage) do |c|
c.above = 50.percent
c.times = 5
end
end
w.lifecycle do |on|
on.condition(:flapping) do |c|
c.to_state = [:start, :restart]
c.times = 5
c.within = 5.minute
c.transition = :unmonitored
c.retry_in = 10.minutes
c.retry_times = 5
c.retry_within = 2.hours
end
end
end
end
# replace my_web_app with any unique identifier
# replace the port number with any unused port
upstream my_web_app {
server 127.0.0.1:15000 fail_timeout=0;
}
server {
# replace my.domain.com with your web site's domain
server_name my.domain.com;
keepalive_timeout 5;
# you don't have to use my.domain.com in the path but I find its a useful convention
# you also might want to write these logs to /var/log/nginx
access_log /var/www/apps/my.domain.com/log/nginx.access.log;
error_log /var/www/apps/my.domain.com/log/nginx.error.log;
# path to the root of your application
root /var/www/apps/my.domain.com/;
location / {
if (-f $request_filename) {
access_log off;
rewrite_log off;
expires 30d;
break;
}
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
if (!-f $request_filename) {
# my_web_app needs to be the same as whatever upstream name you assigned above
proxy_pass http://my_web_app;
break;
}
}
}
# Unicorn says to use at least one worker process per core (particularly on dedicated servers)
worker_processes 4
APP_ROOT = "/var/www/apps/my.domain.com"
# The root directory of you app
working_directory APP_ROOT
# Port that worker processes listen on
# This can also be a unix socket
listen 15010, :tcp_nopush => true
# Location of master process PID file
pid "#{APP_ROOT}/tmp/pids/unicorn-master.pid"
# Location of stderr/stdout logs
stderr_path "#{APP_ROOT}/log/unicorn.stderr.log"
stdout_path "#{APP_ROOT}/log/unicorn.stdout.log"
# combine REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment