Skip to content

Instantly share code, notes, and snippets.

@rubyonrailsworks
Created March 4, 2012 04:22
Show Gist options
  • Save rubyonrailsworks/1970650 to your computer and use it in GitHub Desktop.
Save rubyonrailsworks/1970650 to your computer and use it in GitHub Desktop.
Capistrano-unicorn-apache2
# -*- encoding : utf-8 -*-
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load path.
require "rvm/capistrano" # Load RVM's capistrano plugin.
set :rvm_ruby_string, '1.9.2' # Or whatever env you want it to run in.
require 'bundler/capistrano'
set :application, "YourApp"
set :use_sudo, false
set :scm, :git
set :deploy_via, :remote_cache
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :git_shallow_clone, 1
set :git_enable_submodules, 1
role :web, "foobar" # Your HTTP server, Apache/etc
role :app, "foobar" # This may be the same as your `Web` server
role :db, "foobar", :primary => true # This is where Rails migrations will run
set :user, "root"
set :repository, "[email protected]:yourname/YourApp.git"
set :branch, "master"
set :deploy_to, "/var/rails/YourApp"
# tasks
namespace :deploy do
task :start, :roles => :app do
unicorn.start
end
task :stop, :roles => :app do
unicorn.stop
end
desc "Restart Application"
task :restart, :roles => :app do
unicorn.reload
end
desc "Compile assets"
task :assets do
run "cd #{release_path}; RAILS_ENV=production bundle exec rake assets:precompile"
end
desc "Symlink shared resources on each release"
task :symlink_shared, :roles => :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "ln -nfs #{shared_path}/system #{release_path}/public/system"
end
end
after 'deploy:update_code', 'deploy:symlink_shared'
after "deploy:symlink_shared", "deploy:assets"
#don't forget this!
require 'capistrano-unicorn'
# ------------------------------------------------------------------------------
# Sample rails 3 config
# Shoule bd placed in config/unicorn/
# ------------------------------------------------------------------------------
# Set your full path to application.
app_path = "/var/rails/YourApp/current"
# Set unicorn options
worker_processes 1
preload_app true
timeout 180
listen "your balancer ip"
# Spawn unicorn master worker for user apps (group: apps)
user 'root', 'root'
# Fill path to your app
working_directory app_path
# Should be 'production' by default, otherwise use other env
rails_env = ENV['RAILS_ENV'] || 'production'
# Log everything to one file
stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"
# Set master PID location
pid "#{app_path}/tmp/pids/unicorn.pid"
before_fork do |server, worker|
ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[: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|
ActiveRecord::Base.establish_connection
end
<VirtualHost *:80>
ServerName yourserver
ServerAlias yourserveralias
PassengerEnabled off
# Point this to your public folder of teambox
DocumentRoot /var/rails/yourapp/current/public
RewriteEngine On
# Redirect all non-static requests to unicorn
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://yourbanlancername%{REQUEST_URI} [P,QSA,L]
<Proxy balancer://yourbanlancername>
# or another port
BalancerMember http://127.0.0.1:3001
</Proxy>
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment