How to install Tomcat on Ubuntu 22.04

Installing Apache Tomcat 10 on Ubuntu 22.04 : Step by Step Guide

Introduction

Apache Tomcat, often referred to simply as Tomcat, is a popular open-source Java-based web server and servlet container. It provides a reliable and efficient platform for running Java web applications. In this article, we will explore the features and benefits of Apache Tomcat and guide you through the process of getting started with Tomcat.

What is Apache Tomcat?

Apache Tomcat is an implementation of the Java Servlet, JavaServer Pages (JSP), and WebSocket specifications. It acts as a container for running Java web applications, handling the processing of HTTP requests, and managing the lifecycle of servlets and JSPs. Tomcat is widely used in production environments due to its lightweight design, high-performance capabilities, and robust feature set.

How To Install Apache Tomcat On Ubuntu 22.04

In this short tutorial, we will learn how to install Apache Tomcat on Ubuntu 22.04 LTS operating system.  For this purpose we need several dependencies package. The installation will be arranged as following steps :

Prerequisites

Before we begin, make sure we have the following prerequisites:

  • Ubuntu 20.04 installed on our system.
  • Access to a terminal with administrative privileges.
  • Suficient disk space on our system.

Apache Tomcat 10 installation on Ubuntu 20.04 operating system will consist of several steps as will be discussed below.

Step 1: Update System Packages

Before installing Tomcat, it’s always a good idea to update the system packages to their latest versions. Open a terminal and run the following command:

$ sudo apt update
$ sudo apt upgrade

Step 2: Install Java Development Kit (JDK)

Tomcat requires Java to be installed on your system. You can install OpenJDK, which is an open-source implementation of the Java Development Kit. Run the following command to install OpenJDK:

$ sudo apt install default-jdk

Then we will verify if the Java has been installed on our system, by submitting command line :

$ java --version

The output will be as shown below :

ramans@dev01:~$ java --version
openjdk 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 3: Download Apache Tomcat

To download Apache Tomcat source file, we will visit the official Apache Tomcat website (https://tomcat.apache.org) and navigate to the “Downloads” section. Download the latest stable version of Tomcat (e.g., Apache Tomcat 10) by clicking on the appropriate link.

Alternatively, you can use the following command to download Tomcat directly from the terminal, by usitng wget command line as follow :

$ wget https://downloads.apache.org/tomcat/tomcat-10/v10.1.10/bin/apache-tomcat-10.1.10.tar.gz

Output is as shown below :

ramans@dev01:~$ wget https://downloads.apache.org/tomcat/tomcat-10/v10.1.10/bin/apache-tomcat-10.1.10.tar.gz
--2023-07-04 09:05:08-- https://downloads.apache.org/tomcat/tomcat-10/v10.1.10/bin/apache-tomcat-10.1.10.tar.gz
Resolving downloads.apache.org (downloads.apache.org)... 135.181.214.104, 88.99.95.219, 2a01:4f8:10a:201a::2, ...
Connecting to downloads.apache.org (downloads.apache.org)|135.181.214.104|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12139568 (12M) [application/x-gzip]
Saving to: ‘apache-tomcat-10.1.10.tar.gz’

apache-tomcat-10.1.10.tar.gz 100%[=============================================================>] 11,58M 1,63MB/s in 7,5s

2023-07-04 09:05:17 (1,55 MB/s) - ‘apache-tomcat-10.1.10.tar.gz’ saved [12139568/12139568]

Step 4: Extract the Tomcat Archive

Once the download is complete, navigate to the directory where the Tomcat archive was downloaded (by default, the “Downloads” directory). Use the following command to extract the contents of the archive:

$ tar -xf apache-tomcat-<version>.tar.gz

The output is as shown below :

ramans@dev01:~$ sudo tar xzf apache-tomcat-*.tar.gz -C /opt/tomcat

Step 5: Move Tomcat to /opt Directory And Set Permission

Move the extracted Tomcat directory to the /opt directory, which is commonly used for installing third-party software:

$ sudo mv apache-tomcat-<version> /opt/tomcat
$ sudo sh -c 'chmod +x /opt/tomcat/

The output will be as follow :

ramans@dev01:~$ sudo chown -R tomcat: /opt/tomcat 
ramans@dev01:~$ sudo sh -c 'chmod +x /opt/tomcat/apache-tomcat-10.1.10/bin/*.sh'

Step 6: Create Application Account

At this stage, we will configure our tomcat with user accounts to secure access of admin/manager pages. For this purpose we will edit conf/tomcat-users.xml file in your editor and paste the following code inside <tomcat-users> </tomcat-users> tags. On this stage we will change the password in the below configuration with high secured password.

ramans@dev01:~$ sudo nano /opt/tomcat/conf/apache-tomcat-10.1.10/tomcat-users.xml 

Add the following lines to the file:

<!-- user manager can access only the manager section -->
<role rolename="manager-gui" />
<user username="manager" password="bckinfo123" roles="manager-gui" />

<!-- user admin can access manager and admin section both -->
<role rolename="admin-gui" />
<user username="admin" password="bckinfo321" roles="manager-gui,admin-gui" />

Step 7: Allow Remote Hosts to Access Tomcat

By default Tomcat manager and host-manager applications are accessible only for localhost.  To allow access to these pages from the remote system, you need to modify the following configuration file which is located at webapps/manager/META-INF/context.xml. We will modify it by using nano text editor, as follow.

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

Then we will mark the following line to be ignored.

<Context antiResourceLocking="false" privileged="true" >
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
...
</Context>

Step 8: Create Systemd Service File For Tomcat

In this stage, we will create a systemd service file to manage Tomcat service. By using this file, we will start, stop and check the status of Tomcat service. We will create a new file called as /etc/systemd/system/tomcat.service, with the following entries:

ramans@dev01:~$ sudo nano /etc/systemd/system/tomcat.service 
[Unit]
Description=Apache Tomcat 10 Web Application Server
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
Environment="CATALINA_HOME=/opt/tomcat/apache-tomcat-10.1.10"
Environment="CATALINA_BASE=/opt/tomcat/apache-tomcat-10.1.10"
Environment="CATALINA_PID=/opt/tomcat/apache-tomcat-10.1.10/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/apache-tomcat-10.1.10/bin/startup.sh
ExecStop=/opt/tomcat/apache-tomcat-10.1.10/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

Step 9: Start Apache Tomcat

Now we can start the Tomcat server by running the following command:

ramans@dev01:~$ sudo nano /etc/systemd/system/tomcat.service 
ramans@dev01:~$ sudo systemctl daemon-reload 
ramans@dev01:~$ sudo systemctl start tomcat

Then we will check the Tomcat service by submitting command line :

ramans@dev01:~$ sudo systemctl status tomcat
● tomcat.service - Apache Tomcat 10 Web Application Server
Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2023-07-04 09:33:23 WIB; 10s ago
Process: 30831 ExecStart=/opt/tomcat/apache-tomcat-10.1.10/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 30839 (java)
Tasks: 29 (limit: 9406)
Memory: 131.0M
CPU: 4.132s
CGroup: /system.slice/tomcat.service
└─30839 /usr/lib/jvm/java-11-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/apache-tomcat-10.1.10/conf/>

Jul 04 09:33:23 dev01.bckinfo.com systemd[1]: Starting Apache Tomcat 10 Web Application Server...
Jul 04 09:33:23 dev01.bckinfo.com startup.sh[30831]: Tomcat started.
Jul 04 09:33:23 dev01.bckinfo.com systemd[1]: Started Apache Tomcat 10 Web Application Server.

Tomcat Service

To verify that Tomcat is running, open a web browser and enter http://localhost:8080 in the address bar. If everything is configured correctly, you should see the default Tomcat landing page.

Tomcat web GUI

Conclusion

By following the steps outlined in this guide, you should now have Apache Tomcat successfully installed on your Ubuntu system. You can now deploy and run Java-based web applications using Tomcat. Remember to regularly check for updates and security patches for Tomcat to ensure a secure and reliable web server environment.

(Visited 152 times, 1 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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