Deploying Rails with RVM in a production environment
Jul 26, 2012 - 7 minutesLet’s start out by logging into our machine and installing some pre-requistes (these can also be found by running rvm requirements as well):
1sudo apt-get -y install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion git-core mysql-client libmysqlclient-dev libsasl2-dev libsasl2-dev mysql-server
Lets also install nodejs:
1curl -O http://nodejs.org/dist/v0.8.4/node-v0.8.4.tar.gz
2tar xzvf node-v0.8.4.tar.gz
3cd node-v0.8.4.tar.gz
4./configure && make && sudo make install
Now we can install ruby and RVM:
1curl -L https://get.rvm.io | bash -s stable --ruby
2source /home/ubuntu/.rvm/scripts/rvm
3rvm use 1.9.3 --default
4echo 'rvm_trust_rvmrcs_flag=1' > ~/.rvmrc
5# sudo su before this
6echo 'RAILS_ENV=production' >> /etc/environment
7rvm gemset create tester
And lastly nginx:
1sudo apt-get install nginx
Now let’s make a simple rails application back on our development machine with 1 simple root action:
1rails new tester -d=mysql
2echo 'rvm use 1.9.3@tester --create' > tester/.rvmrc
3cd tester
4bundle install
5rails g controller homepage index
6rm -rf public/index.html
7# Open up config/routes.rb and modify the root to to point to homepage#index
8rake db:create
9git init .
10git remote add origin https://github.com/bluescripts/tester.git # replace this with your git repo
11git add .; git ci -a -m 'first'; git push -u origin master
12rails s
Open your browser and go to http://localhost:3000 – all good! Now lets make some modifications to our Gemfile:
1source 'https://rubygems.org'
2gem 'rails', '3.2.6'
3gem 'mysql2'
4group :assets do
5 gem 'sass-rails', '~> 3.2.3'
6 gem 'coffee-rails', '~> 3.2.1'
7 gem 'uglifier', '>= 1.0.3'
8end
9gem 'jquery-rails'
10gem 'capistrano', :group => :development
11gem 'unicorn'
and re-bundle:
1 bundle
Now lets start prepping for deployment and compile our assets.
1capify .
2rake assets:precompile # dont forget to add it to git!
Make a file called config/unicorn.rb:
1# config/unicorn.rb
2# Set environment to development unless something else is specified
3env = ENV["RAILS_ENV"] || "development"
4
5site = 'tester'
6deploy_user = 'ubuntu'
7
8# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
9# documentation.
10worker_processes 4
11
12# listen on both a Unix domain socket and a TCP port,
13# we use a shorter backlog for quicker failover when busy
14listen "/tmp/#{site}.socket", :backlog => 64
15
16# Preload our app for more speed
17preload_app true
18
19# nuke workers after 30 seconds instead of 60 seconds (the default)
20timeout 30
21
22pid "/tmp/unicorn.#{site}.pid"
23
24# Production specific settings
25if env == "production"
26 # Help ensure your application will always spawn in the symlinked
27 # "current" directory that Capistrano sets up.
28 working_directory "/home/#{deploy_user}/apps/#{site}/current"
29
30 # feel free to point this anywhere accessible on the filesystem
31 shared_path = "/home/#{deploy_user}/apps/#{site}/shared"
32
33 stderr_path "#{shared_path}/log/unicorn.stderr.log"
34 stdout_path "#{shared_path}/log/unicorn.stdout.log"
35end
36
37before_fork do |server, worker|
38 # the following is highly recomended for Rails + "preload_app true"
39 # as there's no need for the master process to hold a connection
40 if defined?(ActiveRecord::Base)
41 ActiveRecord::Base.connection.disconnect!
42 end
43
44 # Before forking, kill the master process that belongs to the .oldbin PID.
45 # This enables 0 downtime deploys.
46 old_pid = "/tmp/unicorn.#{site}.pid.oldbin"
47 if File.exists?(old_pid) && server.pid != old_pid
48 begin
49 Process.kill("QUIT", File.read(old_pid).to_i)
50 rescue Errno::ENOENT, Errno::ESRCH
51 # someone else did our job for us
52 end
53 end
54end
55
56after_fork do |server, worker|
57 # the following is *required* for Rails + "preload_app true",
58 if defined?(ActiveRecord::Base)
59 ActiveRecord::Base.establish_connection
60 end
61
62 # if preload_app is true, then you may also want to check and
63 # restart any other shared sockets/descriptors such as Memcached,
64 # and Redis. TokyoCabinet file handles are safe to reuse
65 # between any number of forked children (assuming your kernel
66 # correctly implements pread()/pwrite() system calls)
67end
Now lets setup the config/deploy.rb to be more unicorn and git friendly, take note of the default environment settings which are taken from the server when running rvm info modified version of ariejan.net’s:
1require "bundler/capistrano"
2
3set :scm, :git
4set :repository, "[email protected]:bluescripts/tester.git"
5set :branch, "origin/master"
6set :migrate_target, :current
7set :ssh_options, { :forward_agent => true }
8set :rails_env, "production"
9set :deploy_to, "/home/ubuntu/apps/tester"
10set :normalize_asset_timestamps, false
11
12set :user, "ubuntu"
13set :group, "ubuntu"
14set :use_sudo, false
15
16role :web, "192.168.5.113"
17role :db, "192.168.5.113", :primary => true
18
19set(:latest_release) { fetch(:current_path) }
20set(:release_path) { fetch(:current_path) }
21set(:current_release) { fetch(:current_path) }
22
23set(:current_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
24set(:latest_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
25set(:previous_revision) { capture("cd #{current_path}; git rev-parse --short HEAD@{1}").strip }
26
27default_environment["RAILS_ENV"] = 'production'
28
29default_environment["PATH"] = "/home/ubuntu/.rvm/gems/ruby-1.9.3-p194/bin:/home/ubuntu/.rvm/gems/ruby-1.9.3-p194@global/bin:/home/ubuntu/.rvm/rubies/ruby-1.9.3-p194/bin:/home/ubuntu/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
30default_environment["GEM_HOME"] = "/home/ubuntu/.rvm/gems/ruby-1.9.3-p194"
31default_environment["GEM_PATH"] = "/home/ubuntu/.rvm/gems/ruby-1.9.3-p194:/home/ubuntu/.rvm/gems/ruby-1.9.3-p194@global"
32default_environment["RUBY_VERSION"] = "ruby-1.9.3-p194"
33
34default_run_options[:shell] = 'bash'
35
36namespace :deploy do
37 desc "Deploy your application"
38 task :default do
39 update
40 restart
41 end
42
43 desc "Setup your git-based deployment app"
44 task :setup, :except => { :no_release => true } do
45 dirs = [deploy_to, shared_path]
46 dirs += shared_children.map { |d| File.join(shared_path, d) }
47 run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
48 run "git clone #{repository} #{current_path}"
49 end
50
51 task :cold do
52 update
53 migrate
54 end
55
56 task :update do
57 transaction do
58 update_code
59 end
60 end
61
62 desc "Update the deployed code."
63 task :update_code, :except => { :no_release => true } do
64 run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"
65 finalize_update
66 end
67
68 desc "Update the database (overwritten to avoid symlink)"
69 task :migrations do
70 transaction do
71 update_code
72 end
73 migrate
74 restart
75 end
76
77 task :finalize_update, :except => { :no_release => true } do
78 run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
79
80 # mkdir -p is making sure that the directories are there for some SCM's that don't
81 # save empty folders
82 run <<-CMD
83 rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
84 mkdir -p #{latest_release}/public &&
85 mkdir -p #{latest_release}/tmp &&
86 ln -s #{shared_path}/log #{latest_release}/log &&
87 ln -s #{shared_path}/system #{latest_release}/public/system &&
88 ln -s #{shared_path}/pids #{latest_release}/tmp/pids &&
89 ln -sf #{shared_path}/database.yml #{latest_release}/config/database.yml
90 CMD
91
92 if fetch(:normalize_asset_timestamps, true)
93 stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
94 asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
95 run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
96 end
97 end
98
99 desc "Zero-downtime restart of Unicorn"
100 task :restart, :except => { :no_release => true } do
101 run "kill -s USR2 `cat /tmp/unicorn.tester.pid`"
102 end
103
104 desc "Start unicorn"
105 task :start, :except => { :no_release => true } do
106 run "cd #{current_path} ; bundle exec unicorn_rails -c config/unicorn.rb -D"
107 end
108
109 desc "Stop unicorn"
110 task :stop, :except => { :no_release => true } do
111 run "kill -s QUIT `cat /tmp/unicorn.tester.pid`"
112 end
113
114 namespace :rollback do
115 desc "Moves the repo back to the previous version of HEAD"
116 task :repo, :except => { :no_release => true } do
117 set :branch, "HEAD@{1}"
118 deploy.default
119 end
120
121 desc "Rewrite reflog so HEAD@{1} will continue to point to at the next previous release."
122 task :cleanup, :except => { :no_release => true } do
123 run "cd #{current_path}; git reflog delete --rewrite HEAD@{1}; git reflog delete --rewrite HEAD@{1}"
124 end
125
126 desc "Rolls back to the previously deployed version."
127 task :default do
128 rollback.repo
129 rollback.cleanup
130 end
131 end
132end
133
134def run_rake(cmd)
135 run "cd #{current_path}; #{rake} #{cmd}"
136end
Now lets try deploying (you may need to login to the server if this is the first time you’ve cloned from git to accept the SSH handshake):
1cap deploy:setup
Create your database config file in shared/database.yml:
1production:
2 adapter: mysql2
3 encoding: utf8
4 reconnect: false
5 database: tester_production
6 pool: 5
7 username: root
8 password:
Go into current and create the database if you haven’t already:
1rake db:create
2# cd down a level
3cd ../
4mkdir -p shared/pids
Now we can run the cold deploy:
1cap deploy:cold
2cap deploy:start
Now we can configure nginx:
Open up /etc/nginx/sites-enabled/default:
1upstream tester {
2 server unix:/tmp/tester.socket fail_timeout=0;
3}
4server {
5 listen 80 default;
6 root /home/ubuntu/apps/tester/current/public;
7 location / {
8 proxy_pass http://tester;
9 proxy_redirect off;
10
11 proxy_set_header Host $host;
12 proxy_set_header X-Real-IP $remote_addr;
13 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
14
15 client_max_body_size 10m;
16 client_body_buffer_size 128k;
17
18 proxy_connect_timeout 90;
19 proxy_send_timeout 90;
20 proxy_read_timeout 90;
21
22 proxy_buffer_size 4k;
23 proxy_buffers 4 32k;
24 proxy_busy_buffers_size 64k;
25 proxy_temp_file_write_size 64k;
26 }
27
28 location ~ ^/(images|javascripts|stylesheets|system|assets)/ {
29 root /home/deployer/apps/my_site/current/public;
30 expires max;
31 break;
32 }
33}
Now restart nginx and visit http://192.168.5.113/ ( replace with your server hostname/IP ). You should be all set!