Set Up an Amazon EC2 Instance with Tomcat and MySQL – 5 Minute Tutorial

Create an AWS Account

First things first: you need to create your AWS account. You can sign up here. You’ll have to provide a credit card and a phone number where you will be called as part of the online registration process for verification purposes. Amazon offers a Free Usage Tier, which is great to explore the services and even host real apps without being charged. Check the details here.

Create an Instance

Now that you have an AWS account, access the AWS Management Console and click the EC2 tab to create a new instance:

  • Choose an AMI in the classic instance wizard: I chose the Basic 64-bit Amazon Linux AMI.
  • Instance details: keep the default settings.
  • Create a new key pair. Enter a name for your key pair (i.e. christophe) and download your key pair (i.e. christophe.pem).
  • Select the quick start security group.
  • Launch your instance.

SSH

Once your instance is running, you can ssh into it. First, you need to identify the address of your instance: Select the instance in the AWS Management Console, and look for the Public DNS in the instance description (bottom part of the screen).

Use that address (and a path to your .pem file) to ssh into your instance:
ssh ec2-user@ec2-50-17-14-16.compute-1.amazonaws.com -i ~/christophe.pem

Important Notes:

  1. You may have to chmod your .pem file as follows:
    chmod 600 ~/christophe.pem
  2. Depending on the image you chose, you may also have to replace ec2-user with root or ubuntu. ec2-user is what you need for an Amazon Linux AMI.

SFTP

At this point, you can also SFTP into your new instance using parameters similar to these:

host: ec2-50-17-14-16.compute-1.amazonaws.com
port: 22
user: ec2-user
password: Select your .pem file

Elastic IP

The public DNS address changes when you restart your instance. To get a permanent IP address, click Elastic IPs in the AWS Management Console (left navigation bar), allocate a new IP address and associate it with your instance.

Install Tomcat

To install tomcat, ssh into your instance and type the following command:
sudo yum install tomcat6 tomcat6-webapps

“tomcat6-webapps” is optional and will install the Tomcat sample apps. The configuration files are in /usr/share/tomcat6. To install your own web app, you can simply SFTP it to /usr/share/tomcat6/webapps.

To start Tomcat:
sudo service tomcat6 start

To stop Tomcat:
sudo service tomcat6 stop

The default Tomcat server uses port 8080. You need to open that port on your instance to make sure your Tomcat server is available on the Web (you could also change the default port). In the AWS Management Console, select Security Groups (left navigation bar), select the quick-start group, the Inbound tab and add port 8080. Make sure you click “Add Rule” and then “Apply Rule Changes”.

You should now be able to access your Tomcat server from a browser using (use your own Public DNS address or Elastic IP):
http://ec2-50-17-14-16.compute-1.amazonaws.com:8080/

MySQL

To install MySQL, ssh into your instance and type the following command:
sudo yum install mysql-server

To start MySQL:
sudo service mysqld start

To stop MySQL:
sudo service mysqld stop

MySQL Workbench

You can administer your database remotely using MySQL Workbench. You don’t have to open an additional port (i.e. 3306). Simply choose Standard TCP/IP over SSH in the Setup New Connection dialog as follows: