Voiced by Amazon Polly |
Overview
Amazon Elastic Compute Cloud (EC2) provides scalable compute capacity in the cloud, allowing developers to run virtual servers known as instances. In this blog post, we will explore deploying a Java web application on an Amazon EC2 server using a Web Application Archive (WAR) file. This step-by-step guide will cover the prerequisites, the benefits of using Amazon EC2, and the necessary steps to set up your Java application.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Introduction
A WAR file simplifies the deployment process, encapsulating the entire application into a single archive.
Prerequisites
Before diving into the deployment process, ensure you have the following in place:
AWS Account: Open the AWS Management Console by creating an AWS account.
Amazon EC2 Instance: Set up an Amazon EC2 instance with the necessary security groups allowing inbound traffic on required ports (e.g., port 8080 for Tomcat).
Java Development Kit (JDK): Install JDK on your Amazon EC2 instance to run Java applications.
Step-by-step guide
Step 1: Connect to Your Amazon EC2 Instance
Use SSH to establish a connection to your Amazon EC2 instance:
1 |
ssh -i "rohit-key-new.pem" ubuntu@ec2-65-0-122-41.ap-south-1.compute.amazonaws.com |
Step 2: Installing the Default JRE/JDK
Update the package list and install Java:
1 |
sudo apt install default-jre |
Verify the Java installation:
1 |
java -version |
Step 3: Create a Tomcat user
Create a new tomcat group
1 |
sudo groupadd tomcat |
Now create a new tomcat user that is member of tomcat group
1 |
sudo useradd -s /bin/false -g tomcat -d /opts/tomcats tomcat |
Step 4: Now install tomcat9
Go to this folder
1 2 |
cd /tmp curl -O https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.84/bin/apache-tomcat-9.0.84.tar.gz |
Now install the tomcat to this folder
1 |
sudo mkdir /opt/tomcat |
1 |
sudo tar xzvf apache-tomcat-*tar.gz -C /opt/tomcat --strip-components=1 |
Step 5: Update the permissions
1 |
cd /opt/tomcat |
Assign ownership of the full installation directory to the Tomcat group:sudo chgrp -R tomcat /opt/tomcat
1 2 3 |
sudo chmod -R g+r conf sudo chmod g+x conf sudo chown -R tomcat webapps/ work/ temp/ {logss}/ |
Need to know where JAVA is installed
1 |
sudo update-java-alternatives -l |
Now copy this Java path and store it somewhere
Go to this file and update the content
1 |
sudo vi /etc/systemd/system/tomcat.service |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[Unit] Description=Apache Tomcat Web Application Container After=network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64 Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid Environment=CATALINA_HOME=/opt/tomcat Environment=CATALINA_BASE=/opt/tomcat Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC' Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom' ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/opt/tomcat/bin/shutdown.sh User=tomcat Group=tomcat UMask=0007 RestartSec=10 Restart=always [Install] WantedBy=multi-user.target |
Next, reload the system daemon
1 |
sudo systemctl daemon-reload |
Start the Tomcat service
1 |
sudo systemctl start tomcat |
Check that it started without errors
1 |
sudo systemctl status tomcat |
Deploy Your Java Application
- Manual Deployment
Your WAR file should be uploaded to the Tomcat webapps directory:
1 |
sudo cp /path/to/your/application.war /usr/share/tomcat/webapps/ |
2. Automatic Deployment
Edit the server.xml file:
1 |
sudo nano /etc/tomcat/server.xml |
Add the following lines inside the <Host> element:
1 |
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="your-application-name" reloadable="true"/> </Host> |
Replace your-application-name with the desired context path for your application.
Access Your Java Application
Go to your Amazon EC2 instance’s public IP address:
1 |
http://your-instance-ip:8080/your-application-name |
Conclusion
Deploying a Java web application on an Amazon EC2 server is a straightforward process that offers scalability, cost-effectiveness, and ease of management. Amazon EC2’s flexibility and variety of instance types make it ideal for hosting a wide range of applications. Following the steps outlined in this guide, you can quickly set up your Java application on an Amazon EC2 instance and leverage the benefits of cloud computing.
Drop a query if you have any questions regarding Amazon EC2 and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, and many more.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. Can I deploy multiple Java web applications on the same Amazon EC2 instance using Tomcat?
ANS: – Yes, Tomcat supports deploying multiple applications on the same instance. You can configure multiple contexts within the server.xml file to achieve this.
2. What security considerations should I keep in mind when deploying on Amazon EC2?
ANS: – Ensure that your security groups only allow necessary inbound traffic, and regularly update and patch your instance to address any security vulnerabilities.
3. How can I scale my Java application on Amazon EC2 to handle increased traffic?
ANS: – You can use Elastic Load Balancing (ELB) to distribute incoming traffic across multiple Amazon EC2 instances, ensuring high availability and scalability.
WRITTEN BY Rohit Kumar
Rohit Kumar works as a Research Associate (Infra, Migration, and Security Team) at CloudThat. He is focused on gaining knowledge of the Cloud environment. He has a keen interest in learning and researching emerging technologies.
Click to Comment