This article explains how to deploy your Java application with the Amazon EC2 service. Because Amazon gives 1 year free tier, you can freely deploy your application for one year. In this tutorial I would like to show you how to deploy Java application (.war) with tomcat server.
First of all you must create an AWS account. After you create the account you will be able to try free tier for one year. (Please read the free tier conditions)
Then login to your account and go to Services -> EC2. Then go to the INSTANCES -> Instances In the left menu bar.
Then go to launch Instance and choose “Ubuntu Server 16.04 LTS (HVM), SSD Volume Type — ami-80861296”. I am using this AMI because I am familiar with Ubuntu.
Use the free tier t2.micro machine for testing. Otherwise, you will be charged.
You should create a new key pair and Download the Key Pair before the next step. I have created a key pair named “aTest” and when I download the Key Pair, It will download a “aTest.pem” file. This file is really important because without it, we will not be able to SSH to the EC2 machine.
Launch the instance and if all the things are correctly done, you will see a new instance have been created.
Wait until Status Checks finish its initialization. After some time status Checks will look like this.
Now your EC2 Instance Lunching part is done. Let’s go to the next steps.
Now you have to realize that you have a virtual instance and you can do whatever you want with it. First before you do anything with the instance you have login to the machine. For that you can use SSH. When you select your instance and click the connect button above the table you will see ab window like this.
Here you have to follow their guideline and SSH to the your instance. If you are using Linux then you can easily use SSH from the terminal. But if you are using windows then you have to use tools like “PuTTY”.
Step 4 — Installing Java
If the all the steps are successfully done, you should be able to login to the your ec2 instance right now.
Now we have to install Java for the EC2 instance. There are several ways to install Java for Ubuntu. But I am using the method described in the following link.
There are some changes you have to do, please read the instructions below the link, before you follow the link instructions.
How to Install Oracle Java on Ubuntu Linux
This tutorial will cover the installation of 32-bit and 64-bit Oracle Java 7 (currently version number 1.7.0_45) JDK…
This link provides how to install the Java on a local machine. Because we are installing it on EC2 instance, after the 4th step of that link ( 4 Download the Oracle Java JDK/JRE for Linux) we have to go through an additional step.
After Downloading the Oracle JDK you have to SCP it to the EC2 machine. Use following command to copy data from your local machine to ec2 instance.
scp -i aTest.pem jdk_tar.gz firstname.lastname@example.org :/home/ubuntu
cp /home/ubuntu/jdk_tar.gz ~/Downloads/
Then follow the step 5 to 12 at the link.
Now you should have installed Java to the machine successfully. Now we have to install tomcat to the machine. Find the latest stable tomcat version and download it’s tar.gz file. Same as above step, transfer the file using the SCP command.
scp -i aTest.pem apache-tomcat-9.0.0.M10.tar.gz email@example.com :/home/ubuntu
Now extract this file to /opt/. To extract this file use following command.
so now you have successfully completed the step 5.
Now what you have to do is transfer your Java Application (.war) to the ec2 machine. To do that, use the SCP command again. The command will be like this.
scp -i aTest.pem testAPP-1.0-SNAPSHOT.war ubuntu@ec2–compute-1.amazonaws.com:/home/ubuntu/ testAPP-1.0-SNAPSHOT.war
Now you have to copy your .war file to webapp folder. Use following command with suitable changes.
cp /home/ubuntu/testAPP-1.0-SNAPSHOT.war /opt/apache-tomcat-9.0.0.M10/webapps/
Now step 6 is completed.
So now you only have to do is, starting the tomcat server. To do that, go to bin folder of the tomcat installation.
To start the server
To stop the server
When you start the server, you will be able access your web application through your ec2 machine public DNS.