Skip to content

monolith/rollout_tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Setup Tutorial for a Rails application with Git / Github / Capistrano / Ruby Enterprise / Passenger

This tutorial will show you how to rollout a basic Rails application using Git and Capistrano. We will do this while leveraging Github, and – on the production server – Ruby Enterprise Edition and Passenger. The database will be MySQL. We will be using Ubuntu as the OS.

I will walk you through from scratch. I recommend you follow from scratch as well, unless you know what you’re doing (in which case, jump in whereever you like but be prepare for potentially unexpected behavior).

This tutorial will NOT teach you Rails, Git, Capistrano, etc. While it will cover the basics, it will be just enough information to get you up and running. However, you will benefit greatly by looking at each one of these things in more detail. Links at the bottom will help you get started on learning the details.

Prerequisites

  • Ruby installation on your local machine
  • Rails installation on your local machine
  • MySQL installation on your local machine
  • Clean server for production (with nothing but the initial OS setup)

It will be nice if you are already comfortable with basic Rails programming. If not, then some of this may not make sense (but it should still work).

My set up

NOTE: If you use a different setup, you may have slightly different commands / results. There will not be many (if any) OS-specific commands used on my local machine (development), so it will be more useful to you to match the production machine. I have not tried this with Ubuntu 8.10 or 9.04 in production, but I imagine the differences will be minor.

  • Rails version: 2.2.2
  • OS: Ubuntu 8.10 (development), Ubuntu 8.04 (production)
  • Db: MySQL Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (i486) using readline 5.2
  • Capistrano v2.5.8
  • Git version 1.5.6.3

Create a new Rails project

Let’s create an initial Rails project on the local development machine, and ensure it works. Of course, you can do this with any Rails project, but this is the one I will use for this tutorial.

Create project:

rails rollout_tutorial

Go to the rollout_toturial directory that was just created.

cd rollout_tutorial

Let’s see if this worked!

script/server

Open your browswer and go to http://localhost:3000/
If you see the “Welcome aboard” page, then good. If not, check that your Rails installation is setup properly.

Let’s do some basic things. For one, let’s remove index.html

rm public/index.html

And set up the database:

mysql -u root

At the mysql prompt:

create database rollout_tutorial_development;

Exit the database and let’s update the project’s database config file. Open config/database.yml

development:
      adapter: mysql
      database: rollout_tutorial_development
      username: root
      password:
      host: localhost

NOTE: You may need to change the username and password (depending on your local set up).

Now let’s create a small application. All this application will do is allow people to enter items into the database and see what has been entered. I will use nifty_generators, see below Useful Links section for a link on how to install this if you do not have it.

This will create the migration files, and controllers

script/generate nifty_scaffold thing title:string index new edit

Now, run the migration (if this doesn’t work, make sure that you set up config/database.yml correctly):

rake db:migrate

Update config/routes.rb, and add the following line

map.connect '', :controller => 'things'

And then this:

script/generate nifty_layout

Finally, let’s check that the web application works. If your server is still up from last time, stop it, and and then:

script/server

Open your browswer and go to http://localhost:3000/
Now you should see your application and be able to add things (which will be saved to the database).

Works? Yay! Time to move on to the fun stuff.

Git

NOTE: Git is not the same thing as Github. Github is service that will let you store your code on their servers (not run the code, just store it).

Let’s set up some version control with Git. Install Git if you don’t already have it installed (on Ubuntu 8.10, the command is sudo apt-get install git). Check the Git link in the Useful Links section for more on Git and installation.

Now, let’s initiate version control for our project.

git init

You should now see .git (hidden) directory in your project (ls -a to see hidden stuff).

Before we go on, let’s tell git to ignore tracking certain files. We want to do this because we don’t want to track temporary files (ending with ~ on Ubuntu), log files, and some sensitive files such as those in the config directory. Up to you what files and directories to ignore, this example just shows the capability. Read up more on Git to learn how to use it better. If you’re hosting your code on Github (for free), others will be able to see your files, which is why you will want to ignore some senstive files.

Create a .gitignore file in the project root.
In the file, specify what should be ignored, here is what I used:

*.log
db/schema.rb
db/schema.sql
db/sphinx/*
*~
tmp/*
config/*

Now, let’s add files for our first version of the project (you need the “.” there)

git add .

If you want to see what is scheduled for versioning, do this:

git status

Time to commit (save) this.

git commit -m “First commit”

Done!

Github

OK, so we already have version control with Git locally on our local computer. Which, for most people may be good enough. But, let’s do more, and host our code repository on Github.

Create a Github account. Done?

Github account settings

Once your account is set up, you need to take a few steps in order to use it.

  • Click on profile, and save your name. You will need to have git set up with the same name locally (we will do this shortly).
  • Click on account and ensure the email address you used here is the same one you will want to use with git locally (will do this shortly as well).

We need to add an SSH key. Assuming you have not done this yet, here are the steps.
On your local machine (you may need to install SSH first… sudo apt-get install ssh):

ssh-keygen

Follow the steps (for this example you can just hit enter and accept all default values).

To see if it worked, do this:

cat ~/.ssh/id_rsa.pub

That should show you the public SSH key. Copy this, and go to your Github account.

  • Add this key as your public SSH key (see account page).

Creating a Github repository

While logged into Github, head for the Dashboard page.

  • Click on dashboard
  • Click on create a new one
  • Name your project the same name as our Rails project “rollout_tutorial”
  • Click on create repository

You should now see a setup page. Most of these things you can ignore (if you’re following this tutorial step by step).

Here is what you need for now (copy and paste from the Github page, or change the name and email below):

git config —global user.name “name”
git config —global user.email email

When this will do is set your git global name and email (on your local machine). Make sure these are the name and emails you want to use, and copy and paste in your local terminal.

After, you’ve done that, let’s check that we can connect to Github via SSH.

ssh git@github.com

If successful, you will see a message telling you that you have been authenticated. If not, go back an make sure you have set up the Github account settings as described above.

Works? Let’s move on.

We have already create a Rails application and used git to save it. So all we need to do now are the following lines (replace username with yours in the first line):

git remote add origin git@github.com:USERNAME/rollout_tutorial.git
git push origin master

Go back to the Github project page and click on Continue (or refresh).

You should now see the code there! And some details on when you last saved the changes.

Just to be sure…

Let’s make an update to one of the files. On your local machine, open the README file in the rollout_tutorial directory, and change it. For example, add the following line at the top:

Hello world

Save the file.

Do this, and you will see that Git noticed the update:

git status

You will need to learn how to use Git to learn how to use version control, add and remove files, etc. But that is not the purpose of this tutorial. So, since we just updated the README file, let’s make sure we can send that change to Github. Here’s how to do that.

While we saved the README file, we did not add it to our latest version with Git. You would use this command regardless of Github. This is simply to commit the changes you made to the latest in your local repository.

git commit -a -m “Updated README

Finally, let’s push these changes to Github:

git push origin master

In your browser, go back to your project (or refresh). You should see the files, and you should also see that the README file was just updated, with a commit comment we added above “Updated README” … click on the file in your browser. You should see your changes there!

Worked? Then, this part is done.

Production server

I am doing the following on a fresh install of Ubuntu 8.04 LTS, I am using a VPS hosted on Linode.com (the company you host with should not matter as long as you have full root access).

General setup

Log into the server:

ssh root@domain_or_ip_of_your_server

Say “yes” add add this domain/server to your known hosts (you won’t have to do this again). Enter your password, and you should be in your server.

Update the OS (for security reasons):

apt-get update

You may want to change the hostname (don’t have to). Replace “myservername” with whatever you like.

hostname myservername

If you exit and reconnect, you will see root@myservername… this will help remind you which server you are working on while looking at the terminal.

Install MySQL

This may seem a little early, but we need to do this before installing Ruby Enterprise. Otherwise, you may run into a problem installing mysql gem. If you plan using another database, install it now anyway.

apt-get install mysql-server

We need this to avoid a problem with installing mysql gem later.

apt-get install libmysql++-dev

Ruby Enterprise Edition

OK, let’s install REE. This is a fork of the Ruby language that will run your code faster (the claim is 33% faster on average). Since, this is indeed Ruby, we don’t need to install the standard Ruby library (if you do, install the standard Ruby library, you will need to modify somethings in this tutorial).

REE will install a bunch of things for you, this is why were are doing this now.

Download Ruby Enterprise Edition (see their website, link below). In this exmaple, I am using the current version as of August 23, 2009. You may see a different version, and need to modify the below as needed.

I will do the download with wget. So first, let’s install wget.

apt-get install wget

Now download REE:

wget http://rubyforge.org/frs/download.php/58677/ruby-enterprise-1.8.6-20090610.tar.gz

Extract it, and remove the original download (unless you want to keep it):

tar xzvf ruby-enterprise-1.8.6-20090610.tar.gz
rm ruby-enterprise-1.8.6-20090610.tar.gz

Run the installation:

ruby-enterprise-1.8.6-20090610/installer

The installation will take you through some steps, follow them. The first time you try this, it will likely not work, but it will tell you what you need to do. Here is what you will likely need to do (unless these were already installed on your server):

apt-get install build-essential
apt-get install zlib1g-dev
apt-get install libssl-dev
apt-get install libreadline5-dev

If you get the message telling you to install these things (or others), do it. And then, do this again:

ruby-enterprise-1.8.6-20090610/installer

  • Just go with all the defaults, unless you have a good reason for using something else.

When done, the installer will prompt you with a command to install some database gems. I am using MySQL, so will only install that one. You will also be told how to install Passenger, and this we will also do. If you followed the steps above, you don’t need to worry about these as you only need the mysql gem (which has been installed).

Passenger (and Apache)

REE told us to install Passenger, so let’s do that now:

/opt/ruby-enterprise-1.8.6-20090610/bin/passenger-install-apache2-module

Once again, the installation will likely ask you to install some additional things. This is what I get, and will install each as requested:

Apache and additional needed items

(This is what Passenger installer asked for.. you may get something different, so pay attention)

apt-get install apache2-mpm-prefork
apt-get install apache2-prefork-dev
apt-get install libapr1-dev
apt-get install libaprutil1-dev

And once more, let’s try Passenger installer:

/opt/ruby-enterprise-1.8.6-20090610/bin/passenger-install-apache2-module

Now, it should have worked. And Passenger will ask you to add the following to the Apache configuration file:

LoadModule passenger_module /opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/passenger-2.2.4/ext/apache2/mod_passenger.so
PassengerRoot /opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/passenger-2.2.4
PassengerRuby /opt/ruby-enterprise-1.8.6-20090610/bin/ruby

Before, we do that – let’s make ourselves happy and make sure our webserver is up and running! Go into your browser and enter your IP or domain name (unless you have done some DNS set up already – not in scope of this tutorial – then just use the IP).

You should see “It works!” in the browser. If you do not see this, then something went wrong. Go back and check that you have followed this section correctly. You may need to consult your host’s user guides.

You probably also saw a note on VirtualHosts. You can ignore this for now, we’ll get to it later.

Let’s move on. We need to add those lines to the Appache config file. So, let’s do that. The configuration file should be here: /etc/apache2/apache2.conf. Open this file (I will use nano as the editor, you can use whatever you like):

nano /etc/apache2/apache2.conf

Copy and paste these lines to the top of the file, save it, and close.

LoadModule passenger_module /opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/passenger-2.2.4/ext/apache2/mod_passenger.so
PassengerRoot /opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/passenger-2.2.4
PassengerRuby /opt/ruby-enterprise-1.8.6-20090610/bin/ruby

“gem”

What gems do I have? If you try “gem list” you will see a command not found. This is because we didn’t install the regular gem package, because we didn’t install standard Ruby (we installed Ruby Enterprise fork, remember?)

So, if you do this:
NOTE: Your REE version may be different – make sure you use the right directory, (you may need to modify the below line).

/opt/ruby-enterprise-1.8.6-20090610/bin/gem list

You should see the list of installed gems. Remembering/typing this entire line is not fun. So let’s create a symlink. Here, the symlink will use “gem” (the last thing in the below line) to refer to the location of the gem. If you already had Ruby and gems installed, you may want to use a different name (i.e. “reegem”).
NOTE: Your REE version may be different – make sure you use the right directory (you may need to modify the below line).

ln -s /opt/ruby-enterprise-1.8.6-20090610/bin/gem /usr/bin/gem

Now, if you do:

gem list

You should see all the gems installed, and can use it just like a regular gem command, just remember that gems will be installed in the REE folder. So, since I am using Rails 2.2.2 in this tutorial, let’s install that gem:

gem install rails —version=2.2.2

rsync

We will make use of rsync with Capistrano (don’t HAVE TO, but will). So let’s install that while here.
(if you don’t have this on your local machine, install it there too).

apt-get install rsync

Create the database

OK, everything looks good.
Let’s log into mysql and create the database we need for our application.

mysql -u root -p
CREATE DATABASE rollout_tutorial_production;

Create MySQL user

Since it’s a good idea not to use the root user for our application, let’s create another user.

CREATE USER ’tutorial_db_user’@localhost IDENTIFIED BY ‘password’;
GRANT ALL PRIVILEGES ON rollout_tutorial_production.* TO ‘tutorial_db_user’;
exit

Feel free to make sure the user was created:

mysql -u tutorial_db_user -p

Update the application configuration

Now that we have done this, let’s go back to our config/database.yml file on the develpment machine and update the production configuration there:

production:
adapter: mysql
database: rollout_tutorial_production
username: tutorial_db_user
password: password

Our server is ready (for now)!

Capistrano

OK, at this point this is what we have done:

  • Created a Rails project on our local machine
  • Created a git repository on our local machine
  • Set up Github to host or remote repository
  • Set up our production server

The next thing we need to do is set up Capistrano, to automate our deployment to production.

Create deploy user on the server

Capistrano will log into our production server. Best practice calls for a deployment user, will call the user “deploy” and then set up Capistrano to log in as that user.

SSH into the server as root (you may already be logged in), and then do this:

adduser deploy

Fill out all the relevant details.

Let’s see if that worked. Exit from the production server. And let’s re-logon with the deploy user.

ssh deploy@domain_or_ip_of_your_server

For now, exit, and log back on with root

ssh root@domain_or_ip_of_your_server

We will follow convention when install our application, which means we will install it to /var/www/apps/

You server should already have /var/www/
So let’s go there:

cd /var/www/

Let’s create the apps folder (remember, we’re logged on as root at the moment):

mkdir apps

The deploy user does not have permissions for this directory, so let’s set those (note that we need to use sudo here):

chown deploy /var/www/apps

OK, so now, the deploy user is set.

Git and Github

Since we’re using git, let’s make sure our server knows git. As root on the production server:

apt-get install git
apt-get install git-core

Let’s set some global settings. We can set these to be same as what you did on your local machine for setting up Github. These should be same as what you have set up in your Github account.

git config —global user.name “name”
git config —global user.email email

OK, one more thing to do before moving on to Capistrano itself. Capistrano will need to get into Github from our production server, because that is where our main git repository is stored. We need to do a couple of things to set this up – mainly setting up SSH keys.

NOTE: if ssh does not work, you need to install it with: apt-get install ssh

Log in as deploy user:

ssh deploy@domain_or_ip_of_your_server

Create the ssh key, use all default (hit enter, do not select passphrase here as that will complicate things):

ssh-keygen

Copy the key, and add it to you Github account. The generated key can be found like so:

cat ~/.ssh/id_rsa.pub

Copy that, then go to your Github account. Ckick on account, and then click on add another key (under SSH public keys), and add the key.

Let’s see if that worked.

We actually need to do this step whether you like it or not because we need to add Github as a known host on our server. So, type this (as the deploy user on the production server):

ssh git@github.com

Type “yes” when prompted. Now you should see a message saying you have been successfully authenticated. Great!

For security reason, let’s make sure no one else can edit the .ssh directory. Do this on the production server:

chmod 700 ~/.ssh

Use SSH key to log into your production server

It will be helpful, for you, and Capistrano, to be able to log into your production server without retyping your password every time. We can solve this by telling the production server your public SSH key on your local machine. Since we already created the key (for use with Github), we can just copy that to our production server. We can do that with one command.

From your local machine, do this:

cat ~/.ssh/id_rsa.pub | ssh deploy@domain_or_ip_of_your_server “cat >> ~/.ssh/authorized_keys”

Now, you won’t have to type your password everytime you log into your production server. To check, try this:

ssh deploy@domain_or_ip_of_your_server

Capistrano Installation

OK – finally, with all that leg work done, it’s time to set up Capistrano itself. On your local machine (you don’t need to install anything on the production server):

On your local machine, install capistrano:

sudo gem install capistrano

There is nothing to install on the production server. So, let’s move on.

Capistrano Setup

Let’s enable Capistrano for this project. On your local machine, while in your project do this (including the “.”)

capify .

All this really does is create a deploy.rb file in the project’s config directoy. This will contain the deployment instructions that Capistrano will follow when rolling out your code.

So, let’s take a look at it. Open config/deploy.rb. You should see something like the following:

set :application, "set your application name here"
set :repository,  "set your repository location here"
role :web, "your web-server here"

Plus some other things. Change the file so that it looks like this:

#	APPLICATION DETAILS #######################################
# name of the application
set :application, "rollout_tutorial" # this will be used to create the folder structure (see line)
set :deploy_to, "/var/www/apps/#{application}"
# Since this will rollout our code to the production server, state so explicitely:
#set :rails_env, "production"
#	GETTING INTO THE PRODUCTION SERVER ########################
# username of the user underwhich capistrano will log in,
# in this case, this is the deploy user we created earlier
set :user, "deploy"
# the deploy user has all the required permissions, so sudo is not needed
set :use_sudo, false
# you may, or may not need to use this - but let's use it just in case
ssh_options[:paranoid] = false 
# address of the production server, not that this is a variable
set :domain, "74.207.226.249" #"IP or domain name of production server"
# the above variable is used here.
# however, you can write these out - and may need to if you set up your environment
# in a way that puts these things on different servers
# however, in our exaple, everything is on the same server, so using the variable makes sense
role :app, domain
role :web, domain
role :db, domain, :primary => true
#	GIT AND GITHUB ############################################
# tell Capistrano that our version control is done with Git
set :scm, :git
# address of our Git repository, which happens to be on Github
# NOTE: the format of this is git@github.com:your_user_name/git_repository_name.git 
set :repository,  "git@github.com:monolith/rollout_tutorial.git"
# don't forget to change the above user name to yours!
# in Git, you can have many branches.  To stay consistent with the defaults (and out example), we are using master
set :branch, "master"
# this command means that only the changes will be updated (not entire code base)
set :deploy_via, :remote_cache
# where is the git command on the server?  stating it just in case
set :scm_command, "/usr/bin/git"
# TASKS #####################################################
# there are some things we'll want to do after the basic deployment has completed
# to do those things, we will create the tasks, and then call them in the end
# until this point, all the code that has been deployed, matches what you have in the (Github) repository
# since we excluded the config directory from our Git versioning (in order to not display or details publically on Github)
# we need to copy the files over to the production server
namespace :deploy do
  desc "Sync the config directory"
  task :sync_config do
    # this will sync files on your local machine with that on your production server
    # we need this for the files that we told git to ignore
    # notice that the domain variable is used (this should be the ip or domain of your app)
    # we set this variable way on top
    # also note that this is going to the shared folder, the symlink task below will link these from the release folder
    # make sure you install rsync on the server
    system "rsync -vr --exclude='*~' config #{user}@#{domain}:#{release_path}/"
    # we'll need the tmp file to restart Passenger (see below)
    system "rsync -vr --exclude='*~' tmp #{user}@#{domain}:#{shared_path}/"
    # and also let's sync the db folder - notice this goes in the current folder
    system "rsync -vr --exclude='*~' db #{user}@#{domain}:#{release_path}/"
  end
  desc "Tell Passenger to restart the app."
  task :restart do
    run "touch #{shared_path}/tmp/restart.txt"
  end
  # Note that the default deploy:start task looks for a script/spin script  
  # to run. If you are using a deployment method that doesn't need script/ 
  # spin, then you'll need to override deploy:start
  # http://www.mail-archive.com/capistrano@googlegroups.com/msg04819.html
  deploy.task :start do
     # nothing
   end
end
after 'deploy:update_code', 'deploy:sync_config', 'deploy:restart'

After copying the above into the config/deploy.rb file, make sure you read over it, and make any necessary changes.

Once that is done, let’s tell Capistrano to set up. What this will do is create a folder structure on your production server.

On the local machine, from your project, do this:

cap deploy:setup

NOTE: the “deploy” command above does not refer to the user you created on the production server, it is a Capistrano command.
To see all commands available, type “cap -T”

OK – Let’s log into the production server and make sure that Capistrano did something…

ssh deploy@domain_or_ip_of_your_server
cd /var/www/apps
ls

You should see a directory for your project here. Let’s see what’s inside:

cd rollout_tutorial
ls

You should see two directories: releases and shared.

Let’s move on. Let’s do a check from our local machine to make sure Capistrano thinks everything is OK:

cap deploy:check

You should now see “You appear to have all necessary dependencies installed.” Great! (If not, you should have some tips what to fix, but if you are not sure – make sure you have followed all the steps in this tutorial).

We have not rolled out the code yet. So let’s do that now. From the local machine, do this:

cap deploy:cold

Look through what Capistrano output, you may see some errors. If you do, you need to address these. For example, if you followed this tutorial exactly, you are likely to see a rake not found error. So let’s fix that. Log into the production server as root:

ssh root@domain_or_ip_of_your_server

Remember, you should be root – if not, you’ll need to permission deploy user to write to the REE bin and lib directories.
Now, check if rake is installed, and install it if needed. You should also check that each of the following are installed – and install them if you think they are needed: rake, irb, ruby (since this is for production, you likley only want to add rake).

On your production server:

gem list
gem install rake

Remember, this rake gem is installed in the REE folder. Always pay attention to which gems your are using and where they are stored, especially if you installed Ruby – and now have to version of it (regular Ruby and REE). If you have both versions, you may want to create useful symlinks.

Here is a useful symlink (that we need for this tutorial since there is no other rake installed… and Capistrano will call the rake command).

You should do this as root, just in case. On your production server (we did this for gem earlier if your remember):

ln -s /opt/ruby-enterprise-1.8.6-20090610/bin/rake /usr/bin/rake
ln -s /opt/ruby-enterprise-1.8.6-20090610/bin/irb /usr/bin/irb
ln -s /opt/ruby-enterprise-1.8.6-20090610/bin/rails /usr/bin/rails
ln -s /opt/ruby-enterprise-1.8.6-20090610/bin/ruby /usr/bin/ruby

Finally, from the local machine, let’s try Capistrano deployment again:

cap deploy:cold

Still getting errors? This is where it get’s tricky. You may need to google these problems.

Here are the additional steps you may needed to do to get it all working:

  • In config/environments/production.rb, comment out: config.action_view.cache_template_loading = true
  • You may be missing gems on the production server. To fix that, either install them or freeze them in your project. The recommended solution is to freeze them (on your local machine, inside your project do this: freeze gems. This will freeze them into your vendor directory).
  • If you initially created with a rails version > 2.2.2, and you then changed it to 2.2.2 in the environment settings, then you need to rename application_controller.rb to application.rb (and then make sure it’s included in the git repository).
  • Remember to commit any changes you make with git.
  • Look over the deployment recipe above, you may need to modify it.

Virtual Host

OK, once everything deployed with no errors, let’s check out the website. Go to your domain in the browser.

What you should see (if you followed the tutorial) is “It works!” There is a reason that’s there, and not our application. We need to modify the Virtual Host settings on the server. Log into your production server:

ssh root@domain_or_ip_of_your_server

Let’s look at the configuration file.

nano /etc/apache2/sites-available/default

There is a lot more to this that you should really learn. But, for purposes of this tutorial, let’s do something quick and easy.

Modify the file so that it only has the following:

NameVirtualHost *
<VirtualHost *>
        DocumentRoot /var/www/apps/rollout_tutorial/current/public
        ErrorLog /var/log/apache2/error.log
</VirtualHost>

Note that the DocumentRoot is now pointing to you application/current/public directory.

After saving this, we need to restart Appache:

apache2ctl restart

Now go back to the browser and refresh.

The application should now be there and work. Congradulations, you made it!

Further updates

You will need to learn a lot more about Git, Github, Capistrano, etc. But let’s do a couple of quick changes just to get comfortable.

Updates not requiring a database migration

Let’s do a minor update to our aplication. On your local (development) machine, open the app/views/things/index.html.erb file.

Change the title to “Lots of Things”

After saving, let’s check out git status.

git status

You should see “modified: app/views/things/index.html.erb”

Great, let’s commit this change:

git commit -a -m “changes title of things index page”
git push origin master
cap deploy

That should have worked! Both Github and your production server should be updated… and if you refresh your browser, you should see the change!

Migrations

Let’s make a change that requires a migration. On your development machine:

script/generate migration addDescription

Open the file, and and save it with this:

class AddDescription < ActiveRecord::Migration
  def self.up
    add_column :things, :description, :string
  end
  def self.down
    add_column :things, :description, :string
  end
end

And let’s update app/views/things/_form.html.erb by adding the description fields. This file should now look like:

<% form_for @thing do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </p>
  <p><%= f.submit "Submit" %></p>
<% end %>

And update the index page to show description. app/views/things/index.html.erb

<% title "Lots of Things" %>
<table>
  <tr>
    <th>Title</th>
  </tr>
  <% for thing in @things %>
    <tr>
      <td><%=h thing.title %></td>
      <td> Desc: <%=h thing.description %></td>
      <td><%= link_to "Edit", edit_thing_path(thing) %></td>
    </tr>
  <% end %>
</table>
<p><%= link_to "New Thing", new_thing_path %></p>

Now, do this:

git commit -a -m “added description to things”
git push origin master
cap deploy:migrations

Notice that we used deploy:migrations here. Check your production website now.. it should be updated!

On your development machine:

cap -T
cap —help
git —help

This should give you some ideas.

But this tutorial has come to an end. There is so much more to learn. Hopefully this was helpful in getting that started!

Useful Links

Git

http://git-scm.com/
http://www-cs-students.stanford.edu/~blynn/gitmagic/

Github

(you’re here!)
Check out the Guides section

Capistrano

http://www.capify.org
The Absolute Moron’s Guide to Capistrano

Ruby Enterprise

http://www.rubyenterpriseedition.com/
Adding Ruby Enterprise Edition to multiruby versions

Passenger

http://www.modrails.com/

Screencasts

Scott Chacon Rails with Git/Capistrano – focus on Git
Railscasts – Git on Rails
Railscasts – Capistrano Tasks
(not free, but good) Peepcode – one screencast on Git, one on Capistrano

Other useful links

Nifty generators (used in the tutorial app)

http://github.com/ryanb/nifty-generators/

VirtualHost setup with Apache

http://www.debian-administration.org/articles/412

http://httpd.apache.org/docs/2.2/vhosts/examples.html

http://httpd.apache.org/docs/

Releases

No releases published

Packages

No packages published