Sunday, January 2, 2011

How to setup Java + Tomcat + MySQL on Amazon EC2

As you may know, by the 1st November of 2010, Amazon launched Free Tier for new users to getting help started with some of their services. The best things in life are for free, I used to speak, so I decided to try it. I am working on smaller Java web application, which is based on Spring 3 and I need some (free) service for hosting my application on a public web. My app needs relational database for persisting data, and access to filesystem for file storage, where file size counts up to hundreds of megabytes. In the beginning I was curious, if 613 MB of memory will be enough for my web app. I found that it was sufficient, and also performs very well.
This article guides you to setup of your own infrastucture. I am still using OS Windows, but few things you need to do, e.g. SSH connection to your EC2 image are simplier to do in Linux. So let's go!

Sign in to Amazon WS and launch image

First of all, you need to create your personal account on Amazon WS, if you don't have any. I signed in to S3 and EC2 services to achieve my application's requirements. After successful registration, I followed a great AWS Management Console, where you can manage your EC2 instances, S3 bucks and other Amazon services. I launched Amazon Linux 32-bit image on Micro Instance. Note, that there are also many preconfigured Community images with complete Java, Tomcat and MySQL stack. Images based on OS with its own GUI (e.g. Ubuntu Desktop, or Windows) allows you to connect thru remote desktop, which is also interesting possibility. I decided to use simpliest image to do the things my way. Amazon Linux AMI comes with preinstalled Open JDK 6. Also note, you have to select ESB based image, if you want to use free tier. I used Putty for SSH connection to my image. Documentation is available in Geeting Started Chapter of EC2 Documentation.

Install Java

Amazon Linux AMI comes with preinstalled Open JDK 6. I changed JAVA_HOME variable for Tomcat, which now points to JDK instead of preconfigured JRE.

Install Tomcat

Amazon Linux AMI is similar to CentOS, so I used this article for installing Tomcat 6 (or other version).
If you want connect to your Tomcat application from public web, don't forget to allow port 8080 (or other depends on your configuration) in Security Group Section of AWS Management console.
I also installed Tomcat native library with following command:

sudo yum install tomcat-native.i686

Install MySQL

Well, this was the funniest part of setup :) I followed these four simple commands for whole MySQL setup.

Conclusion

As you can see, setup your own image with Java stack on Amazon EC2 is not complicated. Next step could be create a script for automatic deploy of WAR application on demand. First, you would upload your application to some server with absolute and reachable URI. The other option is availability of your application on web as a result of automated build. In this case, script could contains commands for download a war archive from public web thru curl tool and copying it to a tomcat webapps folder. If you are using Eclipse IDE, there is AWS Toolkit available, which also looks promising.

4 comments:

  1. Good article, but somewhat incomplete. How do you start the tomcat?

    ReplyDelete
  2. @Sridhar: if you downloaded and extracted tomcat directory like me, just run 'startup.sh'.

    ReplyDelete
  3. I like the article too, but am not clear on the part about changing JAVA_HOME for Tomcat. Can you provide more details on that? What value did you change it to and what is the syntax of the command to do this?

    Thanks!

    ReplyDelete
  4. Thank you! We'll do this way)

    ReplyDelete