Permalink
Browse files

Deploy with unicorn and nignx

  • Loading branch information...
1 parent 1b31aab commit af76e791fa3dd542587007b08189f23339cb721b @reyesyang committed Dec 24, 2013
Showing with 173 additions and 6 deletions.
  1. +1 −1 Gemfile
  2. +7 −0 Gemfile.lock
  3. +19 −5 config/deploy.example.rb
  4. +36 −0 config/nginx.conf
  5. +110 −0 config/unicorn.rb
View
@@ -45,7 +45,7 @@ gem 'rails-i18n', '~>4.0.0'
# gem 'bcrypt-ruby', '~> 3.1.0'
# Use unicorn as the web server
-# gem 'unicorn'
+gem 'unicorn'
# Deploy with Capistrano
gem 'capistrano'
View
@@ -78,6 +78,7 @@ GEM
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.1)
+ kgio (2.8.1)
launchy (2.4.2)
addressable (~> 2.3)
less (2.4.0)
@@ -132,6 +133,7 @@ GEM
activesupport (= 4.0.0)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
+ raindrops (0.12.0)
rake (10.1.0)
redcarpet (3.0.0)
ref (1.0.5)
@@ -192,6 +194,10 @@ GEM
uglifier (2.2.1)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
+ unicorn (4.7.0)
+ kgio (~> 2.6)
+ rack
+ raindrops (~> 0.7)
websocket (1.0.7)
will_paginate (3.0.4)
wmd-rails (0.0.7)
@@ -231,5 +237,6 @@ DEPENDENCIES
turbolinks
twitter-bootstrap-rails
uglifier (>= 1.0.3)
+ unicorn
will_paginate (>= 3.0.pre)
wmd-rails
View
@@ -1,8 +1,8 @@
# -*- encoding : utf-8 -*-
set :user, "your"
-set :application, "your"
+set :application, "blog"
-# If you use rvm, please uncomment the following 4 lines
+# If you use rvm, please uncomment the following 4 lines
# require "rvm/capistrano" # Load RVM's capistrano plugin.
# set :rvm_path, "/home/#{user}/.rvm"
# set :rvm_bin_path, "/home/#{user}/.rvm/bin" # Or whatever env you want it to run in.
@@ -15,10 +15,13 @@
set :scm, :git
set :branch, :master
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
-set :deploy_to, "/home/#{user}/projects/#{application}"
+set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
+set :unicorn_config, "#{current_path}/config/unicorn.rb"
+set :unicorn_pid, "#{current_path}/tmp/pids/unicorn.pid"
+
role :web, domain # Your HTTP server, Apache/etc
role :app, domain # This may be the same as your `Web` server
role :db, domain, :primary => true # This is where Rails migrations will run
@@ -29,9 +32,19 @@
# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
- desc "Cause Passenger to initiate a restart"
+ desc "Start unicorn"
+ task :start do
+ run "cd #{current_path} && RAILS_ENV=production bundle exec unicorn_rails -c #{unicorn_config} -D"
+ end
+
+ desc "Stop unicorn"
+ task :stop do
+ run "if [ -f #{unicorn_pid} ]; then kill -QUIT `cat #{unicorn_pid}`; fi"
+ end
+
+ desc "Restart unicorn"
task :restart do
- run "touch #{current_path}/tmp/restart.txt"
+ run "if [ -f #{unicorn_pid} ]; then kill -s USR2 `cat #{unicorn_pid}`; fi"
end
desc "Reload the database with seed data"
@@ -47,5 +60,6 @@
run "cp #{deploy_to}/shared/config/*.yml #{release_path}/config" # So we need cp config files to share config folder manually first
run "cd #{release_path} && bundle install"
run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"
+ sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
end
View
@@ -0,0 +1,36 @@
+upstream unicorn {
+ server unix:/tmp/unicorn.reyesyang-info.sock fail_timeout=0;
+}
+
+server {
+ listen 80 default deferred;
+
+ client_max_body_size 4G;
+ server_name reyesyang.info www.reyesyang.info;
+
+ keepalive_timeout 5;
+
+ root /home/webuser/apps/blog/current/public;
+
+ location ^~ /assets/ {
+ gzip_static on;
+ expires max;
+ add_header Cache-Control public;
+ }
+
+ try_files $uri/index.html $uri @unicorn;
+ location @unicorn {
+ proxy_redirect off;
+ proxy_set_header Host $http_host;
+ proxy_set_header X-Forwarded-Host $host;
+ proxy_set_header X-Forwarded-Server $host;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_pass http://unicorn;
+ }
+
+ error_page 500 502 503 504 /500.html;
+ location = /500.html {
+ root /home/webuser/apps/blog/current/public;
+ }
+}
View
@@ -0,0 +1,110 @@
+
+# Sample verbose configuration file for Unicorn (not Rack)
+#
+# This configuration file documents many features of Unicorn
+# that may not be needed for some applications. See
+# http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
+# for a much simpler configuration file.
+#
+# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
+# documentation.
+
+# Use at least one worker per core if you're on a dedicated server,
+# more will usually help for _short_ waits on databases/caches.
+worker_processes 2
+
+# Since Unicorn is never exposed to outside clients, it does not need to
+# run on the standard HTTP port (80), there is no reason to start Unicorn
+# as root unless it's from system init scripts.
+# If running the master process as root and the workers as an unprivileged
+# user, do this to switch euid/egid in the workers (also chowns logs):
+# user "unprivileged_user", "unprivileged_group"
+
+app_root = File.expand_path("../..", __FILE__)
+# Help ensure your application will always spawn in the symlinked
+# "current" directory that Capistrano sets up.
+working_directory app_root # available in 0.94.0+
+
+# listen on both a Unix domain socket and a TCP port,
+# we use a shorter backlog for quicker failover when busy
+listen "/tmp/unicorn.reyesyang-info.sock", :backlog => 64
+listen 8080, :tcp_nopush => true
+
+# nuke workers after 30 seconds instead of 60 seconds (the default)
+timeout 30
+
+# feel free to point this anywhere accessible on the filesystem
+pid "#{app_root}/tmp/pids/unicorn.pid"
+
+# By default, the Unicorn logger will write to stderr.
+# Additionally, ome applications/frameworks log to stderr or stdout,
+# so prevent them from going to /dev/null when daemonized here:
+stderr_path "#{app_root}/log/unicorn.stderr.log"
+stdout_path "#{app_root}/log/unicorn.stdout.log"
+
+# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
+# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
+preload_app true
+GC.respond_to?(:copy_on_write_friendly=) and
+ GC.copy_on_write_friendly = true
+
+# Enable this flag to have unicorn test client connections by writing the
+# beginning of the HTTP headers before calling the application. This
+# prevents calling the application for connections that have disconnected
+# while queued. This is only guaranteed to detect clients on the same
+# host unicorn runs on, and unlikely to detect disconnects even on a
+# fast LAN.
+check_client_connection false
+
+# Force the bundler gemfile environment variable to
+# reference the Сapistrano "current" symlink
+before_exec do |_|
+ ENV["BUNDLE_GEMFILE"] = File.join(app_root, 'Gemfile')
+end
+
+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!
+
+ # The following is only recommended for memory/DB-constrained
+ # installations. It is not needed if your system can house
+ # twice as many worker_processes as you have configured.
+ #
+ # # This allows a new master process to incrementally
+ # # phase out the old master process with SIGTTOU to avoid a
+ # # thundering herd (especially in the "preload_app false" case)
+ # # when doing a transparent upgrade. The last worker spawned
+ # # will then kill off the old master process with a SIGQUIT.
+ # old_pid = "#{server.config[:pid]}.oldbin"
+ # if old_pid != server.pid
+ # begin
+ # sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
+ # Process.kill(sig, File.read(old_pid).to_i)
+ # rescue Errno::ENOENT, Errno::ESRCH
+ # end
+ # end
+ #
+ # Throttle the master from forking too quickly by sleeping. Due
+ # to the implementation of standard Unix signal handlers, this
+ # helps (but does not completely) prevent identical, repeated signals
+ # from being lost when the receiving process is busy.
+ # sleep 1
+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)
+end

0 comments on commit af76e79

Please sign in to comment.