This guide outlines the steps to deploy a project to a Tomcat server using Jenkins automation.
- Launch an EC2 instance with Amazon Linux 2 AMI.
- Open TCP ports 8080 (Jenkins UI) and 22 (SSH) in the Security Group.
- Install Java:
sudo amazon-linux-extras install java-openjdk11
- Install Jenkins:
sudo yum install jenkins
- Start and enable Jenkins service:
sudo systemctl start jenkins sudo systemctl enable jenkins - (Optional) Set a hostname for the server:
Enter
sudo vi /etc/hostname
jenkins-serverin the file.
- Install Git on Jenkins server:
sudo yum install git
- Install GitHub plugin on Jenkins UI.
- Navigate to the
/optdirectory:cd /opt - Download and extract Apache Maven:
wget <Maven tar file link> tar -xvzf <Maven tar file> mv <extracted directory> maven
- Set up environment variables for Java and Maven:
Add the following lines:
vi ~/.bash_profileM2=/opt/maven/bin M2_HOME=/opt/maven JAVA_HOME=<path to Java> PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2:$M2_HOME
- Use the following command to find the path to Java:
find /usr/lib/jvm -name java
- Use the following command to find the path to Java:
- Apply changes:
source ~/.bash_profile
- Verify Maven installation:
mvn -v
- Install Maven Integration plugin on Jenkins UI.
- Configure Maven in Jenkins:
- Manage Jenkins > Global Tool Configuration
- Add Maven: Name -
maven, MAVEN_HOME -/opt/maven, untick "Install automatically" - Add Java: Name -
java, JAVA_HOME -<path to Java>, untick "Install automatically" - Use the following command to find the path to Java:
find /usr/lib/jvm -name java
- Launch another EC2 instance with the same Security Group and AMI.
- Download and extract Apache Tomcat.
- Install Java:
sudo amazon-linux-extras install java-openjdk11
- Create symbolic links for starting and stopping Tomcat:
ln -s /opt/tomcat/bin/startup.sh /usr/local/bin/tomcatup ln -s /opt/tomcat/bin/shutdown.sh /usr/local/bin/tomcatdown
- Update
context.xmlfile to access the Manager page:Edit thefind /opt/tomcat -name context.xml
context.xmlfiles in thewebapps/host-manager/META-INFandwebapps/manager/META-INFdirectories. Comment out the following lines in these files:<!-- <Valve className= ....... ...........::1|0:0:0:1" /> -->
- Update
tomcat-users.xmlfile for Manager access:Add the following lines to the end of the file:sudo vi /opt/tomcat/conf/tomcat-users.xml
<role rolename="manager-gui"/> <role rolename="manager-script"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status"/> <user username="deployer" password="deployer" roles="manager-script"/> <user username="tomcat" password="s3cret" roles="manager-gui"/>
- Restart Tomcat service:
tomcatdown tomcatup
- Install "Deploy to container" plugin on Jenkins UI.
- Add Tomcat credentials in Jenkins:
- Manage Jenkins > Credentials > Global
- Add new credentials of type Username and Password.
- Create a new Jenkins job (e.g., "deployToTomcat") as a Maven project.
- Configure Git repository URL in SCM section. That is: https://github.com/HarshGupta-coder/hello-world.git
- Set Maven build goals and options to
clean install. - Add a post-build action "Deploy war/ear to a container":
- WAR/EAR files:
**/*.war - Add container:
Tomcat 8.x Remote - Credentials: Use previously added credentials
- Tomcat URL:
http://<instance_ip>:8080/
- WAR/EAR files:
Speacial Thanks to AR Shankar | Valaxy Technologies (AWS, DevOps Specialist)

