How to Install Tomcat 9 on Debian 12

|
Last Updated:
|
|

How can I install Apache Tomcat 9 on Debian 12? This guide provides step by step tutorial on how you can install Tomcat 9 on Debian 12. Apache Tomcat implements the Java Servlet and the JavaServer Pages (JSP)
specifications from Oracle, and provides a “pure Java” HTTP web server environment for Java code to run.

Installing Tomcat 9 on Debian 12

Apache Tomcat 10 is the current default version of Apache Tomcat that is available on Debian 12. If are you running some Java based app on Debian 12 that requires Apache Tomcat9, then this is the tutorial.

Install the Necessary Package Dependencies

Tomcat9 requires a Java Development Kit. You can install JDK 11 or later version. We will use OpenJDK 17, which is the default version provided by Debian 12 repos, in this guide. Hence, execute the command below to install OpenJDK 17 on Debian 12;

sudo apt update
sudo apt install openjdk-17-jdk

Confirm the version.

java -version
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment (build 17.0.8+7-Debian-1deb12u1)
OpenJDK 64-Bit Server VM (build 17.0.8+7-Debian-1deb12u1, mixed mode, sharing)

Create Tomcat System User

Just like any other web server, Apache Tomcat should not be run with a privileged user. Hence, create a system user for Apache Tomcat as follows (we set the home directory for this user as /opt/tomcat9);

useradd -r -d /opt/tomcat9 -s /bin/false tomcat

Create tomcat user home directory;

mkdir /opt/tomcat9

Download Apache Tomcat 9 Archive

Next, navigate to Apache Tomcat 9 downloads page and download Tomcat 9 archive.

The current release version of Apache Tomcat 9 is v9.0.80.

You can replace the value of the VER variable below with the current version of Apache Tomcat 9.

VER=9.0.80
wget https://dlcdn.apache.org/tomcat/tomcat-9/v${VER}/bin/apache-tomcat-${VER}.tar.gz

Install Apache Tomcat 9 on Debian 12

Extract the Apache Tomcat 9 binary to /opt/tomcat9 directory.

tar xzf apache-tomcat-${VER}.tar.gz -C /opt/tomcat9 --strip-components=1

Confirm the contents of Apache Tomcat’s home directory;

ls -alh1 /opt/tomcat9/
total 156K
drwxr-xr-x 9 root root 4.0K Aug 29 13:19 .
drwxr-xr-x 3 root root 4.0K Aug 29 13:19 ..
drwxr-x--- 2 root root 4.0K Aug 29 13:19 bin
-rw-r----- 1 root root  20K Aug 23 17:59 BUILDING.txt
drwx------ 2 root root 4.0K Aug 23 17:59 conf
-rw-r----- 1 root root 6.1K Aug 23 17:59 CONTRIBUTING.md
drwxr-x--- 2 root root 4.0K Aug 29 13:19 lib
-rw-r----- 1 root root  56K Aug 23 17:59 LICENSE
drwxr-x--- 2 root root 4.0K Aug 23 17:59 logs
-rw-r----- 1 root root 2.3K Aug 23 17:59 NOTICE
-rw-r----- 1 root root 3.4K Aug 23 17:59 README.md
-rw-r----- 1 root root 6.8K Aug 23 17:59 RELEASE-NOTES
-rw-r----- 1 root root  17K Aug 23 17:59 RUNNING.txt
drwxr-x--- 2 root root 4.0K Aug 29 13:19 temp
drwxr-x--- 7 root root 4.0K Aug 23 17:59 webapps
drwxr-x--- 2 root root 4.0K Aug 23 17:59 work

Ensure that tomcat user owns the directories, work, temp, webapps and logs;

chown -R tomcat: /opt/tomcat9/{logs,temp,webapps,work}

Next, you need to set the group ownership of the rest of the files/directories within the Tomcat 9 directory to tomcat.

chown -R :tomcat /opt/tomcat9/

Assign tomcat group the read permissions on the Tomcat 9 configuration files directory.

chmod -R g+r /opt/tomcat9/conf

Next, assign the group ownership the execution permissions on the Tomcat 9 configuration files directory.

chmod g+x /opt/tomcat9/conf

Configure Apache Tomcat 9 Environment Variables

Environment variables are used by the Tomcat startup scripts to prepare the command that starts Tomcat.

To begin with, set the CATALINA_HOME environment variable to the above created Tomcat directory where the binary files exist.

echo 'export CATALINA_HOME="/opt/tomcat9"' > /etc/profile.d/tomcat9.sh

Also, you need to set JRE_HOME (JRE) or JAVA_HOME (JDK) environment variable for the Java version you have installed.

You can find the path with update-java-alternatives command.

update-java-alternatives -l
java-1.17.0-openjdk-amd64      1711       /usr/lib/jvm/java-1.17.0-openjdk-amd64

Hence,

echo 'export JAVA_HOME="/usr/lib/jvm/java-1.17.0-openjdk-amd64"' >> /etc/profile.d/tomcat9.sh 

Reload the environment variables set above.

source /etc/profile.d/tomcat9.sh

Configure Tomcat Web Management Accounts

If you wish to administer Apache Tomcat from web, you can define a user for the web management of Tomcat 9 Admin/Manager User interfaces. This can be done by editing the /opt/tomcat9/conf/tomcat-users.xml file and updating username, password and specific roles between the <tomcat-users> and </tomcat-users> tag.

vim /opt/tomcat9/conf/tomcat-users.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
<!--
  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.

  Built-in Tomcat manager roles:
    - manager-gui    - allows access to the HTML GUI and the status pages
    - manager-script - allows access to the HTTP API and the status pages
    - manager-jmx    - allows access to the JMX proxy and the status pages
    - manager-status - allows access to the status pages only

  The users below are wrapped in a comment and are therefore ignored. If you
  wish to configure one or more of these users for use with the manager web
  application, do not forget to remove the <!.. ..> that surrounds them. You
  will also need to set the passwords to something appropriate.
-->
<!--
  <user username="admin" password="<must-be-changed>" roles="manager-gui"/>
  <user username="robot" password="<must-be-changed>" roles="manager-script"/>
-->
<!--
  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
</tomcat-users>

Configure Tomcat Manager and Host Manager apps Remote Access

Configure Tomcat to allow remote connection to Manager and Host Manager apps. Hence, edit the configuration files below for Manager and Host Manager respectively and enter the IP addresses of the remote server you are accessing the Tomcat from. The IPs are separated by a pipe, |.

vim /opt/tomcat9/webapps/manager/META-INF/context.xml
...
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
          allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.43.96" />
...
vim /opt/tomcat9/webapps/host-manager/META-INF/context.xml
...
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.43.96" />
...

Make the necessary changes, save and exit the file.

Running Tomcat 9

Tomcat can be run by executing the startup script, /opt/tomcat9/bin/startup.sh.

If you are accessing Apache Tomcat externally, you need to open port 8080 on UFW.

ufw allow 8080

Running Tomcat 9 startup script

/opt/tomcat9/bin/startup.sh
Using CATALINA_BASE:   /opt/tomcat9
Using CATALINA_HOME:   /opt/tomcat9
Using CATALINA_TMPDIR: /opt/tomcat9/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.17.0-openjdk-amd64
Using CLASSPATH:       /opt/tomcat9/bin/bootstrap.jar:/opt/tomcat9/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

You can now access your Tomcat 9 using the address, http://server-IP:8080

Install Tomcat 9 on Debian 12

To access the Tomcat Web Application Manager, click Manager App. You will be prompted to login. Supply the credentials you set above.

Similarly, if you need to access host manager, you need to have setup the credentials as shown above.

If you want to stop Tomcat, just run the command below;

/opt/tomcat9/bin/shutdown.sh

Create Systemd Service Unit for Tomcat

You can create a systemd service unit file for Tomcat to make it easily controlled.

vim /etc/systemd/system/tomcat9.service
[Unit]
Description=Apache Tomcat 9 Web Application Server
Documentation=https://tomcat.apache.org/tomcat-9.0-doc/index.html
After=network.target

[Service]
Type=forking
User=tomcat
Group=tomcat

Environment=JAVA_HOME=/usr/lib/jvm/java-1.17.0-openjdk-amd64
Environment="CATALINA_HOME=/opt/tomcat9"
Environment="CATALINA_BASE=/opt/tomcat9"
Environment="JAVA_OPTS=-Djava.awt.headless=true"

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


[Install]
WantedBy=multi-user.target

Start and enable the service;

systemctl daemon-reload
systemctl enable --now tomcat9

Check status;

systemctl status tomcat9
● tomcat9.service - Apache Tomcat 9 Web Application Server
     Loaded: loaded (/etc/systemd/system/tomcat9.service; enabled; preset: enabled)
     Active: active (running) since Tue 2023-08-29 14:34:13 EDT; 3min 36s ago
       Docs: https://tomcat.apache.org/tomcat-9.0-doc/index.html
   Main PID: 22759 (java)
      Tasks: 30 (limit: 2304)
     Memory: 74.5M
        CPU: 3.615s
     CGroup: /system.slice/tomcat9.service
             └─22759 /usr/lib/jvm/java-1.17.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat9/conf/logging.properties -Djava.util.logging.manager>

Aug 29 14:34:13 debian systemd[1]: Starting tomcat9.service - Apache Tomcat 9 Web Application Server...
Aug 29 14:34:13 debian startup.sh[22752]: Tomcat started.
Aug 29 14:34:13 debian systemd[1]: Started tomcat9.service - Apache Tomcat 9 Web Application Server.

Well, that is all on how to install Apache Tomcat 9 on Debian 12.

You can check our other guides below;

Install ManageEngine AssetExplorer on Ubuntu 18.04

Install AnyDesk on Ubuntu 18.04

Install Prometheus on Ubuntu 18.04

Configure Sendmail to Use Gmail Relay on Ubuntu 18.04/Debian 10/9

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
Kifarunix
Linux Certified Engineer, with a passion for open-source technology and a strong understanding of Linux systems. With experience in system administration, troubleshooting, and automation, I am skilled in maintaining and optimizing Linux infrastructure.

Leave a Comment