Setting up a Free Amazon EC2 + Tomcat + MySQL Installation

Today happens to be a day when I’ve got that feeling – the one where I feel something worthwhile was done. The feeling that I *learnt* something. And before I forget all about it, I’ll try and jot down the long winding path I followed before getting this particular thing done.

So here’s what I had set out to achieve:
Set up a globally accessible server for hosting a Tomcat and MySQL, so that I do not have to launch my machine to hook up my Android device, while testing the web-service calls.

Before I continue, let me say first-up, that I did NOT consider any other solution – like Google App Engine, etc, for this purpose. This was primarily due to knowing before-hand that Amazon’s AWS allowed me to do this. Other hosting solutions might also provide the same, but I was going to give AWS my first shot, and that’s what this post is all about.
Also, the free Amazon EC2 micro-server is free only for a duration of 12 months.

Getting hold of the EC2

For anyone who’s curious to know what Amazon’s free EC2 offering has in store, you can check out the details here. Before you can do anything about it, you’ll need to have a valid Amazon account, which you can create by clicking on the “Sign Up Now” button. I’m not going to get into the details of creating an account, since it’s a fairly simple procedure. I particularly liked the part where they’ll validate your phone #. Another thing to keep in mind is that you’ll need a valid credit card to create an account. Once you’ve created your account, Amazon will charge your card $1 to verify its validity. They’ll refund it later.

Now that you’re done with creating your account, you’ll be hit by a barrage of pages which keep on telling you of the many awesome things you can do. I was so very lost in this deluge of information. I finally figured out that the main control hub  lies in the pages that get shown to you once you’ve clicked on the “Sign into the AWS Management Console” at the top. Once you’ve done so, you should see a screen that should resemble something like this:

The Instance Management Dashboard

It is here that the source of all power lies. Next up, click on the tab “EC2”. This should launch your EC2 dashboard. Click on the “Launch Instance” button, which should bring up a dailog resembling:

Choose an instance

As you’re only interested in the “Free Tier” services [note: selecting others will attract a charge on your credit card], go ahead and select the 32/64 bit AMI as per your needs. Clicking on the “Select” button will take you to the next screen for specifying and editing the “Instance Details”. The default options serve just fine, unless of course, you’re looking for something different. Clicking on “Continue” will load some advanced instance details. Again the default options suit you just as well. Amazon is kind enough to indicate which options attract charges, like Enabling Instance Monitoring. Click on continue, and you’ll be asked to add a key-value based tag. This comes in handy when you’ve got a whole lot of instances running. So if you’re not doing so, might as well ignore it, and click on continue.
The next screen asks you to create a RSA key pair [who wants to juggle with passwords anymore?].

Creating an RSA Key Pair

Give a name of choice for the key-pair file, and click on the “Create & Download your Key Pair” link. This creates a [file-name].pem file, which you should be careful to save at a location, which you won’t forget, and, is secure. In general, it’s a good practice to save RSA keys in your “user home”/.ssh folder.

In the next screen, you’ll be asked to create a security group. It’s advisable to create one, so go ahead and do so. Click on continue, and finally, you’ll see a screen which mentions all the details associated with your instance:

Summary details of your instance.

Clicking on launch will do the obvious. And lo behold! your instance is operational. Not that you can do much about it. You now need to install your Tomcat and MySQL servers.

Getting inside

Now that you’ve got your instance running, you can get started doing something productive only after you manage to log-in to the machine. And, SSH is your trusty friend.
In the left panel of the management console, you’ll need to click on the “Instances” link. This should load up your instance in the right panel:

Listing of your running instance.

You need to now right-click on the row listing of your instance. This will bring up the context menu, in which you’ll need to select the “Connect” option.

Context menu for your instance.

Doing so will bring up a dialog which describes the ways in which you can connect to the instance:

How to SSH into your instance.

For non-Windows and *nix/posix based systems, you can simply launch your Terminal to run the ssh command. Windows users can also do so if they have Cygwin or MSYS installed.
For Windows users, as mentioned, PuTTy  serves as aworthy alternative. However, it’s not just as simple as to download it and run it. You need to first format the *.pem file into a *.ppk file which puTTy understands. An excellent description of the steps involved can be found here. Once you’ve successfully logged in with puTTy, you should see something like this:

The PuTTy terminal in Windows.

 Installing Tomcat

By default your Amazon instance should have a JDK installed. To verify the same, type

java -version

Either Sun’s JDK or OpenJDK should be installed. In case there is no JDK installed, then you can execute the following command:

sudo yum install java

sudo” [super-user do] allows you to execute the command in the capacity of an admin/super-user.
Now to install Tomcat, you can either type the following command:

sudo yum install tomcat6

thereafer, you can launch tomcat by running the following command:

sudo service tomcat6 start

or, simply get the Tomcat .tar.gz distribution from the downloads page using wget:


Unzip the distribution folder from the .tar.gz file using

tar -xzvf apache-tomcat-6.0.32.tar.gz

Once extracted, you can go into the bin folder and start Tomcat by typing


Of the 2 methods described above to download and run Tomcat, I prefer the second because I’m more used to working with the core distribution, and I can easily find my way around to configure the launch of the “Manager” application on Tomcat.

At this point of time, you might feel that entering the public DNS of your instance with the :8080 suffix should launch the all too familiar Tomcat page. But it won’t do so, since, you need to configure your instance to open up that particular port.

To do so, you need to go back to your AWS Management Console, and now, from the left panel, under the “Network & Security” section, click on the “Security Groups” link.
Once your instance’s security groups get listed in the right panel, select the one you created.

In panel at the bottom, you’ll see the “Details” of the selected security group displayed. Click on the “Inbound” tab next to Details. You’ll need to create a “Custom TCP rule” with the port range 8080, and leave the Source field unchanged with the value Click on “Add Rule” to apply this rule. You’ll see that this new rule gets listed on the right side, below the SSH rule.

Setting a port rule for Tomcat.

Now try accessing your instance’s public DNS with 8080 suffixed. Voila! Tomcat’s alive!

Sorting out MySQL

This one’s quite straightforward. First up, you need to install MySQL:

sudo yum install mysql

should take care of that.
However, running “mysql” at this point will only throw up an error:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’

So, you need to start the mysqld process this before you’re ready to use MySQL:

/etc/init.d/mysqld start

And with that, you’re good to go with a free Amazon EC2 micro-server + Tomcat + MySQL installation. From a development perspective, this can be an *awesome* thing to have!

Btw, if you want to access your MySQL installation remotely, say from your own local machine, then you’ll need to add another port rule for MySQL as you did for your Tomcat.

Hope this helps someone out there, because it certainly would’ve helped me a lot had I got this information at one place before starting off. But then, therein lies the excitement of discovering and learning something!


2 thoughts on “Setting up a Free Amazon EC2 + Tomcat + MySQL Installation

  1. Hi,
    I am new in java hosting. I normally use windows platform for development. I developed my site using tomcat + mysql in windows. How can I install tomcat + mysql in server from my local pc? Shall the above commands work in windows command prompt.


    • You’ll need to install tomcat and mysql separately on the EC2 instance to get them working. Typically, the EC2 instance won’t come with those installed. You should also verify whether a proper JDK is installed using java -version.
      The commands to install mysql and tomcat are as follows —
      $ sudo apt-get install tomcat6-server
      $ sudo apt-get install mysql-server mysql-client

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s