Skip to content

Instantly share code, notes, and snippets.

@diegorv
Created May 11, 2010 17:32
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 diegorv/397579 to your computer and use it in GitHub Desktop.
Save diegorv/397579 to your computer and use it in GitHub Desktop.
# Sean Porter (http://portertech.ca)
# Example unicorn configuration for a decent VPS (Virtual Private Server)
#
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete documentation.
# Use at least one worker per core
worker_processes 6
# Help ensure your application will always spawn in the symlinked "current" directory that Capistrano sets up
working_directory "/var/www/myapp"
# Listen on a Unix domain socket, use the default backlog size
listen "/var/www/myapp/tmp/sockets/unicorn.sock", :backlog => 1024
# Nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 30
# Lets keep our process id's in one place for simplicity
pid "/var/www/myapp/tmp/unicorn.pid"
# Logs are very useful for trouble shooting, use them
stderr_path "/var/www/myapp/log/unicorn.stderr.log"
stdout_path "/var/www/myapp/log/unicorn.stdout.log"
# Use "preload_app true"
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
before_fork do |server, worker|
# the following is highly recomended for Rails + "preload_app true"
# as there's no need for the master process to hold a connection
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
# Thank you GitHub!
#
# When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
# immediately start loading up a new version of itself (loaded with a new
# version of our app). When this new Unicorn is completely loaded
# it will begin spawning workers. The first worker spawned will check to
# see if an .oldbin pidfile exists. If so, this means we've just booted up
# a new Unicorn and need to tell the old one that it can now die. To do so
# we send it a QUIT.
# Using this method we get 0 downtime deploys.
old_pid = '/tmp/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|
# per-process listener ports for debugging/admin/migrations
# addr = "127.0.0.1:#{9293 + worker.nr}"
# server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
# the following is *required* for Rails + "preload_app true"
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
# if preload_app is true, then you may also want to check and
# restart any other shared sockets/descriptors such as Memcached,
# and Redis. TokyoCabinet file handles are safe to reuse
# between any number of forked children (assuming your kernel
# correctly implements pread()/pwrite() system calls)
# Unicorn master is started as root, which is fine, but let's
# drop the workers to your user/group
begin
uid, gid = Process.euid, Process.egid
user, group = 'www-data', 'www-data'
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