Skip to content

Instantly share code, notes, and snippets.

Forked from jondkinney/
Created November 17, 2012 00:53
What would you like to do?
Deploying Rails to Linode

Deploying Rails to Linode

Installing Ruby Enterprise Edition, Apache, MySQL, and Passenger for deploying Rails 3.0 applications.

Get a Linode, and set it up with Ubuntu 12.04 LTS so that you have till 2017 to get updates. Once the Linode is formatted, boot it and continue on.

Set up an 'A' record in your DNS, pointing to the IP of your Linode. I'm using here.

Initial setup

Log in with ssh root@your_ip

Add a user, and add it to the sudo group,

adduser deploy
usermod -G sudo deploy

Change the default SSH port and disallow root acces. vim /etc/ssh/sshd_config

# What ports, IPs and protocols we listen for
Port 16888

PermitRootLogin no

Logout and reboot your linode via the console.

Test the new settings - Log back in with ssh deploy@your_ip -p 16888 and log out again

Create your authorized keys file

mkdir ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Logout and upload your public key

cat ~/.ssh/ | ssh deploy@your_ip -p 16888 'cat >> .ssh/authorized_keys'

That looks a little complex, but we're just printing out the SSH key, piping the output to SSH and directing it into the authorized_keys on the remote server. You could do this many ways, but this is something you could turn into a script and use this to add multiple keys.

Log back in and you're set with keys.

Installing Git and Apache (we need git for ZSH)

sudo apt-get install apache2 git-core

Now disable the default site.

sudo a2dissite default
sudo /etc/init.d/apache2 restart

Install ZSH

sudo wget --no-check-certificate -O - | sh
  1. Edit the ~/.oh-my-zsh/lib/rvm.zsh so that it points to /usr/local/rvm/bin/rvm-prompt

  2. edit the ~/.oh-my-zsh/themes/j2fly.zsh-theme so that it points to /usr/local/rvm/bin/rvm-prompt as well

  3. Edit your ~/.zshrc so that it has the RVM stuff in it.

  4. You may also need to edit/move the export PATH= line to be above the zsh load line

Modify .gemrc file in your root directory

gem: --no-rdoc --no-ri
:benchmark: false
:backtrace: false
:update_sources: true
:verbose: true
:bulk_threshold: 1000

Installing Ruby

We'll use Ruby 1.9.3 through RVM

  1. Install some required libraries to allow for RVM to build Ruby 1.9.3

    sudo apt-get update; apt-get install build-essential patch zlib1g-dev libssl-dev libreadline5-dev

  2. Install RVM

    \curl -L | bash -s stable --ruby

  3. set default ruby

    rvm --default use ruby

  4. Use the global gemset and install bundler into it

    rvm gemset use global gem install bundler

  5. Install libyaml and reinstall ruby to get rid of the "ruby installation is missing psych" error

    rvm pkg install libyaml rvm reinstall all --force

Configuring Apache and Passenger

rvm use ree
rvm gemset use global
rvmsudo gem install passenger
sudo apt-get install libcurl4-openssl-dev apache2-prefork-dev  libapr1-dev  libaprutil1-dev

rvmsudo /usr/local/rvm/gems/ree-1.8.7-2011.03@global/bin/passenger-install-apache2-module

Follow the instructions.

CD to /etc/apache2/conf.d and add a file called "passenger.conf" which has the following contents:

LoadModule passenger_module /usr/local/rvm/gems/ree-1.8.7-2011.03@global/gems/passenger-3.0.7/ext/apache2/
PassengerRoot /usr/local/rvm/gems/ree-1.8.7-2011.03@global/gems/passenger-3.0.7
PassengerRuby /usr/local/rvm/wrappers/ree-1.8.7-2011.03@global/ruby

Create a new vhost

sudo vim /etc/apache2/sites-available/

Put this code in the file:

<VirtualHost *:80> ServerName DocumentRoot /var/www/
<Directory /var/www/> AllowOverride all
Options -MultiViews

Then enable the site

sudo a2ensite
sudo /etc/init.d/apache2 restart

And give the deploy user permissions to the /var/www root folder:

sudo chown -R deploy:deploy /var/www 

Again, not the best approach, but an acceptable one.

We're done with server setup!

Preparing Your Rails app

Create the Rails 3 application. We'll do a simple one with a single table.

rails new demo
cd demo
rails g scaffold projects name:string description:text
rake db:migrate
rm public/index.html

Add a root route to config/routes.rb

root :to => "projects#index"

Add the mysql2 gem to your Gemfile and get the sqlite3 one only in dev mode. Specify the version numbers for the gems or you'll get burned!'

group :development do                                                                                                                                                                                
  gem 'sqlite3', '1.3.3'

group :production do
  gem 'mysql2', '0.2.7'



Let's deploy!

Capistrano setup

Generate the capistrano config file:

capify .

Modify the config/deploy.rb file with our settings for our server.

require 'bundler/capistrano'

set :application, ""

# Deploy from your local Git repo by cloning and uploading a tarball
set :scm, :git
set :repository,  "."
set :deploy_via, :copy

set :user, :deploy
set :deploy_to, "/var/www/#{application}"
set :use_sudo, false

role :web, ""                          # Your HTTP server, Apache/etc
role :app, ""                          # This may be the same as your `Web` server
role :db,  "", :primary => true # This is where Rails migrations will run

namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
   run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"

Pay attention to this part:

set :scm, :git
set :repository,  "."
set :deploy_via, :copy

This lets you from your local Git repo by cloning and uploading a tarball. This means you can push code from your computer to your server, rather than having to pull code from your remote Git repository to your server. Or if Github is down.

Now specify your production database info in database.yml:

  adapter: mysql2
  host: localhost
  database: projects_production
  username: rails
  password: rails

Here, we're putting our production db password in our database.yml file. You probably want to keep this file out of your repository, and instead write a custom Capistrano task to build the database.yml file during deployment. That's something you can Google on your own.

Now make a Git repo

git init
git add .
git commit -a -m "Initial import"

Finally, set up the app on the server:

cap deploy:setup

Deploy the app

cap deploy:cold

And we're done! At this point, make changes to your code, commit them, redeploy with

cap deploy

Bonus: Stream your logfiles!

Add this to deploy.rb

desc "tail production log files" 
task :tail_logs, :roles => :app do
  run "tail -f #{shared_path}/log/production.log" do |channel, stream, data|
    puts  # for an extra line break before the host name
    puts "#{channel[:host]}: #{data}" 
    break if stream == :err    

Then run it with

cap tail_logs

Capistrano isn't just for deploying Rails apps. You can use it to automate anything you do with SSH.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment