Skip to content

Instantly share code, notes, and snippets.

@plainprogrammer
Created June 17, 2011 20:36
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 plainprogrammer/1032287 to your computer and use it in GitHub Desktop.
Save plainprogrammer/1032287 to your computer and use it in GitHub Desktop.
Orabrush Unicorn Application Server Configs
worker_processes 5;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
upstream unicorn {
server unix:/var/run/unicorn.sock;
}
server {
listen 80;
server_name localhost;
index index.html;
# needed to forward user's IP address to rails
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
proxy_max_temp_file_size 0;
# set Expire header on assets: see http://developer.yahoo.com/performance/rules.html#expires
location ~ ^/(images|javascripts|stylesheets)/ {
expires 10y;
}
# serve any existing file
if (-f $request_filename) {
break;
}
# serve any standard Rails page cache file with .html extension
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
location / {
root /var/app/current/public;
if (!-f $request_filename) {
proxy_pass http://unicorn;
break;
}
}
}
}
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
lockfile=/var/lock/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
if failed host 127.0.0.1 port 80 then restart
if cpu is greater than 40% for 2 cycles then alert
if cpu > 60% for 5 cycles then restart
if 10 restarts within 10 cycles then timeout
#!/bin/sh
#
# unicorn - this script starts and stops the unicorn daemon
#
# chkconfig: - 85 15
# description: A Ruby application server.
# processname: unicorn
# config: /etc/unicron.rb
# pidfile: /var/run/unicorn.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
# Feel free to change any of the following variables for your app:
APP_ROOT=/var/app/current
PID=/var/run/unicorn.pid
ENV=production
CMD="/usr/local/bin/bundle exec unicorn -D -E $ENV -c /etc/unicorn.rb"
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`
}
workersig () {
workerpid="/var/run/unicorn.$2.pid"
test -s "$workerpid" && kill -$1 `cat $workerpid`
}
case $1 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)
sig USR2 && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
$CMD
;;
kill_worker)
workersig QUIT $2 && exit 0
echo >&2 "Worker not running"
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
exit 1
;;
esac
check process unicorn
with pidfile /var/run/unicorn.pid
start program = "/etc/init.d/unicorn start"
stop program = "/etc/init.d/unicorn stop"
if mem is greater than 300.0 MB for 1 cycles then restart
if cpu is greater than 50% for 2 cycles then alert
if cpu is greater than 80% for 3 cycles then restart
group unicorn
check process unicorn_worker_5000
with pidfile /var/run/unicorn/unicorn.5000.pid
start program = "/bin/cat /dev/null"
stop program = "/etc/init.d/unicorn kill_worker 5000"
if mem is greater than 300.0 MB for 1 cycles then restart
if cpu is greater than 80% for 3 cycles then restart
group unicorn_workers
check process unicorn_worker_5001
with pidfile /var/run/unicorn/unicorn.5001.pid
start program = "/bin/cat /dev/null"
stop program = "/etc/init.d/unicorn kill_worker 5001"
if mem is greater than 300.0 MB for 1 cycles then restart
if cpu is greater than 80% for 3 cycles then restart
group unicorn_workers
check process unicorn_worker_5002
with pidfile /var/run/unicorn/unicorn.5002.pid
start program = "/bin/cat /dev/null"
stop program = "/etc/init.d/unicorn kill_worker 5002"
if mem is greater than 300.0 MB for 1 cycles then restart
if cpu is greater than 80% for 3 cycles then restart
group unicorn_workers
check process unicorn_worker_5003
with pidfile /var/run/unicorn/unicorn.5003.pid
start program = "/bin/cat /dev/null"
stop program = "/etc/init.d/unicorn kill_worker 5003"
if mem is greater than 300.0 MB for 1 cycles then restart
if cpu is greater than 80% for 3 cycles then restart
group unicorn_workers
worker_processes 4
preload_app true
timeout 30
listen '/var/run/unicorn.sock', :backlog => 2048
pid '/var/run/unicorn.pid'
stderr_path '/var/log/unicorn/error.log'
stdout_path '/var/log/unicorn/unicorn.log'
user 'deploy','deploy'
# REE
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
before_fork do |server, worker|
old_pid = '/var/run/unicorn.pid.oldbin'
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
port = 5000 + worker.nr
child_pid = server.config[:pid].sub('.pid', ".#{port}.pid")
system("echo #{Process.pid} > #{child_pid}")
ActiveRecord::Base.establish_connection
begin
uid, gid = Process.euid, Process.egid
user, group = 'deploy', 'deploy'
target_uid = Etc.getpwnam(user).uid
target_gid = Etc.getgrnam(group).gid
worker.tmp.chown(target_uid, target_gid)
if uid != target_uid || gid != target_gid
Process.initgroups(user, target_gid)
Process::GID.change_privilege(target_gid)
Process::UID.change_privilege(target_uid)
end
rescue => e
if RAILS_ENV == 'development'
STDERR.puts "couldn't change user, oh well"
else
raise e
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment