Monday, November 5, 2012

Creating Tomcat application on OpenShift

OpenShift

OpenShift is JBoss Cloud solution. I like OpenShift better than Google App Engine because it provides real RDBS if you select MySQL or PostgreSQL, so you can develop simple applications that could be later transferred to some other server.
OpenShift doesn't provide Tomcat out of the box like it does for JBoss AS, PHP or Ruby, but you can install it trough DIY cartridge.

Connecting to OpenShift

Opening account on OpenShift is trivial, I used TOMCAT promo code but you can find a lot of other codes (I don't know what promo code exactly do but - whatever)
 
I have followed tutorial for installing Tomcat on OpenShift with few differences that I will describe here.
First of all, I didn't install RHC client tool because I created application using web interface and all other actions were done trough Eclipse (with git), SSH (Putty) and WinSCP.
Instructions for setting connection with Putty is described here, and WinSCP setup is almost identical.

After I confirmed my account, I opened Create Application tab and selected Do-It-Yourself web cartridge. Configuring application  name and namespace is trivial and you shouldn't have trouble there.  

Shutting Down default Ruby application

We can check what application is running with command ps x

> ps x

PID TTY STAT TIME COMMAND
4682 ? S 0:00 sshd: 6e35702606914790aaddacd6e3eeb7ca@pts/0
4683 ? Ss 0:00 /bin/bash --init-file /usr/bin/rhcsh -i
10884 ? S 0:00 sshd: 6e35702606914790aaddacd6e3eeb7ca@notty
10885 ? Ss 0:00 /usr/libexec/openssh/sftp-server
24328 ? S 0:00 ruby /var/lib/stickshift/6e35702606914790aaddacd6e3eeb7ca/app-root/runtime/repo//diy/testrubyserver.rb 127.8.212.129 /var/lib/stickshift/6e357026069147
24341 ? R+ 0:00 ps x


We can then stop running server by going into directory app-root/repo/.openshift/action_hooks and running script stop or just by killing ruby process.
./stop

Installing and configuring Tomcat

After we have stopped server this way we can continue following tutorial for installing Tomcat.

I made all changes directly trough Putty, but you should be able to do everything with Eclipse and git plugin

My start script:
#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_INTERNAL_IP:8080
set -x
cd ~/tomcat/data/apache-tomcat-7.0.32/
sed -ig 's/OPENSHIFT_INTERNAL_IP/'$OPENSHIFT_INTERNAL_IP'/g' conf/server.xml
bin/startup.sh


My stop script:
#!/bin/bash
# The logic to stop your application should be put in this script.
~/tomcat/data/apache-tomcat-7.0.32/bin/shutdown.sh


You can now start and stop tomcat server by going into ~/tomcat directory and running:
[tomcat-javafiend.rhcloud.com tomcat]\> ./tomcat_ctl.sh start
or
[tomcat-javafiend.rhcloud.com tomcat]\> ./tomcat_ctl.sh stop

That's it! You can start your Java fun with Tomcat!

No comments:

Post a Comment