Deploy to Apache Tomcat using Jenkins [2 Steps]

In this article we are going to cover Deploy to Apache Tomcat using Jenkins | Deploy Java App to Tomcat using Jenkins.

Step #1:Install OpenJDK on Ubuntu 22.04 LTS

Firstly update your system packages

sudo apt update

The java version supported is 8 or above. Here we are getting Java 11.

sudo apt install default-jdk

To check and confirm, that Java has been installed successfully.

java -version

Step #2:Download Apache Tomcat on Ubuntu 22.04 LTS

You can get the latest version directly from the official webpage of Tomcat. So you can download from here. Copy the link address of tar.gz 

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.13/bin/apache-tomcat-10.1.13.tar.gz
Deploy to Apache Tomcat using Jenkins [2 Steps] 1

Step #3:Install Apache Tomcat on Ubuntu 22.04 LTS

Extract the downloaded file in /opt directory

Create a directory under /opt to extract files 

sudo mkdir -p /opt/tomcat

Extract the downloaded Tomcat Tar file into the created directory.

sudo tar xzvf apache-tomcat-*tar.gz -C /opt/tomcat --strip-components=1

Create a dedicated user

sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Step #4:Give Tomcat user permissions

Now let’s assign the permission of the folder to the user we have created above for it.

sudo chown -R tomcat: /opt/tomcat
sudo sh -c 'chmod +x /opt/tomcat/bin/*.sh'

Step #5:Create a Systemd service file for tomcat service

By default, we won’t have a Systemd unit file for Tomcat to run in the background and to easily stop, start and enable its services. So here, we are going to create

Create Systemd unit file for tomcat service

sudo nano /etc/systemd/system/tomcat.service

Paste the following block of code in it

[Unit]
Description=Tomcat webs servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat
RestartSec=10
Restart=always 
Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
Environment="JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"

Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

To save the press Ctrl+X, type –Y, and hit the Enter Key.

Step #6:To start, Enable Tomcat service on Ubuntu 22.04

Let’s enable and run the same, start tomcat service and enable tomcat service at system startup.

sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl enable tomcat

To confirm everything is working normally, check the status of service:

sudo systemctl status tomcat

Step #7:Add Roles and Admin username and password

Edit user configuration file.

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

At the end just before </tomcat-users> tag copy and paste the following lines.

Note– Change the username and password values with whatever you want to set for your Tomcat.

<role rolename="admin-gui,manager-gui,manager-script,manager-jmx,manager-status,admin-gui"/>
  <user username="admin" password="password" roles="admin-gui,manager-gui,manager-script"/>

Step #8:Enable Tomcat and Host Manager Remote access

We have to enable it by editing individually the context.xml file available for Tomcat Manager and Host Manager.

For Tomcat Manager’s remote access:

Edit the Context file  

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

In the file, scroll and go to the end and comment out the following block of text-

<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
Just add <!-- at the beginning and --> in the end, after that, this will look something like this-
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

Save the file and exit- Ctrl+X, type- Y, and hit the Enter key.

Deploy to Apache Tomcat using Jenkins [2 Steps] 2

For Host manager remote access:

sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
Just like above, also add <!-- at the beginning and --> at the end of the text given below in the file, after that, this will look like something this-
Just like above, also add <!-- at the beginning and --> at the end of the text given below in the file, after that, this will look like something this-

Save the file and exit.

Deploy to Apache Tomcat using Jenkins [2 Steps] 3

Restart Tomcat service:

sudo systemctl restart tomcat

Step #9:Access the Tomcat Web interface

http://server-ip-addres:9090
or 
http://youdomain.com:9090

Note: Here I have change port number of Tomcat so thats why Here Tomcat port Number is 9090

Step #10:Install openjdk 17 on Ubuntu 22.04 LTS

Firstly Update your System packages:

sudo apt update

To Install Jenkins we need to Install java 17

sudo apt install openjdk-17-jre

To check version of java run the below command:

java -version

Step #11:Install Jenkins on Ubuntu 22.04 LTS

Using curl command lets download

curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

Now lets update system packages:

sudo apt update

Now Install Jenkins:

sudo apt-get install jenkins

Step #12:To start, Enable Jenkins on Ubuntu 22.04

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

Step #13:Access Jenkins on browser

http://server-ip-addres:8080

Deploy to Apache Tomcat using Jenkins

Step #14:Make Sure you have Git and Maven installed on Jenkins Instance

In Jenkins UI, Go to Manage Jenkins -> Tool Section of Jenkins

Deploy to Apache Tomcat using Jenkins [2 Steps] 4
Deploy to Apache Tomcat using Jenkins [2 Steps] 5

Step #15:Install Deploy to Container Plugin in Jenkins

Manage Jenkins -> Manage Plugins -> Available -> Deploy to Container Plugin

Deploy to Apache Tomcat using Jenkins [2 Steps] 6

Step #16:Integrate Tomcat GitHub Project with Jenkins

New Item -> Tomcat Project

In the Configuration Section, Under Source Code Management Fill your Github/BeanStalk/Gitlab Repository URL

For testing you can use one of our Sample Application Named Tomcat Maven App from our Github public Repository.

My repository you can use

Click on Add button displayed near the Credentials drop-down and enter the username and password of your GItHub Repo and Once it is saved. It would be available on the Dropdown for you to select.

Deploy to Apache Tomcat using Jenkins [2 Steps] 7

Step #17:Configure the Post-build Action and Specify the Tomcat Server Details

Drag to the bottom and Go to the Post-build Actions section

On the available options click on the Deploy war/ear to container

Deploy to Apache Tomcat using Jenkins [2 Steps] 8

Fill the required parameters for the plugin. Use the following Screen Shot as the reference

Choose the Context Path in which the application should be installed. It would rename the WAR file before deploying to the server and thereby the application context root would be changed.

Tomcat URL: Instance_ip:8080
Deploy to Apache Tomcat using Jenkins [2 Steps] 9
Deploy to Apache Tomcat using Jenkins [2 Steps] 10

After that Execute the Job you have created by clicking on the Build Now button

Console Output after the Successful build.

At the last line you can see that the WAR file has been generated and deployed on the remote server.

Step #18:Check deployment of tomcat in browser

Open your browser access tomcat app using below syntax

instance-ip:9090/your-war-file
Deploy to Apache Tomcat using Jenkins [2 Steps] 11

Conclusion:

In this article we have covered Deploy to Apache Tomcat using Jenkins | Deploy Java App to Tomcat using Jenkins.

Related Articles:

Push Docker image to AWS ECR using Jenkins pipeline

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap