DevOps

Deploying Web Application on EC2 Instance – AWS

In this post, we will deploy spring web application on EC2 Amazon Linux AMI t2.micro instance following below steps:

Step 1: Set up Amazon EC2 instance following set-up-amazon-ec2-instance.

Step 2: Launch an EC2 instance following ec2-launch-linux-instance.

Step 3: Upload .war file from local machine directory to EC2 user home (/home/ec2-user) directory using secure copy as follows:

scp -i /Users/ArpitAggarwal/arpitaggarwal-key-pair.pem /Users/ArpitAggarwal/hello-spring/target/hello-spring.war ec2-user@ec2-54-218-30-7.us-west-2.compute.amazonaws.com:/home/ec2-user

arpitaggarwal-key-pair.pem refers to private key file. ec2-user@ec2-54-218-30-7.us-west-2.compute.amazonaws.com refers to public dns name of EC2 instance.

Step 4: Connect to your EC2 instance using your private key file and public dns name as follows:

ssh -i /Users/ArpitAggarwal/arpitaggarwal-key-pair.pem ec2-user@ec2-54-218-30-7.us-west-2.compute.amazonaws.com

Step 5: Install Tomcat7 on EC2 instance as a root user:

[ec2-user@ip-10-0-0-28 ~]$ sudo su root
[ec2-user@ip-10-0-0-28 ~]$ yum install tomcat7

Step 6: Copy .war file from ec2-user home directory to webapps folder of tomcat, as follows:

[root@ip-10-0-0-28 ec2-user]# cp hello-spring.war /usr/share/tomcat7/webapps/

Step 7: Edit the JAVA_HOME in /etc/tomcat7/tomcat7.conf to point to JDK 7, replacing

# Where your java installation lives
JAVA_HOME="/usr/lib/jvm/jre"

to

# Where your java installation lives
JAVA_HOME="/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.91.x86_64"

Step 8: Start tomcat as follows:

[root@ip-10-0-0-28 ec2-user]# start tomcat7

Step 9: As tomcat is running on port 8080, we have to allow 8080 port from security group. To do that, from your instance, find out the security group associated and edit the security group adding another Type as Custom TCP Rule, Protocol as TCP, Port Range as 8080 and Source as Anywhere.

Now, access the web application from your browser using public dns name of your ec2 instance as:
http://ec2-54-218-30-7.us-west-2.compute.amazonaws.com:8080/hello-spring/

  • The complete source code of spring web application is hosted on github.
Reference: Deploying Web Application on EC2 Instance – AWS from our SCG partner Arpit Aggarwal at the Arpit Aggarwal blog.

Arpit Aggarwal

Arpit is a Consultant at Xebia in India. He has been designing and building J2EE applications since more than 4 years. He is fond of Object Oriented and lover of Functional programming and his specialties are Java, J2EE, Scala, SOA, Cloud and Big Data Technologies.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Craig
Craig
8 years ago

Hi, first great tutorial. I think I have done everything right however when I launch a web browser to my application I just get a blank page. How do I go about troubleshooting this. Also I had to start tomcat7 with the following command: tomcat7 start as opposed to what you wrote which was start tomcat7. tail localhost_access_log.2016-03-11.txt 1*9.1*1.1*1.187 – – [11/Mar/2016:15:18:31 +0000] “GET /greeting HTTP/1.1” 404 – 1*9.1*1.1*1.187 – – [11/Mar/2016:15:19:44 +0000] “GET /greeting HTTP/1.1” 404 – This seems to suggest the resource is not available as I am getting a 404 error [root@ip-172-31-57-122 tomcat7]# tail catalina.out at java.lang.Thread.run(Thread.java:745)… Read more »

Back to top button