Rails Kitchen

It's a place to write on stuff I learned recently.

Deploying Ruby on Rails Application to Cloud Application Platform - Openshift

| Comments

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. It is open source and written in Ruby.
To get started 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. Once you are signed up, install the OpenShift RHC Client Tools by running these commands in a terminal
1
2
gem install rhc
rhc setup
We can deploy rails application by adding OpenShift as a remote repo.
1- Create a new application in oppenshift account, then get the git URL for your new application. This was shown to you when you created your application,through the web console
2- Add your OpenShift repo as a remote repo
1
git remote add openshift <OpenShift repo URL>
3- Configure Database
Since your database address may change, you will need to access it using an environment variable. A random username and password were also generated for you when your application was created. But you don’t need to hardcode it into your application, there are environment variables for that too! Add this configuration to database.yml file
1
2
3
4
5
6
7
8
9
10
production:
  adapter: mysql2
  encoding: utf8
  database: <%=ENV['OPENSHIFT_APP_NAME']%>
  pool: 5
  host: <%=ENV['OPENSHIFT_MYSQL_DB_HOST']%>
  port: <%=ENV['OPENSHIFT_MYSQL_DB_PORT']%>
  username: <%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%>
  password: <%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%>
  socket: <%=ENV['OPENSHIFT_MYSQL_DB_SOCKET']%>
4- Now that your app is configured properly, it’s time to deploy it to OpenShift. To do that, simply run a git push
1
git push openshift master
If everything went well, your app should deploy and be accessible. If not, pay attention to the output from the git push, if anything failed, it will tell you there.
If your application requires some persistent directory for your data then you can use directory (app-root/data/) you can access this directory in your application using environment variable OPENSHIFT_DATA_DIR
5- Managing apps:
To ssh into your server, type:
1
rhc ssh app-name
Navigate to App folder:
1
cd app-root/repo
Migrate Database:
1
bundle exec rake db:migrate RAILS_ENV="production"
If you want to associate your own domain name eg. (www.yourdomain.com) with your Openshift rails application URL then you will need to create URL alias as shown below
1
rhc alias add railsapp www.yourdomain.com
then change cname records in your DNS provider account.

Comments