How to Install and Configure Tomcat 10 on Ubuntu 22.04: A Step-by-Step Guide

How to Install and Configure Tomcat 10 on Ubuntu 22.04 A Step-by-Step Guide

Apache Tomcat is a widely used open-source web server and servlet container that is designed to run Java-based web applications. The latest version of Tomcat, Tomcat 10, comes with several new features and improvements. In this article, we will guide you on how to install and configure Tomcat 10 on Ubuntu 22.04.

Prerequisites

Before proceeding with the installation, ensure that you have the following prerequisites:

  • A server running Ubuntu 22.04.
  • A sudo user account.
  • Java Development Kit (JDK) version 11 or later installed on your system.

Update Ubuntu Packages

Before installing Tomcat, it is essential to update the Ubuntu package repository to ensure that the system has the latest software packages.

To update the Ubuntu package repository, run the following command in the terminal:

sudo apt update

Install Java Development Kit (JDK)

Before we can install Tomcat, we need to ensure that Java is installed on our system. Tomcat is a Java-based application, so we need to have Java Development Kit (JDK) installed on our system.

Tomcat 10 requires JDK 11 or later to be installed on your system. If you haven’t already installed JDK, run the following command in the terminal to install it:

sudo apt install openjdk-11-jdk

This will install the OpenJDK 11 JDK package that is available in the Ubuntu package repository.

To verify that JDK has been successfully installed on your system, run the following command:

java -version

If JDK is successfully installed, you should see output similar to the following:

openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.22.04)
OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.22.04, mixed mode, sharing)

Creating a System User

It is a good practice to create a dedicated system user to run Tomcat instead of using the default system user. We can create a new system user called “tomcat” by running the following command:

sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat

This command will create a new system user called "tomcat" with the home directory set to "/opt/tomcat".


Downloading Tomcat:

Next, we need to download the Tomcat binary distribution from the Apache Tomcat website. To download Tomcat 10, visit the Apache Tomcat download page at https://tomcat.apache.org/download-10.cgi and select the binary distribution for your system.

Once you have selected the distribution, copy the download link for the latest version of Tomcat 10.

In the terminal, use the wget command to download the Tomcat 10 binary distribution:

wget [Tomcat 10 download link]

For example:

cd /tmp
wget https://downloads.apache.org/tomcat/tomcat-10/v10.1.7/bin/apache-tomcat-10.1.7.tar.gz

This will download the Tomcat 10 binary distribution in the “/tmp” directory.

After downloading the Tomcat 10 binary distribution, extract the archive to a directory of your choice. I will create a Tomcat home folder at /opt/Tomcat and extract the downloaded archive file into it by running the following commands in the terminal:

sudo mkdir /opt/tomcat
sudo tar -zxvf apache-tomcat-10.*.tar.gz -C /opt/tomcat --strip-components=1

Next, to give the tomcat user control of the entire /opt/Tomcat directory and make all the scripts in the bin location executable, you can run the following commands:

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

Creating SystemD Unit File

We can create a SystemD unit file to manage the Tomcat service. A SystemD unit file is a configuration file that defines how SystemD should manage a service. We can create a new SystemD unit file called "tomcat.service" in the "/etc/systemd/system" directory by running the following command:

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

We can then add the following lines to the file:

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'

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

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Save the file and exit the editor.

This SystemD unit file defines how SystemD should manage the Tomcat service. It specifies the location of the Java home directory, the Tomcat installation directory, and the Tomcat environment variables. It also defines the user and group that should run the service, and sets the restart policy for the service.

Reload the systemd daemon with the following command:

sudo systemctl daemon-reload

Start the Tomcat service and enable it to start automatically at system boot with the following commands:

sudo systemctl start tomcat
sudo systemctl enable tomcat

To check the service status, run the following command:

sudo systemctl status tomcat

Output:

● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2022-05-09 14:36:24 CDT; 1 day 6h ago
 Main PID: 1234 (java)
    Tasks: 59 (limit: 4613)
   Memory: 857.1M
   CGroup: /system.slice/tomcat.service
           └─1234 /usr/lib/jvm/java-11-openjdk-amd64/bin/java -
....

Since Tomcat is managed as a systemd service on Linux systems, you can use the same commands to start, stop, and restart it as you would for any other systemd service.

Here are the basic commands you can use:

sudo systemctl start tomcat
sudo systemctl stop tomcat
sudo systemctl restart tomcat

Configuring Firewall

We need to configure the firewall to allow traffic to the Tomcat service. We can do this by creating a new firewall rule to allow incoming traffic on port 8080, which is the default port that Tomcat listens on. We can create this rule by running the following command:

sudo ufw allow 8080/tcp

This will allow incoming traffic on port 8080.


Configuring Tomcat Web Management Interface

Tomcat provides a web-based management interface that allows us to manage Tomcat using a web browser. By default, this interface is not enabled. We can enable it by editing the “tomcat-users.xml” file located in the “conf” directory of the Tomcat installation directory. We can edit this file by running the following command:

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

We can add the following lines to the file, just before the closing </tomcat-users> tag:

<tomcat-users>
<!--
    Comments
-->
   <role rolename="admin-gui"/>
   <role rolename="manager-gui"/>
   <user username="admin" password="admin_password" roles="admin-gui,manager-gui"/>
</tomcat-users>

This will create a new user called “admin” with the password “admin” that has the “manager-gui” role. This user will be able to access the Tomcat web management interface. Please change the username and password to according your choice.


Test the Tomcat Installation

To test whether the Tomcat installation was successful, you can open a web browser and navigate to the following address: http://<your_domain_or_IP_address>:8080

http://<server-ip>:8080

Replace <server-ip> with the IP address of your server. If everything is working correctly, you should see the Tomcat welcome page.

We can also access the Tomcat web management interface by navigating to the following URL:

http://<server-ip>:8080/manager/html

This will prompt us for a username and password. We can enter the "admin” username and password that we configured earlier to access the web management interface.


Conclusion

In this article, we have discussed how to install Tomcat 10 on Ubuntu 22.04. We started by updating the Ubuntu package repository and installing the Java Development Kit (JDK). We then downloaded and extracted the Tomcat 10 binary distribution, created a symbolic link, and set environment variables. Finally, we started the Tomcat service, enabled automatic startup, and verified that it was running.

By following these steps, you can successfully install Tomcat 10 on Ubuntu 22.04 and start deploying your Java-based web applications.

Got questions or suggestions on the article? Don’t hesitate to leave a comment below. Your feedback is valuable to us and helps improve the content quality. And don’t forget to share this article with others who might find it useful. Looking forward to hearing from you!

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Related Posts

Leave a Reply

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

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