Deploy Java Web Application on Aws
Deploying a Java web application on AWS (Amazon Web Services) provides developers with a powerful, scalable, and flexible environment for hosting applications. Whether you’re running a simple Java-based web app or a complex enterprise-level application, AWS offers the tools to deploy, manage, and scale your application in the cloud. This guide walks you through the essential steps for deploying Java web application on AWS for optimized performance.
Steps to follow to Deploy Java Web Application on Aws
- Create EC2 Instance in Aws.
- Get Elastic IP (Static Public IP).
- Connecting to EC2 Instance Using OpenSSH Client.
- Setup Java and Mysql.
- Tomcat Installation.
- Deploying the WAR File.
Creating EC2 Instance in Aws:
- Go to the AWS Management Console at AWS Management Console.
- Name and Tags:
- Enter a name for your instance, such as “Ecommerce Web Server.”
- Application and OS Images (Amazon Machine Image):
- Choose Ubuntu Server 22.04 LTS (HVM), SSD Volume Type, which is eligible for the free tier.
- Select 64-bit (x86) for the architecture.
- Instance Type:
- Choose t2.micro (1 vCPU, 1 GB RAM), which is suitable for lightweight workloads and also eligible for the free tier.
- Key Pair (Login):
- Select Create new key pair to generate a secure key for SSH access to your instance.
- Enter the key pair name (e.g., ‘ecom’).
- Select RSA as the key pair type.
- Choose the private key file format as ‘.pem’ (if you want to connect via PuTTY, select .ppk).
- Click Create key pair; this will download the .pem file to your system.
- Network Settings:
- For Firewall (Security Groups), select Create security group since we don’t have any right now (AWS will automatically create one for us).
- Make sure Allow SSH traffic from is checked with the option Anywhere for convenience (this will allow us to connect to our instance using SSH from any IP).
- Check both Allow HTTPS traffic from the internet and Allow HTTP traffic from the internet.
- Configure Storage:
- Input the number of GiB you want for storage.
- We’ll be using 1 x 8 GiB, which is equivalent to 8.59 GB.
- Select the storage type General Purpose SSD (gp3).
- Leave the rest of the options as they are and click on the Launch instance button.
- Wait until the launching instance is complete. Once complete, check your instances at the AWS EC2 Instances Console. If the Instance state is Running, then we are ready to connect with our instance using SSH.
- But, before we connect via SSH, we’ll acquire the Elastic IP.
Getting Elastic IP (Static Public IP):
- Currently, we have a dynamically allocated public IP that is not static and may change when we restart, terminate, or resume the instance.
- AWS provides an Elastic IP, which is public and static in nature.
- Open the EC2 Dashboard in the AWS Management Console and click on Elastic IPs.
- Once the page loads, click on the Allocate Elastic IP address button; this will open a page for allocating an Elastic IP address.
- Leave all the default settings as they are and click Allocate to confirm the allocation.
- After allocation, you will see the new Elastic IP address in your list.
- Now, we need to associate this Elastic IP with our instance.
- To do this, select the Elastic IP, click on Actions, and choose Associate Elastic IP address.
- In the Associate Elastic IP address dialog, complete the following:
- In “Resource type,” select “Instance“.
- In “Instance” select your newly created instance (e.g., Ecommerce Web Server).
- In “Private IP address” select the private IP of your instance.
- Click Associate to complete the process. Now our instance has a static public IP.
- You can verify it from the summary of the Elastic IP, which shows the associated instance ID.
Connecting to EC2 Instance Using OpenSSH Client:
- Installing OpenSSH Client:
- For Windows 10/11: Open Settings: Press Windows + I to open the Settings app. Navigate to Apps: Go to Apps > Optional features. Add a feature: Click on Add a feature at the top of the page. Find OpenSSH Client: In the search box, type “OpenSSH Client.” Install: Select the OpenSSH Client from the list and click Install.
- For Windows versions lower than 10: Download Git for Windows, which includes Git Bash with built-in support for the OpenSSH client.
- For Linux users: You can skip this installation, as OpenSSH is already pre-installed on most Linux distributions.
- Connecting to the Instance Using OpenSSH:
- Open the AWS EC2 Instances Console and select your newly created instance by clicking on its Instance ID. This will take you to the summary page for that instance.
- Click the ‘Connect’ button to view connection options. Choose the SSH client option, where you’ll see an example command for connecting.
- Copy the provided example command similar to this (e.g., ssh -i “ecom.pem” [email protected]).
- Now, in your system, open Command Prompt, PowerShell, Git Bash, or Shell as per your environment.
- Navigate to the directory where your private key file (ecom.pem) is stored. Use the cd command to change directories.
- If you are a Linux user, make sure to give the appropriate permission to the ecom.pem file using chmod 400 ecom.pem. If you are a Windows user, make sure to open the terminal with administrative privilege.
- Paste the SSH command you copied earlier into the terminal. Make sure the path to your key file is correct and press Enter to execute the command.
- Since we are connecting, we may see a message about the authenticity of the host. Type yes and press Enter to continue.
- We should now be connected to your Ubuntu EC2 instance, and you’ll see a welcome message or a terminal prompt indicating you’re logged in.
Setup Java and Mysql:
- Since we’re using Ubuntu, execute:
sudo apt update && sudo apt upgrade - to ensure our system is up to date.
- Install the necessary modules:
sudo apt install openjdk-17-jdk mysql-server - Setup JAVA: After installation, you can check the Java version with:
java -version - Now, add the JAVA_HOME path to .bashrc and reload it:
echo ‘export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64’ >> ~/.bashrc && source ~/.bashrc - Setup MySQL Server: After that, make sure to start the MySQL server:
sudo systemctl start mysql && sudo systemctl enable mysql - Now we will create a new user for our web application on our MySQL server.
- To do this, log into the MySQL shell using root:
sudo mysql -u root -p - Then, run the following commands:
CREATE USER ‘ecom’@’localhost’ IDENTIFIED BY ‘Ecom6201^’; GRANT ALL PRIVILEGES ON *.* TO ‘ecom’@’localhost’ WITH GRANT OPTION; FLUSH PRIVILEGES; - Next, log in as the new user:
mysql -u ecom -p - After logging in, create a new database named ecommercedb:
CREATE DATABASE ecommercedb; - Note: We are using an ORM (Object-Relational Mapping) in our project, so we’ll leave it here, as the ORM will handle the creation of tables based on your models. If you want to create tables manually, you can run the appropriate SQL commands after creating the database.
Tomcat Installation:
- Since the official repository for Ubuntu Server 22.04 LTS only supports up to Tomcat 9, we are going to manually download Tomcat 10.1.24 because the project is using the Jakarta namespace.
- Download Tomcat 10.1.24:
VERSION=10.1.24 - wget https://archive.apache.org/dist/tomcat/tomcat-10/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz
- Ensure the Directory Exists:
sudo mkdir -p /opt/tomcat - Extract the Archive:
sudo tar -xf apache-tomcat-${VERSION}.tar.gz -C /opt/tomcat –strip-components=1 - (Ensure the VERSION variable is still accessible, or manually replace it with the version number of the downloaded Tomcat.)
- Change Ownership:
sudo chown -R ubuntu:ubuntu /opt/tomcat - Set Group Inheritance:
sudo chmod g+s /opt/tomcat - Create a Systemd Service File:
sudo nano /etc/systemd/system/tomcat.service - Add this inside the Systemd Service File (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 26 27 | [Unit] Description=Apache Tomcat Web Application Container After=network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/java-17-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=root Group=root UMask=0007 RestartSec=10 Restart=always ReadWritePaths=/var/www/uploads/ [Install] WantedBy=multi-user.target |
- Configure Tomcat Connector:
- Open the file /opt/tomcat/conf/server.xml in a text editor and update the connector to listen on port 80:
<Connector port=”80″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”443″ maxParameterCount=”1000″ />
- Open the file /opt/tomcat/conf/server.xml in a text editor and update the connector to listen on port 80:
- Reload Systemd:
sudo systemctl daemon-reload - Start Tomcat:
sudo systemctl start tomcat && sudo systemctl enable tomcat - Now, the Tomcat server has started and is serving the default Tomcat page at – http://<instance-public-ip>
Deploying the WAR File:
- Finally, we will replace the default Tomcat page with our own application to make it accessible through the web.
- Upload your WAR file using SCP (Secure Copy Protocol) to /opt/tomcat/webapps – from another terminal.
scp -i <path_to_private_key> <source_file_path> <username>@<remote_host> : <destination_path> - scp -i “ecom.pem” “C:\Users\kumar\IdeaProjects\eShop\target\eShop-1.0-SNAPSHOT.war” [email protected]:/opt/tomcat/webapps/ROOT.war
- Restart Tomcat:
sudo systemctl restart tomcat - Once the upload is complete, Tomcat will automatically detect the new ROOT.war file and redeploy it on restart.
- Also, make sure the Inbound rules allow incoming traffic on the HTTP (port 80) and HTTPS (port 443) protocols under the security section of an instance.
Our web application should now be accessible at http://<instance-public-ip>.
In conclusion, deploying a Java web application on AWS offers lot of benefits, including scalability, flexibility, and robust security. By utilizing AWS’s powerful tools, you can efficiently manage and scale your Java web application to meet the demands of your users.
Leave a Reply