Skip to content

Instantly share code, notes, and snippets.

@BinaryMuse BinaryMuse/
Created Jan 30, 2011

What would you like to do?
DND - blog post
# Install RVM and dependencies
sudo aptitude install curl git-core
sudo bash < <( curl -L )
sudo aptitude install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev
sudo adduser `whoami` rvm
echo 'source /usr/local/lib/rvm' >> ~/.bashrc
# Set up users and groups
sudo useradd --home /var/www --create-home --groups rvm unicorn && sudo chmod g+w /var/www
sudo adduser `whoami` unicorn
# >> Log out and back in to SSH, open a new shell, etc. -- something to reload your environment
# Install Ruby 1.9.2-p136 and make it default
rvm install ruby-1.9.2-p136
# Make a sandwich while you wait (or have someone make you one:
rvm use ruby-1.9.2-p136 --default
# Install Nginx
sudo bash -c 'echo "deb $(lsb_release -cs) main" > /etc/apt/sources.list.d/nginx-stable-$(lsb_release -cs).list'
sudo apt-key adv --keyserver --recv-keys C300EE8C && sudo aptitude update
sudo aptitude install nginx
sudo bash -c 'curl -L > /etc/nginx/nginx.conf'
sudo rm /etc/init.d/nginx
for file in $(ls /etc/rc*/*nginx); do sudo rm $file; done
sudo bash -c 'curl -L > /etc/init/nginx.conf'
sudo start nginx
# Install Unicorn in the global gemset, and create a wrapper (yo yo yo in the hooouuuuuse!?)
rvm use ruby-1.9.2-p136@global
gem install unicorn --no-ri --no-rdoc
rvm wrapper ruby-1.9.2-p136 r192 unicorn
# Create and switch to a new gemset
rvm gemset create rails_app && rvm gemset use rails_app
gem install rails --no-ri --no-rdoc
# Create a sample Rails application
cd /var/www
rails new test_app
echo 'rvm use ruby-1.9.2-p136@rails_app --create' > test_app/.rvmrc
cd test_app
# >> Accept the .rvmrc warning
echo "gem 'unicorn'" >> Gemfile && bundle install
curl -L > config/unicorn.rb
sudo bash -c 'curl -L > /etc/init/test_app.conf'
sudo start test_app
worker_processes 1;
user www-data www-data;
pid /tmp/;
error_log /tmp/nginx.error.log;
events {
worker_connections 1024;
accept_mutex off;
http {
include mime.types;
default_type application/octet-stream;
access_log /tmp/nginx.access.log combined;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/html text/xml text/css
text/javascript application/x-javascript
upstream unicorn_test {
server unix:/var/www/test_app/tmp/sockets/unicorn.sock fail_timeout=0;
server {
listen 80;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
root /var/www/test_app/public;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://unicorn_test;
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/www/test_app/public;
description "nginx http daemon"
start on runlevel [2]
stop on runlevel [016]
console owner
exec /usr/sbin/nginx -c /etc/nginx/nginx.conf -g "daemon off;"
description "Test rails application"
start on runlevel [2]
stop on runlevel [016]
console owner
exec /usr/local/rvm/bin/r192_unicorn -c /var/www/test_app/config/unicorn.rb
APP_ROOT = File.expand_path(File.dirname(File.dirname(__FILE__)))
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm')
rvm_path = File.dirname(File.dirname(ENV['MY_RUBY_HOME']))
rvm_lib_path = File.join(rvm_path, 'lib')
$LOAD_PATH.unshift rvm_lib_path
require 'rvm'
RVM.use_from_path! APP_ROOT
rescue LoadError
raise "RVM ruby lib is currently unavailable."
ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__))
require 'bundler/setup'
worker_processes 4
working_directory APP_ROOT
preload_app true
timeout 30
listen APP_ROOT + "/tmp/sockets/unicorn.sock", :backlog => 64
pid APP_ROOT + "/tmp/pids/"
stderr_path APP_ROOT + "/log/unicorn.stderr.log"
stdout_path APP_ROOT + "/log/unicorn.stdout.log"
before_fork do |server, worker|
defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect!
old_pid = RAILS_ROOT + '/tmp/pids/'
if File.exists?(old_pid) && != old_pid
rescue Errno::ENOENT, Errno::ESRCH
puts "Old master alerady dead"
after_fork do |server, worker|
defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.