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:

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

  1. Lola LB November 16, 2011 at 5:28 pm #

    What is the AMI ID of the instance that you selected?

  2. Christophe Coenraets November 16, 2011 at 6:08 pm #

    ami-1b814f72

  3. Dinesh Kumar November 21, 2011 at 9:41 am #

    Hi Christopher,
    Just tried your employee details on flex mobile app. Just having some doubts in Manager and Direct Report parts. Can you please send me the clear code of that part??

  4. hem chandra November 29, 2011 at 11:38 pm #

    Thanks for sharing such kind of wonderful information about java. Your blog is very informative. Please keep such beautiful postings.

  5. Alan Greenblatt December 1, 2011 at 3:34 pm #

    As an FYI, I use FileZilla for sftp from my Windows 7 box. FileZilla uses a ppk file, not a pem file.
    You can generate a ppk file from pem file from puttygen, which can be accessed here:

    http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

    Run puttgen, load your pem file and then save the private key, which is a ppk file.
    Within FileZilla, go to Edit->Settings…Connection->SFTP and add your private key/ppk file.

    Now you can connect to your EC2 instance using SFTP, by just specifying the public dns or elastic IP address and the username ec2-user or root (depending on the AMI).

    • KUmar January 25, 2012 at 11:55 pm #

      Hi,
      I’m tring to use filezilla to transfer my app to tomcat6/webapps folder on the ec2 linux instance.
      How did you get across the permission issue ” open for write: permission denied”
      we login as ec2-user. in a putty session I can sudo. how do we do it using filezilla gui?
      Thanks in advance.

  6. carolo December 2, 2011 at 12:46 pm #

    Wonderfull

  7. TopHostingProviders December 5, 2011 at 8:56 am #

    All these ideas will really help me in the future to have a successful blog MORE!!

  8. Shu December 18, 2011 at 4:24 am #

    Thanks a lot man!

  9. Kumar January 20, 2012 at 1:57 am #

    Excellent help. My first attempt at doing anything with EC2 and it went through like a breeze.
    Only step I got stuck. I’m using tomcat; mysql and I’m on windows 7 trying to open SSH using putty to the linux.
    Downloaded putty; puttygen;
    I had the *.pem file. which putty’s SSH>Auth did not accept.
    Used puttygen to convert *.pem to *.ppk. I forget the blog entry that helped me. Else I would have quoted it here.
    Everything else, including user id ec2-user…..was spot-on.
    Thanks for sharing my man.
    Kumar

  10. Haribalan Raghupathy January 27, 2012 at 12:28 am #

    so if i want to scale this setup … like say i want to have multiple tomcat or mysql..things like that..
    how can we do it. I guess amazon cannot auto scale this setup on their own..
    So are we left with creating another EC2 (or more ram on current one like more to micro to huge-ec2) and then
    configure all our load balacing etc???

    Thanks
    Hari

  11. Zaid Ansari February 5, 2012 at 9:56 pm #

    Very well done article. It was a breeze.

  12. Abdus April 2, 2012 at 11:32 pm #

    I got a question. Under the Install tomcat section above you said open up the 8080 port that will allow to access http://ec2-50-17-14-16.compute-1.amazonaws.com:8080/. But lets say i use godaddy to setup domain, and then i want my website URL to point to this IP and port, on the godaddy DNS setup i am only able to specify IP. How do i make the URL go to this IP and then onto this specific port 8080?

    • Saket July 6, 2012 at 8:12 am #

      Any luck on this? I’m also stuck with the same.

    • Netlander December 2, 2012 at 4:34 pm #

      Guys, you need to follow your URL address with :8080, for example http://www.mygodaddydomainname.com:8080. No need to change the DNS name.
      HTH

  13. WoolyNinja May 31, 2012 at 4:24 am #

    This was a fantastic tutorial. Clean, to the point, and easy to understand. Thanks!

  14. Girum Ibssa June 7, 2012 at 11:23 pm #

    EXCELLENT tutorial. This makes having a Java server on the internet amazingly easy.

  15. Nandish July 5, 2012 at 11:35 am #

    Thank you very much… I was trying to connect by using ip address. Also i made many changes to config files so i didnt know where the prb is… Thank you for your information….

  16. Gary July 9, 2012 at 8:38 pm #

    Great tutorial, thanks for taking the time to put up screenshots.

  17. slametz November 28, 2012 at 3:25 am #

    awesome, thanks

  18. Al Klein December 10, 2012 at 2:21 am #

    Thanks! Good explanation, to the point.

  19. Praveen January 31, 2013 at 2:58 pm #

    HI,

    I had an ec2 instance running, I am using wamp server and running my php website.

    Now I am implementing the searching functionality for that I want to install tomcat server..

    Now my question is can I install tomcat server on the same instance with wamp server??

    Thanks in Advance..
    Praveen

  20. Интернет магазин March 18, 2013 at 9:31 pm #

    Excellent points altogether, you simply received a new reader. What would you recommend about your submit that you just made some days in the past? Any certain?

  21. Satish April 13, 2013 at 10:01 am #

    Great post. Thanks for detailed information.

  22. Ovidiu May 5, 2013 at 7:33 am #

    Excellent tutorial ! Thank you !

  23. Ashutosh June 11, 2013 at 11:02 pm #

    simple, useful and informative, Thank You!!!

  24. Solens June 28, 2013 at 1:50 pm #

    Great tutorial! It works smoothly for port 8080, however, I need to setup several instances of tomcat from 8080 to 8088, how can I open the remaining ports on EC2?

    • reshmi December 15, 2013 at 12:19 pm #

      hi i have install tomcat but when
      i give the url in browser its not working even the port is changes and
      apply rules has been done. Please respond

  25. nginx tomcat July 15, 2013 at 9:25 am #

    Thanks for the tutorial. Easy to follow on using EC2

  26. adobe Photoshop cs6 book August 5, 2013 at 10:16 am #

    Folks can also check out Inkscape (inkscape.org) which can be a extremely great program for
    vector graphics similar to AI. It is going to import AI files and
    can create SVG files which are compatible with AI.

  27. Shwetha August 6, 2013 at 6:14 pm #

    Very informative article.. Setup my Amazon instance in quite literally 5 min.. Thanks Christophe

  28. sreejith September 17, 2013 at 1:06 am #

    It shows internal server error while accessing the JSP after installing Tomcat. Is that related with Java Hosting apps permissions or tomcat instance?

  29. Anurag October 10, 2013 at 3:05 am #

    Thank you! :)

  30. Often, you will give your body approximately 500
    more calories per gram, while fat contains 9 calories per gram
    than either carbohydrates or protein. Stay away from weight gain powders.

    If helps the internal systems and promote the healthy functioning
    of different parts of the body in balance.
    Combined with weight training exercise will put the
    muscles under stress and force them to grow. This is one
    of the three mass gainer major energy sources.

  31. Suresh November 7, 2013 at 12:37 am #

    Very nice article. Thank you so much.

  32. zolibra November 14, 2013 at 4:36 am #

    the free tier only have 600M memory , which has no chance to load a big WAR..that’s the problem

  33. prakash November 20, 2013 at 1:31 am #

    Such an awesome tutorial. You almost saved my day. Thank you

  34. Ramanan January 11, 2014 at 5:44 pm #

    Clean and well written article. Good work Chris !!!

  35. Jesho February 7, 2014 at 10:07 am #

    Awesome article. Had a prob with the putty username and finally realized it as ‘ec2-user’. Other than that everything specified in the article was awesome

  36. David April 9, 2014 at 9:57 am #

    Thanks a lot! was really useful ! :)

  37. Cindy Corritore April 13, 2014 at 12:31 pm #

    Great great information. I have one issue – I am trying to connect to my LAMP instance on AWS using mySQL Workbench. Your screenshot (last image in your post) was perfect, except that mine won’t connect – it says “Could not connect the SSH tunnel Authentication Error’. I am using ec2-user for the user name, no pw, and my .pem file (created on AWS). Thoughts?

  38. avast gratuit May 25, 2014 at 1:21 am #

    Hey would you mind stating which blog platform you’re using?
    I’m planning to start my own blog in the near future but I’m having a difficult time selecting between BlogEngine/Wordpress/B2evolution and
    Drupal. The reason I ask is because your design and style seems
    different then most blogs and I’m looking for something unique.
    P.S My apologies for being off-topic but I had to ask!

  39. hypnosis anxiety June 14, 2014 at 12:27 am #

    Many people have quickly been able to dramatically improve their lives through the power of hypnosis.
    These ways on inducing hypnosis are taught by Ledochowski through his very
    own covert hypnosis course. any person who has ever dieted and failed knows the biggest obstacles to weight
    loss can be in the mind.

  40. cheap Nets 2014 jerseys June 15, 2014 at 12:01 pm #

    cheap china jerseys free shipping/cheap college jerseys from china

  41. cheap Wild jerseys from china June 16, 2014 at 6:24 am #

    Customized Senators new jerseys 2014

  42. network marketing tips July 14, 2014 at 5:00 am #

    I visit everyday a few sites and sites to read content, however this website presents quality based articles.

  43. pavankumar July 14, 2014 at 6:56 am #

    Fant Blog man!! Helped a lot..Keep writing..:)

  44. Daniel Nwankwo November 23, 2014 at 7:41 pm #

    Thank you for the blog, it will help me and I have been looking for something like this. One question please, if I stop the instance what happens to the data in the database, is it lost, is it only held in memory? Same question for the tomcat webapp files.
    Thank you

Trackbacks/Pingbacks

  1. Links for November 14th through November 15th — Vinny Carpenter's blog - November 15, 2011

    [...] Set Up an Amazon EC2 Instance with Tomcat and MySQL – 5 Minutes Tutorial – Set Up an Amazon EC2 Instance with Tomcat and MySQL – 5 Minutes Tutorial [...]

  2. – Amazon Kindle Books – 6 Settings Amazon Sites – (3/02/2012) | Amazon Fire Tablet - February 3, 2012

    [...] Set Up an Amazon EC2 Instance with Tomcat and MySQL 5 .Nov 15, 2011 Create an AWS Account. First things first: you need to create your AWS account. You can sign up here. [...]

  3. Installing mySQL on AWS EC2 Ubuntu Machine through SSH « Application & Program Tips « Electronic Services - May 10, 2012

    [...] Also, a really good, AWS EC2 direct tutorial for setting up mySQL on the server can be found at Christophe Coenraets site. [...]

  4. Creating an Amazon EC2 Instance « SoftwareSanders - August 21, 2012

    [...] http://coenraets.org/blog/2011/11/set-up-an-amazon-ec2-instance-with-tomcat-and-mysql-5-minutes-tuto… Share this:TwitterFacebookLike this:LikeBe the first to like this. [...]

  5. Set Up an Amazon EC2 Instance with Tomcat and MySQL – 5 Minute Tutorial | TechnoVeille - September 3, 2012

    [...] original : ici ‹ Tutorial: HTML Templates with Mustache.js Tagués avec : Amazon EC2, [...]

  6. Tomcat etc | Firelife - February 25, 2013

    [...] http://coenraets.org/blog/2011/11/set-up-an-amazon-ec2-instance-with-tomcat-and-mysql-5-minutes-tuto… [...]

  7. Erro Datasource Tomcat na Amazon EC2 (Linux) | Aristides Neto Blog - June 6, 2013

    [...] Existem vários tutoriais que ajudam nesse processo de configuração, como esse aqui. [...]

  8. Grails on AWS | Greg Stephens Blog - July 23, 2013

    [...] addition to Bobby Warner’s post I found the post by Christophe Coenraets on EC2 with Tomcat & MySQL and Liferay on Amazon [...]

  9. Set Up an Amazon EC2 Instance with Tomcat and MySQL – 5 Minute Tutorial | indybee83 - December 30, 2013

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

Leave a Reply

css.php