COMPREHENSIVE GUIDE TO DEPLOYING JAVA WEB APPLICATIONS ON TOMCAT USING MAVEN AND AWS INFRASTRUCTURE
PROJECT OUTLINE
Part 1: Setting Up the Cloud Environment
Part 2: Creating AWS Resources Using AWS CLI
Part 3: Creating an EC2 Instance
Part 4: Building the Java Web App
Part 5: Setting Up the Tomcat Server
Part 6: Tomcat Configuration
Part 7: Deploying from Maven to Tomcat Using Plugin
Part 8: Deploying from Maven to Tomcat Using SCP Command
PART 1: SETTING UP THE CLOUD ENVIRONMENT
-
Sign in to AWS Management Console
- Go to the AWS website and log in to your account.
- Set the region to **US East (N. Virginia) or your preferred region.
Open the CloudShell in the AWS console to run AWS CLI commands
PART 2: CREATING THE RESOURCES USING AWS CLI
1. Create a VPC (Virtual Private Cloud)
- Open the CloudShell in the AWS Console to run AWS CLI commands.
- Run the command to create a VPC: aws ec2 create-vpc --cidr-block 10.1.0.0/16 --region us- east-1 Note: Save the VPC ID shown in the output.
2. Create a Subnet
- Run the command to create a subnet within the VPC:
aws ec2 create-subnet --vpc-id --cidr-block
10.1.1.0/24 --region us-east-1
** Note: Save the Subnet ID.**
3. Create an Internet Gateway
Run the command to create an Internet Gateway:
aws ec2 create-internet-gateway --region us-east-1
**Note: Save the Internet Gateway ID.**
Attach the Internet Gateway to the VPC
aws ec2 attach-internet-gateway --vpc-id --
internet-gateway-id --region us-
east-1-
Create a Route Table
aws ec2 create-route-table --vpc-id --
region us-east-1**Note: Save the Route Table ID.**
- Create a Route for Internet Access aws ec2 create-route --route-table-id --destination-cidr-block 0.0.0.0/0 --gateway-id --region us-east-1
- Associate the Subnet with the Route Table aws ec2 associate-route-table --subnet-id --route-table-id --region us-east-1
PART 3: CREATING AN EC2 INSTANCE
1. Create a Security Group
Run the command to create a security group:
aws ec2 create-security-group --group-name
Security Group Name> --description "Description of
SG" --vpc-id --region us-east-1
**Note: Save the Security Group ID**.
2. **Add Security Group Rules**
Allow SSH (port 22) for connecting to the server:
aws ec2 authorize-security-group-ingress --group-id
<Your Security Group id> --protocol tcp --port 22 --
cidr 0.0.0.0/0 --region us-east-1
**_NB: it is advisable to allow ssh from my ip in the
working environment for security best practices. _**
-
Create a Key Pair
aws ec2 create-key-pair --key-name --
query 'KeyMaterial' --region us-east-1Copy the output into a text editor and save it with your key
pair name.
-
Create an EC2 Instance
aws ec2 run-instances --image-id --count 1 --
instance-type t2.micro --key-name --
security-group-ids --subnet-id
--region us-east-1
Note: For EC2 instance image id check this link https://us-
east-1.console.aws.amazon.com/ec2/home?region=us-east-
1#LaunchInstances:.Note: Save the Instance ID.
- Allocate and Associate an Elastic IP aws ec2 allocate-address --domain vpc --region us-east-1
- Allocate an Elastic IP to the instance. aws ec2 associate-address --instance-id i-07f62fa3f94ddb1bd- -allocation-id --region us-east-1
PART 3: BUILDING THE JAVA WEB APP
-
Connect to the Maven EC2 Instance Server
- Use EC2 instance connect or SSH connect: ssh -i MyCLIKP.pem ec2-user@
-
Maven Installation Steps on Ubuntu:
- Change the Hostname of The EC2 instance sudo hostnamectl set-hostname maven sudo su - ubuntu
Update and Upgrade the Ubuntu EC2
sudo apt update -y
sudo apt upgrade -yInstallation of Java and Git
cd /opt
sudo apt install default-jdk git -yVerify the Installation of Java and Git
Java -version
git --versionInstalling and Extracting Apache Maven
sudo wget https://dlcdn.apache.org/maven/maven-
3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
sudo tar -xvzf apache-maven-3.9.6-bin.tar.gz
- Remove the Zip File and Rename the Extracted File sudo rm -rf apache-maven-3.9.6-bin.tar.gz sudo mv apache-maven-3.9.6/ maven
- Setting Up Environmental Variables for Maven sudo nano ~/.bashrc copy the path below and paste it at the end of the script. export M2_HOME=/opt/maven export PATH=$PATH:$M2_HOME/bin
Restart the .bashrc file
source ~/.bashrcCheck the Installation of Maven
mvn -version
NB: cd from the /opt directory after the InstallationMake a directory named webapp-project and cd into it
mkdir webapp-project
cd webapp-project
NB: the cloning must be done in the webapp-project directory
you created-
Git clone the project repo
git clone https://github.com/JOMACS-IT/web-app.gitAfter cloning, you must change the directory (cd) into the
web-app directory before you start executing any maven (mvn)
commands or goal -
You can start the following maven goals on your project:
- mvn clean
- mvn validate
- mvn compile
- mvn test
- mvn package
PART 4: SETTING UP THE TOMCAT SERVER
Repeat PART 1 and 2 to set up the Tomcat Server
Installing Tomcat on the Tomcat Ubuntu Server
sudo apt install update
sudo apt install upgrade-
Installing and Extracting Apache Tomcat
sudo wget https://dlcdn.apache.org/tomcat/tomcat-
9/v9.0.97/bin/apache-tomcat-9.0.97.tar.gzsudo tar -xzvf apache-tomcat-9.0.97.tar.gz
Remove the Zip File and Rename the Extracted File
sudo rm -rf apache-tomcat-9.0.97.tar.gz
sudo mv apache-tomcat-9.0.97/ /opt/tomcat9Giving Executable Permission to Tomcat
sudo chmod 777 -R /opt/tomcat9Creating Symbolic Links to Start and Stop Tomcat
sudo ln -s /opt/tomcat/tomcat9/bin/startup.sh
/usr/bin/starttomcat
sudo ln -s /opt/tomcat9/tomcat9/bin/shutdown.sh
/usr/bin/stoptomcatStarting and Stopping Tomcat
sudo starttomcat
sudo stoptomcatTo access the Tomcat server on the web browser:
NB: You must add port 8080 to your security group
inbound rules. Use the ip-address of your Tomcat
server and the default port number of Tomcat to access
the homepage of the Tomcat server.
e.g http://ip-address:8080
PART 5: TOMCAT CONFIGURATIONS:
- To gain access to Tomcat's manager's page, you must edit
the context.xml file
- sudo nano /opt/tomcat/webapps/manager/META-
INF/context.xml
- Locate these 2 lines and uncomment it as shown in the image.
- Save and exit
- sudo nano /opt/tomcat/webapps/manager/META-
INF/context.xml
-
Tomcat Users Creation:
- sudo nano /opt/tomcat9/conf/tomcat-users.xml
roles="manager-gui, manager-script, admin-gui, manager-
status"/><user username="Your user name" password="Your password" roles="manager-script, manager-gui, admin-gui"/> - Save and exit
*NB: The first script can be linked to a root user in AWS and the second as an IAM user with limited permissions. Click on this link https://tomcat.apache.org/migration-7.html for more explanation about the roles. *
PART 6: DEPLOYING FROM MAVEN TO TOMCAT USING PLUGIN:
On your maven server, navigate to the .m2 directory
- cd ~/.m2Create a settings.xml file
- sudo nano settings.xml-
Paste this in the settings.xml
TomcatServer # this id must match that of the under the configuration in pom.xml
tomcat username
tomcat password
- Save and exit
-
Edit the pom.xml file
- nano pom.xml Note: Add this plugin
org.apache.tomcat.maven
tomcat7-maven-plugin
2.3
TomcatServer
http://localhost:8080/manager/text #change the localhost to your ip
- Save and exit
- Now run this command to deploy from Maven to Tomcat:
- mvn tomcat7:deploy
- To undeploy, run:
- mvn tomcat7:undeploy
- mvn tomcat7:deploy
PART 7: DEPLOYING FROM MAVEN TO TOMCAT USING SCP COMMAND:
-
Assign a password to the tomcat user (ubuntu) using this
command- sudo passwd ubuntu
-
Enable password authentication
- sudo nano /etc/ssh/sshd_config
-
Locate the PasswordAuthentication line and uncomment by
deleting the #tag- PasswordAuthentication yes
Press CTRL + O to Save and CTRL + X to exit
-
Restart the sshd service
- sudo systemctl restart sshd
On the maven server perform the following actions:
N*B: A connection needs to be established between the Maven
server and the Tomcat server.*
- Copy the keypair of the tomcat server.
- create a .pem file on the maven server to save the tomcat
keypair
- sudo nano tomcatkeypair.pem
- Paste the tomcat keypair copied earlier
- Use CTRL + O to save and CTRL + X to exit
-
Give a read me only permission to the tomcatkeypair.pem
- chmod 400 tomcatkeypair.pem
-
SSH into the Tomcat server
- ssh -i tomcatkeypair.pem (ubuntu@ip-address)
Now run this command to copy from Maven to Tomcat securely:
- scp -i keypair name /path/to/local/artifact.war username@tomcat_server_ip:/path/to/tomcat/webapps/
Conclusion
Deploying a web application on Tomcat using Maven in an AWS environment showcases a hands-on approach to DevOps practices by combining development tools, cloud infrastructure, and deployment strategies. This project is invaluable for modern enterprise environments, where scalable and reliable application hosting is crucial for business success.
*NB: I am looking forward to Junior DevOps internship opportunities. *
- Now run this command to deploy from Maven to Tomcat:
- mvn tomcat7:deploy
- To undeploy, run:
- mvn tomcat7:undeploy
PART 7: DEPLOYING FROM MAVEN TO TOMCAT USING SCP COMMAND:
- Assign a password to the tomcat user (ubuntu) using this command
- sudo passwd ubuntu
- Enable password authentication
- sudo nano /etc/ssh/sshd_config
- Locate the PasswordAuthentication line and uncomment by deleting the #tag
- PasswordAuthentication yes
- Press CTRL + O to Save and CTRL + X to exit
-
Restart the sshd service
- sudo systemctl restart sshd
-
On the maven server perform the following actions:
NB: A connection needs to be established between the Maven server and the Tomcat server.- Copy the keypair of the tomcat server.
- create a .pem file on the maven server to save the tomcat keypair
- sudo nano tomcatkeypair.pem
- Paste the tomcat keypair copied earlier
- Use CTRL + O to save and CTRL + X to exit
- Give permission to the tomcatkeypair.pem
- chmod 400 tomcatkeypair.pem
-
SSH into the Tomcat server
- ssh -i tomcatkeypair.pem (ubuntu@ip-address)
-
Now run this command to securely copy from Maven to Tomcat:
- scp -i keypair name /path/to/local/artifact.war username@tomcat_server_ip:/path/to/tomcat/webapps/
- scp -i tomcatkeypair.pem /home/ubuntu/webapp-project/web-app/target/web-app.war ubuntu@3.142.232.34:/opt/tomcat9/webapps
Conclusion
Deploying a web application on Tomcat using Maven in an AWS environment showcases a hands-on approach to DevOps practices by combining development tools, cloud infrastructure, and deployment strategies. This project is invaluable for modern enterprise environments, where scalable and reliable application hosting is crucial for business success.
NB: Looking forward to Junior DevOps internship opportunities.
Top comments (0)