After developing my local app using Spring MVC and hibernate I decided to put it on openshift.
The technology stack of my app is:
1. Spring MVC v3.2.2.RELEASE
2. Hibernate v3.5.4-Final
7. Twitter Bootstrap for Responsive Web UI.
OS :Ubuntu 12.04
The source code can be checkout from here : email@example.com:anirudh83/TaskMaster1.1.git
Now, my app is build with maven and can be ran using mvn tomcat:run , as tomcat plug in installed in pom.
And the code is checked into my git repository.
So, Now lets deploy our application on OpenShift.
1. Sign Up to OpenShift : OpenShift lets you create 3 free apps to try. So go to https://www.openshift.com/ and create an account.
2. Select Tool to upload.
You can either use web interface to upload your war or use CLI.
I prefer to use CLI so that I can just push my code to cloud and it deploys automatically.
Just like heroku you can upload your code to openshift git repo, and from there it builds and deploys automatically.
Below were the steps:
1. Install RHC on ubuntu 12.04 – sudo gem install rhc
2. After that run rhc setup – sudo rhc setup
Make sure you have ruby installed on your machine.
See the documentation here : https://www.openshift.com/developers/rhc-client-tools-install
3. Creating and adding SSH keys
While running rhc setup ; the wizard will guide you to enter your openshift username/password and create SSH keys ,So you need not worry about this step.
4. Creating first app :
Before you create your first app, you would need to create a namespace for it. Openshift creates your app as : appname-namespace-rhcloud.com
To create a namespace go to https://openshift.redhat.com/app/account
I created the name space as anirudhbhatnagar .
Next step would be to create our first application. Our application would be deployed on Tomcat and would be using MySql Database, So we would need support for those in our application.
Cartridge : Any plugin, feature, database or application support as Infrastructure like MySQL,JBoss,Tomcat,MongoDB,Node.JS, Php ,Ruby can be added as a “Cartridge” in OpenShift.
You can actualy build your custom cartridge as well. See details here ; But we are not discussing that now.
So,In order to build our application on openshift we need these Cartridges to be added.
For our application we would need 2 cartridges, Tomcat 7 and mysql database 5.1.
When you start creating your app on openshift management console it asks you to add the cartridge on which your application would be build.
Go to the Management Console page, and create your first app, And add the cartridge : Tomcat7
(Dont worry we can add more cartrdiges later,but to begin we need minimum this).
Now create the app with the name “taskmaster”
So, now the complete URL becomes http://taskmaster-anirudhbhatnagar.rhcloud.com
I have my source code checked into my git, so I will provide the git URL of my repo to be put in SourceCode section in the management Console. (https://github.com/anirudh83/TaskMaster1.1.git )
Then press : Create Application, It will checkout the code from your git repo and create the app. It might take a while so be patient.
5. Adding and configuring database :
Once your application is created, click on the link of your app in “All Applications” page which will open a dashboard for your app.
Below it, we can find the button “Add Cartridge”, Press the button to add another cartridge and select MySql and add it.
Once mysql database is added it will show you the credentials for mysql, url,username and password, store it carefully.
Add these credentials into your database properties file to create db connections.
Integrating with git:
When you create an app on openshift it provides you with a git repository, so if you have not already hosted your code on git, you can make use of this git repository. Whenever you will push into it. An automated build would get triggered.
In case you already have your code on git, you can provide the git URL in the online settings page of openshift while configuring the app. It will take the source code from master branch and will build it for you.
On the application dashboard page in management console, We can see the git URL, this is the repository on which openshift hooks are listening to trigger the build.
So we would need to clone this repo in our local.
As I already have my git repo and dont want to store my code in openshift’s git repo.
I can just put my built war in the “webapps” folder and push the code in git repo of openshift.
If you are building my application, you would need to do the following changes:
a.) In applicationContext.xml you have to change the property of
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <!-- <value>/properties/database.local.properties</value> --> <value>/properties/database.dev.properties</value> </property> </bean>
b.)In database.dev.properties: You would need to add the username,password and openshift URL for DB.
c.)Save this and run mvn clean package to package the war.
d.)Copy this war from target folder and paste it inside webapps folder and push it to openshift’s git repo.
It will automatically trigger the build and deploy my app.
We could install the php-mysql-admin cartridge so that we can view the database online using php-mysql-admin console.
Now you can hit the url and see your application there.
You can checkout my application at
Note: In case the URL is broken, it might mean that I have not used this app for some time and openshift has cleaned this or my instance is turned off.