Creating and deploying a Rails app

Posted by jason on Dec 30, 2009 in Ruby |

This documents how I create Ruby on Rails applications and deploy them to my server.  I have created a bare bones apache web server that hosts ruby applications using MySQL.  It also assumes you have Ruby on Rails set up on a Macbook Pro.

Server Side

User

mkdir ~/myrumbleapp
mkdir ~/myrumbleapp/www
mkdir ~/myrumbleapp/www/current
mkdir ~/myrumbleapp/logs

GIT

sudo groupadd myrumbleapp
sudo usermod -a -G myrumbleapp {USER}
cd ~/myrumbleapp
mkdir git
sudo chown {USER}.myrumbleapp git
cd git
git init --bare --shared=group

MySQL

mysql -u root -p
mysql> create database myrumbleapp;
mysql> use myrumbleapp;
mysql> grant all on myrumbleapp.* to myrumbleapp@localhost identified by 'password';

Apache2

/etc/apache2/sites-available/myrumbleapp
<VirtualHost *:80>
ServerName www.myrumbleapp.com
ServerAlias myrumbleapp.com
DocumentRoot /home/{USER}/www.myrumbleapp.com/www/current/public
ErrorLog /home/{USER}/www.myrumbleapp.com/logs/error_log
TransferLog /home/{USER}/www.myrumbleapp.com/logs/access_log
</VirtualHost>
sudo ln -s /etc/apache2/sites-available/myrumbleapp /etc/apache2/sites-enabled/myrumbleapp
sudo /etc/init.d/apache2 restart

Client Side

On the client

First, create a skeleton rails application.  For this example, I will be using “myrumbleapp”.  We will also freeze the rails version into our application. That way we never have to worry about rails versions on serverrs.

cd ~/Sites/
rails myrumbleapp
cd ~/Sites/myrumbleapp
rake rails:freeze:gems

/.gitignore

tmp
log
mkmf.log
config/database.yml

Capistrano
cd ~/Sites/myrumbleapp
capify .

I use the following deploy.rb file.  The important part is that it creates a link from my shared/system/config/database.yml file into the release folder.  I keep the database.yml file out of GIT because it seems to be a best practice that other folks follow.  Not exactly sure it is necessary with my code since I host my own GIT repos accessible only via SSH.

/config/deploy.rb

default_run_options[:pty] = true
set :application, "myrumbleapp"
set :repository,  "{USER}@{SERVER.COM}:myrumbleapp/git"
set :user, "USER"
set :deploy_to, "/home/{USER}/myrumbleapp/www"
set :scm, :git
set :branch, "master"

role :web, "{SERVER.COM}"
role :app, "{SERVER.COM}"
role :db,  "{SERVER.COM}", :primary => true

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

  [:start, :stop].each do |t|
    desc "#{t} task is a no-op with passenger"
    task t, :roles => :app do ; end
  end

  desc "Symlink shared configs and folders on each release."
  task :symlink_shared do
    run "ln -nfs #{shared_path}/system/config/database.yml #{release_path}/config/database.yml"
  end
end

after 'deploy:update_code', 'deploy:symlink_shared'


GIT

Next we will check our application into GIT version control.

git init
cp config/database config/database.tmp.yml
git add .
git commit -m "initial checkin"

Configure the remote repository for GIT

git remote add origin {USER}@snowcaptech.com:myrumbleapp/git

Push the skeleton application to the server

git push origin master

Deploy the application

cap deploy:setup

cap deploy

cap deploy:migrations

Reply

Powered by WP Hashcash

Copyright © 2010 programming with passion All rights reserved. Theme by Laptop Geek.