How to install Apache Tomcat on Ubuntu 22.04

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Hello, friends. In this post, you will learn how to install Apache Tomcat on Ubuntu 22.04 Important if you are a Java web application developer or sysadmin.

Introduction

Apache Tomcat is an open-source Java servlet and Java Server Page container. Developers build and deploy dynamic Java-based applications using Apache Tomcat. Java servlets are small java programs defining how a server handles requests and responses. Developers write the servlets, while Tomcat handles all the backend and routing. As it is an open-source tool, Apache Tomcat is contributed by developers all over the world.

Apache Tomcat is an open-source Java HTTP web server developed by the Apache Software Foundation. Tomcat helps to deploy the Java Servlet and the Java Server Pages and serves them like an HTTP web server.

Let’s get started.

Install Apache Tomcat on Ubuntu 22.04

First, open a terminal or connect via SSH to the server and update it

sudo apt update
sudo apt upgrade

Next, install Java on Ubuntu 22.04 For this post, we will use the default version included in the official repositories.

sudo apt install default-jdk

At the end, you can check the Java version with the following command

java --version

Sample Output:

openjdk 11.0.16 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

The next step is to create a new system user for Tomcat.

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

As you will notice, this user will have its home directory at /opt/tomcat and will not be able to execute shell commands. Its name is tomcat.

Next, you have to download the Tomcat binary from an Apache mirror.

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.23/bin/apache-tomcat-10.0.23.tar.gz -O /tmp/tomcat-10.tar.gz

As you can notice, we are downloading version 10.0.23 Remember that changing the numbering will change the command.

Now unzip the file

sudo -u tomcat tar -xzvf /tmp/tomcat-10.tar.gz --strip-components=1 -C /opt/tomcat

It is best to configure Tomcat as a system service. To achieve this, create a new configuration file.

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

And add the following

[Unit]
Description=Apache Tomcat
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat/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

ExecReload=/bin/kill $MAINPID
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
Configuring the tomcat service
Configuring the tomcat service

Remember to keep an eye on the user which is tomcat. If you chose another name, then you have to be careful in the file.

Save the changes and close the editor.

Refresh the list of services.

sudo systemctl daemon-reload

Start and enable the new service for Tomcat.

sudo systemctl enable --now tomcat

To find out if everything is OK, check the status

systemctl status tomcat
Tomcat status
Tomcat status

You can now open your favorite web browser and visit http://your-server:8080, however, let’s configure it a bit first.

Configuring Apache before use

The first thing we need to do is to set up a user to access the configuration via the web. To achieve this, open the configuration file.

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

And inside the </tomcat-user> section add the following

<role rolename="manager-gui" />
<role rolename="admin-gui" />changes
<user username="admin" password="pass" roles="manager-gui,admin-gui"/>

Replace pass with your password and save the file.

By default, you will not be able to access the Tomcat settings via web. This is for security reasons, but can be inconvenient for developers. To change this behavior, edit another configuration file.

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

Look for the lines starting with

<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

And comment them:

<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
Tomcat configuration
Tomcat configuration

Save the file and exit the editor.

To apply the changes, restart Tomcat.

sudo systemctl restart tomcat

Login to Tomcat

Now if you open your web browser and go to http://your-server:8080 you will see the following screen

Apache Tomcat on Ubuntu 22.04
Apache Tomcat on Ubuntu 22.04

From there you will have access buttons to the Tomcat panel

Tomcat admin panel - Server status
Tomcat admin panel – Server status

Conclusion

In this post, you learned how to install Apache Tomcat on Ubuntu 22.04. I hope it will be useful for your projects.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook