Utilizing OpenShift To Deploy a Web Application
Early in my career as a web developer, I wanted to be able to demo my personal projects. In the last few years, I found a platform as a service (PaaS) provider called OpenShift (https://www.openshift.com/).
For a web developer, creating the application and running it locally tends to be easy. The tricky part comes in when you want to show others what you have done! OpenShift provides a 100% free solution to this problem.
https://www.openshift.com/products/pricing/plan-comparison
This blog post will walk a user from account creation through application deployment in a step-by-step process.
Dependencies
There are a few things we will assume we already have ready for use:
- Git
- Eclipse
Also, I will be approaching this tutorial with the use of a Mac. Using cygwin on a Windows machine, steps would be comparable.
Creating an OpenShift Account
All you need is an email account!
For the purposes of this tutorial, I am going to create a new Google Gmail account (https://accounts.google.com/SignUp). New email! openshift.dev3l@gmail.com
- Navigate to the OpenShift site and fill out the new account form
- Follow the "What's Next" steps
- Open up your email account and click the "Activate Account" link
- On the page that is opened, I unchecked the notifications checkbox
- Click the "I Accept" button
- On the following page, click the link "Create your first application now"
Let's Gear Up!
After completing the last step in the previous section, we find ourselves on the "Gears" page.
The free account allows up to three out-of-the-box solutions. For now, let's pursue Tomcat7 as our host container of choice.
- Under the Java section, select Tomcat7
- Fill in the public URL and click the "Create Application" button at the bottom of the page
- Important: The second box in the public URL will not be modifiable for other applications! Future Gears will have this pre-filled with the value you enter now
- Click the "Not now, continue" link on the next page
HelloWorld Web Application
We need a .war to deploy to our new Tomcat7 application server!
Let's create a "Hello World" application in Eclipse...
- Start Eclipse and open a workspace of your choosing
- From the top menu select: File -> New Dynamic Web Project
- Name the project HelloWorld and select
for Target runtime - Click Next, Click Next
- Select the checkbox for "Generate web.xml deployment descriptor"
- Click Finish
- In HelloWorld/WebContent directory create a jsp file called "index.jsp"
- Using the Eclipse file creation wizard for JSP will fill in some of the details
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<head><title>Hello World</title></head>
<body><h1>Hello World</h1></body>
</html>
- Right-click on the HelloWorld project and select: Export -> WAR file
- Select a destination and click Finish
Fingerprints?
Now that we have an available server and an application to deploy, we have to get a little bit more friendly with our OpenShift server.
I have generated ssh keys on my computer already, but for those who have not, the process is well defined in the following article: manually-generating-your-ssh-key-in-mac-os-x
ssh-keygen -t rsa
# follow steps that follow
# ...
pbcopy < ~/.ssh/id_rsa.pub
# copy the key
Generating the ssh keys on Windows is a slightly different process. The OpenShift documentation walks a user through the steps. https://developers.openshift.com/en/getting-started-windows.html
With SSH keys in hand, let's add them to our server!
- On the Applications dashboard page, click the link "add an SSH public key to your account"
- On the command line, run the pbcopy command
- Fill in the name field with whatever you like, paste the copied value into the text area, and click the Create button
- Navigate back to your tomcat application once the key has been uploaded
- To the right, there should be a "Source Code" ssh address
Deploying With Git
Now that we have access to our source directory we can push updates to it!
In the following code snippet, we will use git to clone our directory, move our HelloWorld.war to the deployments directory, commit, and push the application to the server.
git clone ssh://5568aaad5973ca2cc4000156@tomcat-dev3ldemo.rhcloud.com/~/git/tomcat.git/
# the ssh url is the one I copied from my dashboard
cd tomcat
mv /Temp/HelloWorld.war ./webapps/
# place the HelloWorld.war in the webapps directory to be deployed
git add *
git commit -m "HelloWorld"
git push
# app deploys!
Profit!
Once the app completes its deployment, it should be publicly accessible.
http://tomcat-dev3ldemo.rhcloud.com/HelloWorld/
NOTE: Due to this being the "free" tier, the application will sleep after prolonged periods of inactivity. OpenShift offers premium subscriptions that assure the platform is always turned on.
Get In Touch
We'd love to hear from you! Whether you have a question about our services, need a consultation, or just want to connect, our team is here to help. Reach out to us through the form, or contact us directly via social media.