Checklist for Running Ruby on Rails on Bluehost

Kudos to Bluehost for adding Rails hosting, but you might have found getting more than a trivial example running is an exercise in frustration. The following checklist details at least one successful path to getting a full Rails app up and running. (Much of the basic outline comes from the following Bluehost knowledgebase article, but it tends to leave out some details, especially newer things like ActiveRecord migrations.)

Setup Directories on Bluehost

Bluehost suggests setting up a working directory for your rails projects. Reasonable enough just use the wonderful CPanel ssh terminal and create it at the top level of your home directory:

% cd ~
% mkdir rails
% cd rails

Next you want to startup your first rails project. This may seem a bit bizarre, since you may have a perfectly good rails project already. Lets just assume you want to upload the sample project from Agile Web Development with Rails, the Depot project.

% rails depot
% cd depot

Point a New Subdomain to Your Rails App

Now it’s time to setup a subdomain for your little application. Bluehost gives you plenty of these so one per rails app is no big deal. We’re just going to assume we’re using example.com so the final subdomain will be depot.subdomain.com. Just go into CPanel and add depot as a new subdomain. Now we want to point to it in our ssh session again:

% cd ~/public_html
% ln -s /home/YOUR_USERNAME/rails/depot/public depot

As a test that things are going right so far you can check out http://depot.example.com/ and you see the familiar rails default page. Nice but not exactly what you were hoping for.

Setting Up the Databases

First you need to add a database user. cPanel prepends your own Bluehost username to everything so just be aware. Add the database user by going to cPanel choosing the MySQL databases and adding a user. If you pick ‘rails’ the name will be

1
username_rails

.

Next, just add two databases dev and prod. You won’t need a test database probably because you won’t be running your unit/functional tests on the Bluehost server. So same MySQL link in cPanel and just name it ‘depotdev’. (You can’t use an underscore since it just ignores it anyway.) Since it appends the username it will become

1
username_depotdev

. Then you repeat the steps again for the production database and name it ‘depotprod’ so that you have another database

1
username_depotprod

.

Finally you give the

1
username_rails

user access to the the two databases

by first selecting

1
username_rails

from the user pulldown menu and then

1
username_depotdev

from the database pulldown. Go ahead and give it all permissions and click on ‘Add User to Db’. Repeat for

1
username_depotprod

.

Configuring the Database Connection

Of course here you’re going to edit

1
config/database.yml

on your local machine. It should look something like:

development:
  adapter: mysql
  database: username_depotdev
  username: username_rails
  password: password
  host: localhost
production: adapter: mysql database: username_depotdev username: username_rails password: password host: localhost

Then FTP the file up and replace

1
~/rails/depot/config/database.yml

.

Migrating the Database Schema

Migrating your database using ActiveRecord migrations starts with FTPing over everything in your

1
depot/db/migrate

directory locally. Then log into your server using the cPanel SSH client and run:

% cd ~/rails/depot
% rake db:migrate
% rake db:migrate RAILS_ENV=production

Both of the migrate commands should show some schema migration logging comments. If not you probably need to check the syntax in your database.yml file for errors. This migrates the schema to both the dev and production instances.

Migrating the Application Code

Next step is to FTP over all of the files in

1
depot/app

on your local machine to the server at

1
~/rails/depot/app

.

Since you’re application may need some sample data you can add it through phpMyAdmin, SQL scripts, or a Rails scaffolding.

Switching to a Default Production Environment

Rails by default assumes you’re running in development mode. There are lots of nice ways to change the RAILS_ENV environment variable, but since Bluehost is a hosted solution there is a pretty simple hack that is guaranteed to work. Simply uncomment the following line in your

1
config/environment.rb

file:

# Uncomment the line below to force Rails
# into production mode when you don't
# control web/app server and can't set it
# the proper way
# ENV['RAILS_ENV'] ||= 'production'

Then FTP the file over to your Bluehost account. You’ll probably want to comment out the line in

1
environment.rb

again for you local development.

Positive Feedback

Just point your web browser at the URL assuming you have controller named ‘store’ and a method named ‘list’.

http://depot.example.com/store/list

Now do a little victory dance or eat some M&Ms, your choice.