Deploying Ruby on Rails Application to OpenShift PaaS

OpenShift is a Platform as a Service (PaaS) from RedHat. It’s great for deploying web applications as you can setup/scale/manage apps quickly without any hassle, just like Heroku, but the OpenShift platform is available as a free and open source software, so you’re not locked in (you can install OpenShift on your server and create your own private PaaS if you want).

Previously, I’ve already written on various deployment options available for a rails application, also on deploying rails app to vps. In this article, I’ll guide you in deploying your Rails application to OpenShift.

Step 1. OpenShift Setup

First of all, Create a free Account. You get 3 small gears (resource container/unit : one small gear is equivalent to 512 MB RAM and 1GB storage) for free. If you need more, you can upgrade to premium plans.

Install rhc tool

gem install rhc

Note : If you use rbenv for managing ruby then you also need to run `rbenv rehash`.

rhc_setup

create token and upload public keys

rhc setup

And follow the instructions. Once the setup is completed, you can easily create/manage your apps using this client utility (rhc).

Just type :

rhc

to see available options. If you want to see all the cartridges (application environment e.g ruby, php, python etc) available, just type :

rhc cartridges

Create a Ruby on Rails App [openshift]
Run this command from the parent directory of your project (~/parent_directory/project) or it will create a directory inside your app.

Note : joblee is the name of the Rails app we’re going to deploy. So, replace it with the name of your app. And I assume you’re using Postgresql for database.

rhc app create ruby-1.9 -a joblee

Add database cartridge

rhc cartridge add postgresql-9.2 -a joblee

Add ‘pg’ to your Gemfile. And run bundle install. Although, you’ll receive database details in the end (above command) but it’s a better to use openshift environment variables in database.yml. So, update the database.yml accordingly. Something like this one : production config for database.yml (openshift)

Step 2. Preparing the application for deployment

Once the openshift is setup, move to your project directory and setup git push deployment for your app. rhc show app rails

cd joblee
rhc show-app joblee

And get the value of Git URL from the above command. Now, add the remote url for deployment. (replace GIT_REMOTE_URL with the value you got above)

git remote add openshift GIT_REMOTE_URL

Now, merge the remote repo with your development repo.

git pull openshift master

Then, you need to manually fix conflict in config.ru. Keep the default content of config.ru file.

 git add .
 git commit -am "fixed merge and now getting ready for deployment"

Step 3. Deploy

git push openshift master

That’s all :-). Now, your app should be live at : http://your_app_name-domain.rhcloud.com.

Note : If anything goes wrong, you can always ssh into your app server and fix things there. To ssh into your server, type :

rhc ssh joblee

Managing apps
ssh into the app server and type

ctl_app

to see lots of options available. You can also get rails console, just by typing :

cd app-root/repo
bundle exec rails console RAILS_ENV=production

Postgresql Database Setup

cd app-root/repo
RAILS_ENV=production rake db:setup

Assets compilation problem
I had to run few other commands on server due to some gems/assets related problems. But you can add it to openshift deployment hooks like this example rails app, so you won’t have to manually execute it every time you deploy the app.

RAILS_ENV=production bundle
RAILS_ENV=production bundle exec rake assets:precompile

Adding Custom Domain
First, add openshift app url (app-domain.rhcloud.com) as a cname record for www and the IP Address 174.129.25.170 (using the free redirection service) for root record. So, it will redirect root domain to www version.

Now, register that domain with rhc server.

rhc alias-add joblee www.joblee.in

If you don’t want to use www domain, then you should type this instead : (and you also need to add cname record for root domain, at your domain registrar)

rhc alias-add joblee joblee.in

Note : I’ve setup naked domain so, www version of the domain is getting redirected(301) to the root domain. Not all DNS providers supports this (I’m using namecheap and I can specify openshift app url as a CNAME record for the root record) and sometimes it can cause some weird behaviours, especially if you’re using email with the domain. (read more on how it can break MX records)

Join the Conversation

8 Comments

  1. Openshift is a nice option for Deployment, nice community and you get lot of info from their forums. Nice post, keep it up 🙂

  2. I think the limitations of the available cartridges, the hurdle to create custom ones, and Openshift still being relatively new, is ideal for those that want to make a custom Heroku alternative for their users so that their users can deploy their own app for you to host. Or if you want a very simple rails app, deployed. Though Openshift is open, you’d still have to pay for a server to host it.

  3. Great post! I have a problem though. Everything works until the end. When I try to go into the created app I get an error from Passenger saying “production database is not configured (ActiveRecord::AdapterNotSpecified)”. Do you know what I might have missed?

  4. The ‘bundle’ commands fail for me, I have a ruby 2.0 cartridge and when ssh-ing and executing the “RAILS_ENV=production bundle exec rake db:setup” command it says “Your Ruby version is 1.8.7, but your Gemfile specified 2.0.0” although when running “ruby -v” it correctly states ruby 2.0.0p353 (2013-11-22) [x86_64-linux].
    Any idea on what’s the problem?

    1. Came across this recently, and while my answer likely doesn’t help tuxcayc from 6 months ago, I also didn’t know what to do.

      Apparently, bundler uses it’s own version, so if you do a “bundle exec ruby -v” in the app-root/repo directory, you’ll see what bundler references as the existing ruby version. It likely is inconsistent with the “ruby -v” results.

      To fix this, you need to install the bundler gem again with the preferred version of ruby, as you’ll notice it doesn’t get listed when you do a “gem list”. So, a simple “gem install bundler” should work, and then log out and back in.

      Your commands should start working properly after that.

  5. This guide helped me get up to speed a year after it was written. I ran into a few issues, but I worked those out. The one thing I didn’t catch until later on was that the host and port values have a leading ‘$’ in them and caused issues when I attempted to use them without removing those characters. Everything worked just fine when I did.

    Thanks!

Leave a comment

Your email address will not be published. Required fields are marked *