nginx, unicorn and capistrano config files for rails projects

nginx

This is a nginx config file for working with unicorn. Note that I use a socket file for unicorn rather than tcp port.

To use this file for your app. Just changed the server_name and root.

server {
  listen 80;
  
  server_name .rubycodesearch.com; # <-----
  root /u/apps/RubyCodeSearch/current/public; # <-----

  proxy_read_timeout 5;
  proxy_connect_timeout 5;

  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  location / {
    if (!-f $request_filename) {
      proxy_pass http://unix:$document_root/../tmp/sockets/unicorn.sock;
    }
  }
}

unicorn

Here is unicorn’s config file. It’s under rails root config/unicorn.rb. It’s all dynamic. You don’t need to change anything.

app_path = File.expand_path('../../', __FILE__)

worker_processes 3

listen "unix:#{app_path}/tmp/sockets/unicorn.sock", :delay => 5

timeout 10

working_directory app_path

pid 'tmp/pids/unicorn.pid'

stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"

preload_app true

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

capistrano

config/deploy.rb

require "rvm/capistrano"
require "bundler/capistrano"
require "capistrano-unicorn"
require 'capistrano/sidekiq'

set :whenever_command, "bundle exec whenever"
set :whenever_environment, defer { stage }
set :whenever_identifier, defer { "#{application}_#{stage}" }
require "whenever/capistrano"

set :default_environment, LANG: "en_US.UTF-8"

set :stages, %w(production develop)
set :default_stage, "production"
set :rails_env, "production"

require "capistrano/ext/multistage"


set :application, "RubyCodeSearch"    # <-----
set :repository,  "git@chengguangnan.com:RubyCodeSearch.git"    # <-----
set :branch, "master"
set :shared_children, %w(log tmp/pids tmp/sockets public/uploads)

set :rvm_type, :system
set :rvm_ruby_string, "default"

set :user, "root"
set :use_sudo, false

set :keep_releases, 30

server "111.222.333.444", :app, :web, :db, :primary => true     # <-----


before "deploy", "deploy:setup"

after "deploy:update_code", "deploy:migrate"

after "deploy:restart", "unicorn:restart"
after "deploy:restart", "deploy:cleanup"

after "deploy:restart", "sidekiq:restart"

after "deploy:restart", "whenever:update_crontab"

Gemfile

Note that I used a own fork of capistrano2 because capistrano2 is broken and not supported any more meanwhile capistrano3 is really buggy and nesty.

group :development do
  gem 'capistrano', github: 'chengguangnan/capistrano2'
  gem 'rvm-capistrano'
  gem 'capistrano-unicorn', '< 0.2.0'
  gem 'capistrano-sidekiq'
end