Skip to main content

Amazon EC2 Server Setup & Installing JDK 8 and Tomcat 8, Running on Port 80 & 443 and Redirect Request from port 80 to 8080 and 443 to 8443

Amazon EC2 Server Setup & Installing JDK 8 and Tomcat 8, Running on Port 80 & 443 and Redirect Request from port 80 to 8080 and 443 to 8443.

Step 1 : Log in to your aws account by following this link

then click on my account and choose option aws management console.

Note: I am assuming you created your account with aws and you are ready with you account if you haven’t done then you can check out on google you will get many and it's a straight forward steps if you have still problem while creating an account you can comment in comment box i will also provide tutorial for that.

Once you logged in aws management console you are able to see window like this one

Note : Before we go ahead we have to select proper reason from right and side.I choose ohio region for this example.

Step 2 : Now you have to choose EC2 Server from Services tab on left side
top corner then choose EC2 Services from “Compute option

You will get window like this one and right now i have one instance is
running for our website but you will have zero instance if you haven't setup
any instance then you will have all the values will be 0 except security group.

Now you have to Launch Instance for that you have to click on Launch Instance
Button You will have  an Amazon Machine Image (AMI)  page.

Now if you are in free tier then click on Free tier only AMI on left and side
Once you done ! Now you can select AMI that are available under free tier.

Here i will choose Amazon Linux AMI 2018.03.0 (HVM), SSD Volume
Type - ami-922914f7

Why i am taking this AMI For following reason some of because in this AMI
i will get some pre-installed packages like different different jdk version
as well as tomcat version inside this image.

Note : You can also choose Ubuntu or any other Linux version but
i am choose amazon Linux

Then click on select option Now you will have choose an instance
type (type of process and memory instance storage and all)
I am under free tier so i will go for default option that is t2.micro instance.
Once we select then we have to go for Next : Configuration Instance Details

Step 3: Configure Instance Details
Don’t change any default option if you don’t have any prior knowledge of
aws services. Otherwise it may cost unexpected charges. And click on add
storage  again don’t change any default option keep it as it is give by default.

Step 4: Add Storage
Your instance will be launched with the following storage device settings.
You can attach additional EBS Volumes and instance store volumes to your
instance, or edit the settings of the root volumen. You can also attach
additional EBS volumes after launching an instance, but not instance store
Next Add Tags.

To help you manage your instances, images, and other Amazon EC2 resources,
you can optionally assign your own metadata to each resource in the form of tags.
This topic describes tags and shows you how to create them.

Step 6 is : Configure Security Group

A security group is a set of firewall rules that control the traffic for your instance.
On this page, you can add rules to allow specific traffic to reach your instance.
For example, if you want to set up a web server and allow Internet traffic to
reach your instance, add rules that allow unrestricted access to the
HTTP and HTTPS ports. You can create a new security group or select
from an existing one below.

Note: This is very important step toward to connect your instance and to access your instance if you do small mistake then it would lead you disappoint. While defining your security rules keep in mind you have to also take care of your external thread suppose you defined some rules that would lead to damage/hack by other person. So define each rules very carefully.

Here i am defining some security rules like
1) To access EC2 instance through SSH(like putty ) we have to access it on port 22
2) To access EC2 instance through HTTP request we have to give an access it on port 80
3) To access EC2 instance through HTTPs(SSL) request we have to give an access it on port 443
4) To access your EC2 Instance through SMTP (Like filezilla) we have to allow it on port 25
5) To access your EC2 instance through tomcat that will be listing on port by default on 8080 so we have to also give an access to it.(Note later will redirect the request from 8080 to port 80 and 443 port request to 8443)

Note: Add all these ports and make their source custom to public (But it is not at all suggest to give all these port as public access but for sake of simplity i am allowing it. You can change their source later after lunch instance successfully as well as.)

After configuration security group you can Review and Launch an instance from bottom right corner.

Step 7 : Review Instance Launch page

This will show detailed information that we configured in last 6 steps after view your all the configuration you can click on lunch instance .
When you click on launch instance then you will get an pop window from where you have to create your key pair and give a valid name and then click on Download Key Pair or you can use existing key pair if you have already download. If you missed it you will lose your services access. So keep it at safe place. Because these key pair are used while we connect our server from ssh client like putty.

After downloading click on lunch instance

Final Step : After this you will have one running instance in you account and it will look like something

Connect AWS EC2 instance to ssh client

Before we go ahead we need to download software
1) Putty   
2) Puttygen Tool  
Once you download it then first you have to run Puttygen Tool

Now you have to import your private key as shown above by click on Conversions -> Import Now you have to select your .ppm file that you download while lunching an instance. Once you import it. Then click save your private key and save it with the same name as your .ppm file name that you created it. It will save your .ppm file to .ppk file.

Step 2 : Now you have to install putty and run it. Now at Hostname or IP address you have to define as public dns address from server page its look like something “”

You have to also need to specify your ppk file but before to specify it you have to go to connection → SSH → Auth  here at bottom you will get and browse option where you have to specify you puttegen ppk file then click on open.
It will propmt a cmd window below one you have to enter “ec2-user”
Here is sample image.

Now we are connected with our EC2 instance as ec2-user  through ssh.

First Update Amazon Linux AMI then we have to install JDK1.8 in EC2 instance .

Once you  connected to your EC2 server it will suggest the following command
Like to update your instance by follwoing this command

sudo yum update

To update all the packages.
But as i mentioned above your are looked in as ec2-user but to do some further operation i will logged in as root user for that you don’t need to do anything except this 2 word command like
sudo -s
Once you logged in as root user then we can any type of changes inside our EC2 instance.
Now i can use yum package manager to install other software like apache tomcat.
But first we have to check latest version of java that is present by default in this AMI for that i have to run this command like
java -version

Here its will sample output

It will display our current java version like depending on time you are seeing it. Right now while writing this tutorial its showing me jdk1.7 But i have been coding in java 8 so i have to install that For that i will run following command
For that i have to run the command
sudo yum list java*

It will list down show all the java package it has that present in yum package manager. So here we have java 6 available and we have java 7 and java 8 available so what we can do Then we have to copy the first part of jdk like java-1.8.0-openjdk

And then we have to run the following command like
sudo yum install java-1.8.0-openjdk

Once it runs. Our java installation is complete. So now can i run java 8 version ?
No !  You haven't set it as default java 8 so for that i have to run the following commands
Now i have 2 java version java 7 and java 8
So we have to use alternatives command like
sudo /usr/sbin/alternatives --config java
It will say 2 program is providing java one is java 7 which is selected by default and java 8 for that i have to enter 2 from keyword. If you want to keep java 7 version then you have to just enter key nothing else but if you want to choose java 8 then you have to enter 2 i have to enter it
One more command to alternatives is
sudo /usr/sbin/alternatives --config javac  
This command will make java compiler as java 8 as default compiler
Phase 4 : To install Apache Tomcat on AWS AMAZON EC2 instance
Now we have to install apache tomcat on linux ec2 server
We will use yum package manager to list down all the available package for tomcat
You have to run this command
yum list tomcat*

Now to install tomcat8 we have to run the following command

sudo yum install tomcat8

Now to manage tomcat web application i have also need to install tomcat admin webapps  manager panel or web interface to deploy our web application and undeploy our web application
For that i have to run the following command
sudo yum install tomcat8-admin-webapps

After installing we can start tomcat for that i will use
sudo services tomcat8 start
sudo services tomcat8 stop
sudo services tomcat8 restart

Because we have to use sudo for every time because we are running of behalf of root user

Once you execute start you tomcat then go to web browser and type your DNS:8080/manager/html

For example :

It will ask for login but we haven’t stepu user for tomcat so when we click on cancle then it will be displayed like this one

Now we have to enable to user to sign to the user tomcat8 web apps interface for that i will have to change configuration first we have to know where is our tomcat configuration files are

For that i will use  command like
whereis tomcat8
So tomcat8 are available at /usr/share/tomcat8
So i have to goto this directory by typing command like
cd /usr/share/tomcat8
Now to list down all the folder and files inside this directory we have to use “ls” command. Now i have to goto “conf” folder to change configuration for tomcat web apps
“cd conf”
then “ls”
It will show all the present directory
In this directory we are looking for “tomcat-users.xml” file we have to change our configuration
We have to add to user role that is shown in 403 error page.
To edit this file i will use text editor that is “vi” which is default in this server, its not very convenient but we can edit easy.
sudo vi tomcat-users.xml
After opening this file you can go up and down on up and down key
Go to bottom the page before the </tomcat-users>
Now to enable this file for editing you have to use “o character “ from keyword
To save this file we have to enter esc button from keyword and “:wq “
To cross check our changes are made successfully or not we can re open this file by again this command and easily we can cross verify
sudo vi tomcat-users.xml
After making changes we have to take server restart for that i will use
sudo services tomcat8 restart
Now try to access your dns url with the following pattern you will still get an error then we have to change again
context.xml file that is present inside /usr/share/tomcat8 ----->  webapps → manager → META-INF → context.xml file
We have to comment the value tag that is present inside the Context tag like below code
<Context antiResourceLocking="false" privileged="true" >
<!--  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
       allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>

  • Setting tomcat to run in ports 80 & 443:
    There are many ways to achieve this. Some of them need you to install an apache server, and handle the different XML configuration files, but I think that this is a quite easy way, and you don’t need to install anything else, you just need to learn a little bit on how to use iptables so you can redirect the requests from port 80 to 8080 and from port 443 to 8443.
    To do so, you can type in the following commands in your console:
# Checking the current iptables content on the prerouting table
$ sudo /sbin/iptables -L -n -t nat
# Adding the rules:
$ sudo /sbin/iptables -t nat -I PREROUTING -p tcp —-dport 80 -j REDIRECT --to-port 8080
$ sudo /sbin/iptables -t nat -I PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443
# Checking that the rules were added succesfully
$ sudo /sbin/iptables -L -n -t nat
# Saving the rules
$ sudo /sbin/service iptables save
# Restarting ipTables:
$ sudo /etc/init.d/iptables restart

# Checking that the rules were saved succesfully
$ sudo /sbin/iptables -L -n -t nat

If you want to deploy application through filezilla then you have to change the following permission to the following file
The ec2-user don't have permission to write files in /var/lib/tomcat6/webapps/. Only root user can do so. You have two ways here to do so:
1) Copy files to /home/ec2-user/ using filezilla. Now SSH into linux machine through putty. Change to root user using command sudo -s. Then copy file from /home/ec2-user to /var/lib/tomcat6/webapps/ using command cp -i RealEstateERP.war  /var/lib/tomcat6/webapps/.
2) SSH into linux machine through putty.Change to root user using command sudo -s.Provide write permission to all users on /var/lib/tomcat6/webapps/ using command chmod 777 /var/lib/tomcat6/webapps/. Then copy files to directory directly from filezilla.

How to take backup of your project in aws EC2 Instance 

Like first you have to take your project backup so for that you can run different different command based on recuriements if you run directoly like 
sudo cp -i source_direcotry destination_direcotry or sudo cp source_direcotry destination_directory then it will raise an error like  :  cp : omitting direcotry '/usr/share/tomcat8/webapps/ROOT
so your ROOT folder consiste of sub folder then this above command doesn't work you have to run the following command sudo cp -r /usr/share/tomcat8/webapps/ROOT /usr/share/othogobackup/

How to Deploy & Take Backup of Java Web Application At AWS EC2 Instance Through ssh.

Steps 1: 

Step 2: 

sudo chmod 777 /var/lib/tomcat8/webapps

If you want to enter in root user then use the following command
Sudo -s

If you want to exit from root user to back to ec2-user then use the following command like


  1. You won't believe me, but I was planning to write a blog very similar to the one you have posted here. Great work!

    AWS Training In Chennai

    Hadoop Training In Chennai


Post a Comment

Popular posts from this blog

Search Engine Optimization (SEO) Tricks

6 Key Search Engine Optimization (SEO) tricksStressed and frustrated with your Website's lower rankings on the Search Engines?The tricks and tactics listed below can tremendously change the entire scenario of your website's ranking and rating on Search Engine Results Pages (SERPs) and can take your website to a whole new level, with which increase in relevant traffic on the website is easily observable. The tricks discussed here needs to be implemented in the most desirable way possible. Paginate Your Content: The content on your website is the main facial asset with which you can either impress your audience if the content is paginated well, or you can confuse the hell out of them if the content is unpaginated. So, make sure to arrange everything in a well-ordered manner. Organize CSS For Spiders: Organizing CSS for spiders is a crucial factor that must be considered in order to let the Search Engines know what you are up to and what exactly are you trying…

Android & iOS Application Development For Dummies

App Development ProcessApp Development Process explained in the most simplified way This tutorial will guide all the beginners how an app is developed from scratch and will help in letting the beginners know how the process works.Phase-1 (Conceptualization): Even a concept or an idea starts with an approach to introduce something after acknowledging the inkling of amiss or what's missing in the existing market. Developing the aftermath ideas involves critical questioning and rational thinking towards the wire-framing stage. Phase-2 (Design): After analyzing the idea thoroughly and looking through all possible dimensions, comes the design part, where a feasible layout of the model is planned to move ahead for the further development processes. Phase-3 (Coding): Development process begins with designing a user-friendly UI. Prototypes of the model are prepared with the combined agreement of developers and designers to increase the efficiency and capitalization. The expedience checking r…

Digitization of Restaurant Chains

How Restaurant Businesses Are Transforming With Mobile Apps The Interaction Between Apps and Customers: Customers interact with mobile phones more often than we can imagine and this interaction can be efficiently utilized when it comes to ordering food, which in turn makes it hassle-free for the customers to order food. Just by looking at the online menu on their mobile phones, customers can effortlessly feed themselves with your service and at the same time, your sales graph can jump vertically upwards. While a customer easily gets all information regarding your restaurant, your restaurant gets famous at the same time. In that sense, mobile apps have restructured the way customers and restaurants interact with each other. Business Profits With Mobile Apps:Mobile apps for restaurants benefits them through multiple dimensions. From push notifications to personalized offers, the customer engagement becomes more thrifty with your catering. To explain it with an example, consider the restau…