πŸš€ Deploy a Java Web App to Remote Tomcat Using Jenkins + Maven | AWS EC2 Setup

🧰 Overview

In a real-world CI/CD setup, Jenkins and Tomcat often run on different servers. This guide shows how to:

βœ… Set up Jenkins on one EC2 instance
βœ… Install and configure Tomcat on a separate EC2 instance
βœ… Deploy a Java WAR file using Maven and Jenkins
βœ… Automatically trigger builds using Poll SCM every minute

πŸ—οΈ Architecture Diagram

[Jenkins EC2 Instance] ──deploys WAR──▢ [Tomcat EC2 Instance]
Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β β”‚Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  β”‚
Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β   └─── Polls Git Repo every minuteΒ  β”€β”€β”€β”˜

βœ… Prerequisites

  • Two EC2 instances on AWS (Amazon Linux or Ubuntu)

  • Jenkins installed on one EC2 instance

  • Tomcat installed on another EC2 instance

  • Maven installed on Jenkins EC2

  • A sample Java project using Maven (pom.xml)

  • Git repository (GitHub, GitLab, Bitbucket, etc.)

  • Properly configured Security Groups:

    • Port 8080 open on Tomcat instance

    • Port 22 open for SSH (optional)

πŸ“¦ Installing Jenkins on Amazon Linux

βœ… Prerequisites

πŸ‘‰ Before you begin, ensure you have:

    • Basic Linux knowledge

    • AWS EC2 instance (Amazon Linux 2)

    • Internet access

    • SSH access to the instance

πŸ›  Step 1: Update Your System

sudo yum update -y

β˜• Step 2: Install Java (Required for Jenkins)

sudo yum install java-17-amazon-corretto -y

βœ… Verify Java version:

java -version

πŸ“¦ Step 3: Add Jenkins Repository

sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo

sudo rpm –import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

sudo yum upgrade

🧰 Step 4: Install Jenkins

sudo yum install jenkins -y

πŸ”„ Step 5: Start and Enable Jenkins

sudo systemctl start jenkins
sudo systemctl enable jenkins
sudo systemctl status jenkins

🌐 Step 6: Allow Port 8080 in Security Group

πŸ” Steps to allow traffic on port 8080:

  1. Go to EC2 Dashboard
  2. Select your Instance
  3. Click on the linked Security Group
  4. Click Edit Inbound Rules
  5. Click Add Rule:
    • Type: Custom TCP
    • Port Range: 8080
    • Source: Anywhere (or your IP)
  6. Click Save Rules

βœ… Now your Jenkins server is publicly accessible via port 8080.

πŸ” Step 7: Access Jenkins UI

Open browser:

http://<your-ec2-public-ip>:8080

Get the initial password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Paste it into Jenkins and complete the setup wizard.

πŸ”Œ Optional: Install Plugins

Choose β€œInstall Suggested Plugins” when prompted during setup. These plugins will help you get started faster.

🧱 Part 1: Tomcat Setup on Separate EC2 Instance

πŸ”Ή Step 1: SSH into the Tomcat EC2 Instance

ssh ec2-user@<tomcat-public-ip>

πŸ”Ή Step 2: Install Java

sudo su
yum update -y
yum install java-17-amazon-corretto

πŸ”Ή Step 3: Install Apache Tomcat

cd /opt/
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.106/bin/apache-tomcat-9.0.106.tar.gz
mv apache-tomcat-9.0.106.tar.gz tomcat.tar.gz
tar -xvf tomcat.tar.gz

πŸ”Ή Step 4: Configure Tomcat for Remote Deployment

Edit tomcat-users.xml

sudo nano /opt/tomcat/conf/tomcat-users.xml

Add

<role rolename=”manager-script”/>
<user username=”jenkins” password=”jenkins123″ roles=”manager-script”/>

Do not add manager-gui role together with manager-script.

Allow Remote Access in context.xml

sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml

Change the line:

<Valve className=”org.apache.catalina.valves.RemoteAddrValve”
allow=”127\.\d+\.\d+\.\d+|::1″/>

To:

<Valve className=”org.apache.catalina.valves.RemoteAddrValve”
allow=”.*”/>

⚠️ For production, replace .* with Jenkins private IP.

Start Tomcat

sudo chmod +x /opt/tomcat/bin/*.sh
sudo /opt/tomcat/bin/startup.sh

Verify Tomcat is running:

curl http://localhost:8080

πŸ”§ Part 2: Jenkins Setup and Job Configuration

πŸ”Ή Step 1: Install Required Jenkins Plugins

Go to Manage Jenkins β†’ Manage Plugins β†’ Available tab, and install:

βœ… Maven Integration Plugin
βœ… Deploy to container Plugin
βœ… Credentials Plugin

πŸ”Ή Step 2: Create a Freestyle Project in Jenkins

  • Open Jenkins dashboard

  • Click New Item

  • Choose Freestyle project

  • Name it devopsProject

πŸ”Ή Step 3: Configure Git Source

  • Under Source Code Management:

    • Choose Git

    • Enter the repository URL

    • Add credentials if required

πŸ“¦ Sample Repo

Use this sample Java Maven project:

πŸ‘‰ https://github.com/srtechops/SampleJavaWebApp

πŸ”Ή Step 4: Set Build Triggers – Poll SCM Every Minute

πŸ”„ Enable Poll SCM in Jenkins Job

πŸ“Œ Step-by-Step

  • Go to your Jenkins job: Dashboard β†’ Your Job β†’ Configure

  • Scroll to Build Triggers section

  • βœ… Check the option: β˜‘οΈ Poll SCM

  • In the Schedule textbox, enter:

* * * * *

πŸ•’ Explanation of Cron Format

* * * * *
β”‚ β”‚ β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”‚ └─ Day of week (0 - 7) [0 or 7 = Sunday]
β”‚ β”‚ β”‚ └─── Month (1 - 12)
β”‚ β”‚ └───── Day of month (1 - 31)
β”‚ └─────── Hour (0 - 23)
└───────── Minute (0 - 59)

βœ… * * * * * β†’ Every minute

πŸ›‘ This does not run the build every minute β€” it checks Git for changes every minute, and triggers the build only if there are changes.

πŸ”Ή Step 5: Configure Maven Build Step

  • Under Build β†’ Invoke top-level Maven targets

  • Goals:

clean package

This will build your WAR file in target/

πŸ”Ή Step 6: Deploy WAR to Remote Tomcat

  • Under Post-build Actions β†’ Add post-build action β†’ Deploy war/ear to a container

    Fill in the following:

    FieldValue
    WAR/EAR files**/target/*.war
    Context Path/myapp
    ContainerTomcat 9.x Remote
    Tomcat URLhttp://<tomcat-private-ip>:8080
    CredentialsAdd jenkins/jenkins123 if not already added

πŸ§ͺ Test Deployment

  1. Click Build Now in Jenkins.

  2. Console output should show:

[DeployPublisher][INFO] Deploying ... to container Tomcat 9.x Remote
Finished: SUCCESS
  1. Visit:

http://<tomcat-public-ip>:8080/myapp/

βœ… Your Java app should now be live!

🐞 Common Issues & Fixes

ErrorReasonFix
401 UnauthorizedWrong credentialsVerify tomcat-users.xml
403 Access DeniedIP not allowed in context.xmlUpdate allow=".*" or specific IP
WAR not foundWrong WAR pathEnsure correct build & target path
Jenkins can’t reach TomcatNetwork issuesCheck security group & private IP

πŸ” Security Tips

  • Use private IPs for Jenkins ⇄ Tomcat communication

  • Restrict Tomcat’s port 8080 to Jenkins instance only

  • Use a reverse proxy (e.g., Nginx) + HTTPS in production

  • Use strong passwords in tomcat-users.xml

βœ… Summary

βœ… TaskStatus
Jenkins and Tomcat on separate EC2 instancesβœ…
WAR deployment using Jenkinsβœ…
Poll SCM every minute for Git changesβœ…
Secure remote Tomcat access configuredβœ…

Devops Multi cloud Training

Choose the training style that fits your schedule β€” Self-Paced or Live Interactive Sessions. Both include hands-on projects, expert support, and lifetime access.

FeatureSelf-Paced TrainingLive Training
🎯 ModeπŸŽ₯Pre-Recorded SessionπŸ§‘β€πŸ«Live Class + Recordings
πŸ’Ό ProjectsπŸ•’ Weekend Real-Time ProjectsπŸ“… Weekdays + Weekend Real-Time Projects
❓ Doubt ClearingπŸ“ž Weekend Live Support Session🧠 Anytime Doubt Clearing Session
πŸ‘₯ Career Support & Mentorship❌ Noβœ… Yes
πŸŽ“ Global Certification Training❌ Noβœ… Yes
πŸ”‘ Access♾️ Lifetime Access♾️ Lifetime Access
πŸ’° Feesβ‚Ή4,999 (2 x β‚Ή2,500)β‚Ή7,999 (2 x β‚Ή4,000)
ℹ️ For More InfoExplore Self-Paced Training Explore Live Training

Leave a Comment

Your email address will not be published. Required fields are marked *

Open chat
Hello, Good day!!
How can we help you?