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 email@example.com:/home/ec2-user
arpitaggarwal-key-pair.pem refers to private key file.
firstname.lastname@example.org 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 email@example.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"
# Where your java installation lives JAVA_HOME="/usr/lib/jvm/java-1.7.0-openjdk-184.108.40.206.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:
The complete source code of spring web application is hosted on github.