Tidbits on software development, technology, and other geeky stuff

Ruby on Rails in CentOS 5

I recently started playing around with Ruby on Rails 3.1 to see what all the hype was about.  I’ve been working on a website for our neighborhood and decided to use RoR for it so I can learn it.  Rather than going the Heroku route, I wanted to host the site on my Amazon EC2 instance running CentOS 5 64bit.  Getting it setup on my server was a bit of a chore mostly because CentOS doesn’t have the latest and greatest packages.  Here are the steps I took to get a Rails site up and running.

Ruby

The latest package offered by yum in CentOS 5 is too old to work with Rails 3.1 so we must build a newer one.

cd /tmp
wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p290.tar.gz
tar -zxvf ruby-1.9.2-p290.tar.gz
cd ruby-1.9.2-p290
./configure --prefix=/opt/ruby192 && make && make install
export PATH=$PATH:/opt/ruby192/bin
echo 'export PATH=$PATH:/opt/ruby192/bin' > /etc/profile.d/ruby192.sh

RubyGems

cd /tmp
wget http://rubyforge.org/frs/download.php/75309/rubygems-1.8.10.tgz
tar -zxvf rubygems-1.8.10.tar.gz
cd rubygems-1.8.10
ruby setup.rb

Rails

gem install rails

SQLite3

Although CentOS 5 has SQLite3 installed out of the box it is an older version so we must build a new one.

cd /tmp
wget http://www.sqlite.org/sqlite-autoconf-3070800.tar.gz
tar -zxvf sqlite-autoconf-3070800.tar.gz
cd sqlite-autoconf-3070800
./configure --prefix=/opt/sqlite3 && make && make install

Passenger

Phusion Passenger makes running rails apps with Apache a cinch.  Run the following:

gem install passenger
passenger-install-apache2-module

The installation has nice walk-through instructions.  Basically, after the install, you will be placing the following in a new apache config file such as /etc/httpd/conf.d/rails_whatever.conf

LoadModule passenger_module /opt/ruby192/lib/ruby/gems/1.9.1/gems/passenger-3.0.9/ext/apache2/mod_passenger.so
PassengerRoot /opt/ruby192/lib/ruby/gems/1.9.1/gems/passenger-3.0.9
PassengerRuby /opt/ruby192/bin/ruby

<VirtualHost *:80>
 ServerName www.yourhost.com
 DocumentRoot /opt/somewhere/public    # <-- be sure to point to 'public'!
 <Directory /opt/somewhere/public>
   AllowOverride all              # <-- relax Apache security settings
   Options -MultiViews            # <-- MultiViews must be turned off
 </Directory>
</VirtualHost>

New Rails Site

cd /opt
rails new somewhere

So at this point I got a bundle error about problems installing sqlite-ruby. Don’t fret.  Add the following to /opt/somewhere/Gemfile below gem ‘rails’, ‘3.1.1’:

gem 'execjs'
gem 'therubyracer'

Now run the following:

bundle config build.sqlite3 --with-sqlite3-include=/opt/sqlite3 --with-sqlite3-lib=/opt/sqlite3
 --with-sqlite3-dir=/opt/sqlite3
bundle update
chown -R apache /opt/somewhere

Create Database

rake db:migrate RAILS_ENV="production"

Fire it Up

service httpd restart

Open a browser and go to www.yoursite.com.  If you see the Rails test page then you are up and running!

Discuss on Twitter