Rails deployment

Chennai-Rails Workshop 

21 March 2015

Abhishek Yadav


Deploy to heroku

  • Install heroku toolbelt 
  • heroku create tournament-tracker-<your_name>
  • git remote add heroku git@heroku.com:tournament-tracker-<your_name>.git
  • Edit Gemfile: replace 'sqlite3' with 'pg'
  • git init
  • git add
  • git commit -m 'first commit'
  • git push heroku master
  • heroku run rake db:migrate


Quick Rundown

Traditional deployment

  • Provision server from Digital Ocean website
  • Setup DNS/Cnames
  • Install build-tools, libs
  • Install Ruby, bundler
  • Copy code to the server
  • bundle install, db:migrate, asset precompile
  • Setup Unicorn/Puma + Nginx
  • Install database

Quick Rundown

Traditional deployment

sudo apt-get install build-essential \

zlib1g-dev \
libssl-dev openssl \
libreadline-dev \
sqlite3 libsqlite3-dev \
libxslt-dev libxml2-dev \
libffi-dev \
curl wget git-core

Install build-tools, libs

Traditional deployment

git clone git://github.com/sstephenson/ruby-build.git; cd ruby-build; sudo ./install.sh


sudo ruby-build -kv 2.2.0 /usr/local/
sudo gem install bundler


Install Ruby and bundler

Traditional deployment

mkdir -p /var/www/my_app; cd /var/www/my_app

git clone git@bitbucket.org:my_app


Or setup Capistrano

Copy code to the server

Traditional deployment

export RAILS_ENV=production


Edit database.yml to setup production config

bin/rake db:create

bin/rake db:schema:load

bin/rake db:seed


bin/rake assets:precompile


bundle install, db:migrate, asset precompile

Traditional deployment

Setup Unicorn/Puma + Nginx

Webrick only: bin/rails s -p 80 


Nginx: sudo apt-get install nginx

Unicorn: Add to Gemfile: gem 'unicorn'

Traditional deployment

Setup Unicorn + Nginx

Nginx site config: /etc/nginx/sites-available/my-app

upstream my_app {
      listen 80;

      server_name example.org;
      root /var/www/my_app/current/public;

      location / {
        proxy_set_header X_FORWARDED_PROTO $scheme;
        proxy_set_header Host $http_host;

        if (!-f $request_filename) {
          proxy_pass http://my_app;

Traditional deployment

Setup Unicorn + Nginx

Unicorn config: Rails.root/config/unicorn.rb

worker_processes 4

listen "/tmp/.sock", :backlog => 64
listen 8080, :tcp_nopush => true

pid "/var/www/my_app/tmp/pids/unicorn.pid"

stderr_path "/var/www/my_app/log/unicorn.stderr.log"
stdout_path "/var/www/my_app/log/unicorn.stdout.log"

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and

Traditional deployment

Setup Unicorn + Nginx

Unicorn start:

bundle exec unicorn -c config/unicorn.rb -E production -D 


Nginx start:

sudo service nginx start

Traditional deployment

Setup Database: mysql


sudo apt-get install mysql-server libclient-dev mysql-client


Edit database.yml

bin/rake db:create

bin/rake db:schema:load


DNS setups

  • Don't buy from Godaddy. Use namecheap.com, gandi.net instead
  • Either provide a Name server, or setup records 
  • First option is better, since we can move DNS management out to our server vendor (like Digital-Ocean or Amazon Aws) or third party services (like DynDns)
  • For the second option, figure out the DNS editor, and edit 

DNS setups: setting up DNS Records

DNS setups: transferring Name Server

Rails deployment

By Abhishek Yadav

Rails deployment

  • 1,285