Deploy your Rails App

Replacing Heroku

What is Heroku?

  • heroku login

  • heroku create

  • git push heroku master

A service for deploying applications

What Chris Says

"Nothing sweeter than git push production master"

How this works

  • git hook
    • post-receive hook
  • script that completes tasks

Getting started

Begin a Rails app

gem install rails && rbenv rehash
rails new jokes && cd jokes
bundle exec rails generate scaffold Joke joke:string punchline:string
bundle exec rake db:migrate

Quick configuration

  • config/routes
Rails.application.routes.draw do
  resources :jokes
  root to: 'jokes#index'
end
  • Gemfile
gem 'therubyracer', platforms: :ruby

Create the Droplet

  • Visit https://www.digitalocean.com
  • "Log In"
  • "Create Droplet"
    • Ubuntu 14.04.4 x64
    • $5/mo
    • New York 3
    • Add your SSH key
    • Choose a hostname

Prepare our server

  • ssh root@XXX.XXX.XXX.XXX
  • Install packages
apt-get update
apt-get install git-core curl zlib1g-dev build-essential libssl-dev
libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev
libcurl4-openssl-dev python-software-properties libffi-dev
  • Create a user
useradd -m app
chsh app -s /bin/bash

Install Ruby

  • sudo su - app
  • Install rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
  • Install and use Ruby
rbenv install 2.3.0
rbenv global 2.3.0
gem install bundler

Let's Deploy

Setup a middleware for app

  • Update the Gemfile
group :production do
  gem 'puma'
end

Setup a middleware for server

  • Retrieve suggested Puma configuration
wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma-manager.conf
wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma.conf
  • Update puma.conf
setuid app
setgid app
  • Add configuration to Bash execution
export SECRET_KEY_BASE="YOUR_SECRET_KEY_HERE"

Server middleware (continued)

  • Deploy startup files
sudo cp puma.conf puma-manager.conf /etc/init
  • Configure Puma (/etc/puma.conf)

 

/home/app/jokes

Run a web server

  • Install nginx
apt-get install nginx

Special sauce

Setup git remote

  • Setup the repository
sudo su - app && cd
mkdir ~/jokes-production && cd ~/jokes-production
git init --bare
  • Add the Hook!
    • ~/jokes-production/hooks/post-receive
    • post-receive from https://goo.gl/f36vHg
    • chmod +x hooks/post-receive
  • Setup sudo
sudo sh -c 'echo "app ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/90-app'

Viola!

Hit that deploy button

  • Add your server as a remote
git remote add production app@rails.atomaka.com:jokes-production
  • And, as Chris says
    • "there's nothing sweeter than"
git push production master

Resources

Made with Slides.com