Deploy war file in tomcat [2 Steps]

In this Article we are going to learn Install OpenJDK on Ubuntu 22.04 LTS,Download Apache Tomcat on Ubuntu 22.04 LTS, Install Apache Tomcat on Ubuntu 22.04 LTS,How to Deploy war file in Tomcat on Ubuntu 22.04 LTS.

What is Apache Tomcat ?

Apache Tomcat, also known as Tomcat Server or simply Tomcat, is an open-source web server and servlet container developed by the Apache Software Foundation. It is designed to execute Java servlets and render JavaServer Pages (JSPs) to provide a Java-based web application environment.

Tomcat is one of the most widely used web servers and is highly popular among Java developers. It acts as a web server by processing HTTP requests, serving static web content, and supporting various Java-based technologies, including Java Servlet, JavaServer Pages (JSP), Java Expression Language (EL), and WebSocket.

Features of Apache Tomcat:

  1. Servlet Container: Tomcat provides a runtime environment to execute Java servlets, which are Java classes that handle HTTP requests and produce responses. It manages the lifecycle of servlets, including their initialization, invocation, and destruction.
  2. JSP Support: Tomcat supports JavaServer Pages (JSP), which are dynamic web pages that allow embedding Java code within HTML or XML templates. Tomcat compiles JSPs into Java servlets and executes them.
  3. Web Server Capabilities: Tomcat can serve static web content, such as HTML, CSS, JavaScript, and image files. It can handle HTTP requests and responses, manage sessions, and process cookies.
  4. Connectors: Tomcat includes connectors that allow it to communicate with different web servers, such as Apache HTTP Server or Microsoft IIS, using standard protocols like HTTP or AJP (Apache JServ Protocol). This enables Tomcat to be used as a standalone server or integrated into existing server infrastructures.
  5. Security: Tomcat provides various security features, including SSL/TLS support, authentication mechanisms (such as Basic, Digest, and Form-based authentication), and access control configurations to protect web applications.
  6. Clustering and Load Balancing: Tomcat supports clustering, where multiple Tomcat instances can be grouped together to enhance scalability and high availability. It also integrates with load balancers to distribute incoming requests across multiple Tomcat servers.
  7. Management and Monitoring: Tomcat includes management tools and interfaces to monitor server status, configure resources, deploy and undeploy web applications, and view log files.

Due to its simplicity, lightweight nature, and robustness, Apache Tomcat is a popular choice for deploying Java web applications, ranging from small-scale projects to enterprise-level applications.

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

Output:

openjdk version "11.0.19" 2023-04-18
OpenJDK Runtime Environment (build 11.0.19+7-post-Ubuntu-0ubuntu122.04.1)
OpenJDK 64-Bit Server VM (build 11.0.19+7-post-Ubuntu-0ubuntu122.04.1, mixed mode, sharing)

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 

sudo wget https://mirror.olnevhost.net/pub/apache/tomcat/tomcat-9/v9.0.35/bin/apache-tomcat-9.0.35.zip

Deploy war file in tomcat [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

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

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

Output:

● tomcat.service - Tomcat webs servlet container
     Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-06-22 07:45:57 UTC; 5h 30min ago
    Process: 4541 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
   Main PID: 4548 (java)
      Tasks: 30 (limit: 4686)
     Memory: 268.9M
        CPU: 38.623s
     CGroup: /system.slice/tomcat.service
             └─4548 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties>

Jun 22 07:45:57 ip-172-31-4-113 systemd[1]: Starting Tomcat webs servlet container...
Jun 22 07:45:57 ip-172-31-4-113 startup.sh[4541]: Tomcat started.
Jun 22 07:45:57 ip-172-31-4-113 systemd[1]: Started Tomcat webs servlet container.

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"/>
<role rolename="admin-gui"/>
<role rolename="manager"/>
<role rolename="manager-gui"/>

<user username="devopshint" password="pwd" roles="admin,admin-gui,manager,manager-gui"/>

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 war file in tomcat [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 war file in tomcat [2 Steps] 3

Restart Tomcat service:

sudo systemctl restart tomcat

Step #9:Access the Tomcat Web interface

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

Deploy war file in tomcat [2 Steps] 4

Step #10:Run catalina.sh file

Go to the bin location in the tomcat folder. Run the below command.

cd bin/

#This will start the tomcat server and it should be running on port number 8080.

./catalina.sh start 

Make sure you open the port in the security group.

Step #11:Download the War file and Deploy War file in Tomcat

Go to the tomcat folder and change your location to webapps. Download the sample.war file to this location, deploy war file in tomcat, You can download the war file from the below url.

 https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war. 

You can run the below command to download .

cd webapps
wget https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war

Step #12:Go to the browser and access the war file on browser 

    ipaddress:8080/sample

Deploy war file in tomcat [2 Steps] 5

You should be able to see the sample war file running in the tomcat server.

If you are getting any errors, you can look at the logs for the tomcat servers. The logs are present in the logs folder under the tomcat directory.

cd logs
sudo nano catalina.out 

We have covered Deploy war file in Tomcat.

Conclusion:

In this article we have covered Install OpenJDK on Ubuntu 22.04 LTS,Download Apache Tomcat on Ubuntu 22.04 LTS, Install Apache Tomcat on Ubuntu 22.04 LTS,How to Deploy war file in Tomcat on Ubuntu 22.04 LTS.

Related Articles:

How to Install Tomcat 9 on Ubuntu 20.04 LTS

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

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