Rails + Capistrano + git + BlueHost
UPDATE: I’ve moved from BlueHost, as their quality sucks and techsupport is unable to offer any solution for timeouts/500/503 HTTP errors.
But sometimes you want to run your app with a set of different programming languages, frameworks, and databases. This is something that a typical PaaS provider can’t offer, so you need a general-purpose hosting.
Important Note: BlueHost charges you for the whole 1 or 3 years – depending on the plan you choose – immediately. So if you want per-month subscription, you have to contact them directly. In this case they charge $30 “setup fee”.
After you’ve successfully purchased your brand new BlueHost account, you have to make some additional tweaks.
BlueHost has “Ruby on Rails” button in cPanel, but you can safely leave it alone – it doesn’t do anything useful:
What we need to tweak, though, is our ~/.bashrc file. Add these lines to the bottom of this file:
1 2 3 4 5 6 export HPATH=$HOME export GEM_HOME=$HPATH/ruby/gems export GEM_PATH=$GEM_HOME:/usr/lib64/ruby/gems/1.9.3 export GEM_CACHE=$GEM_HOME/cache export PATH=$PATH:$HPATH/ruby/gems/bin export PATH=$PATH:$HPATH/ruby/gems
In order to conveniently deploy our projects to BlueHost over SSH, we would need to add our public key to the list of allowed keys.
To do so, we have to edit
~/.ssh/authorized_keys file on BlueHost, adding there contents of our local
Now we can login to BlueHost from our local host without a password prompt. More on SSH keys generation and passwordless login is here.
In case you never worked with git before, here is a brief crash course:
Setup an empty git repository in your Rails application under a root folder: $ git init .
Add all project files into a repository: $ git add .
git commit -m "Initial commit"
That’s pretty much it – your git repository is ready. If you want more details, check this great book.
Capistrano + Rails Setup
Capistrano is nothing more than another Rails gem. So we just add it to our Gemfile:
1 gem 'capistrano'
And install it:
1 $ bundle install
Now we have to initialize Capistrano configuration:
1 $ capify .
This command creates several files, which Capistrano needs to work properly. One which is of particular interest for us is config/deploy.rb. Default settings make no sense for our setup, so we re-create this file from scratch:
The final touch is to tune
public/.htaccess file to let BlueHost mod_rails know how to treat our web app. Don’t forget to change
user_dir to the real folders:
1 2 3 4 5 Options -MultiViews PassengerResolveSymlinksInDocumentRoot on RailsEnv production RackBaseURI / SetEnv GEM_HOME /home_dir/user_dir/ruby/gems
Now we have everything in place. Let’s go:
1 2 3 4 5 $ git add . $ git commit -m 'Add Capistrano' $ cap deploy:setup $ cap deploy $ cap deploy:migrate
That’s it! Don’t forget to commit your changes and then use
cap deploy or
cap deploy:migrate (for DB migration) command every time you need to push a new version to your BlueHost hosting.