How to Install and Configure Apache Tomcat 10 in Linux Debian 11.3

To Install and Configure Apache Tomcat 10 in Linux Debian 11.3

Introduction

Apache Tomcat is a famous open-source web server and Servlet container for Java code. As the reference implementation of Java Servlet and Java Server Pages (JSP), Tomcat was initiated at Sun Microsystems, which afterward donated the code base to the Apache Software Foundation.

Installation Procedure:

step 1:Check the OS version by using the below command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 11 (bullseye)
Release:	11
Codename:	bullseye

step 2:Next, update the repository by using the below command

root@linuxhelp:~# sudo apt update
Get:1 http://security.debian.org/debian-security bullseye-security InRelease [48                                    .4 kB]
Hit:2 http://repo.mysql.com/apt/debian bullseye InRelease
Hit:3 http://deb.debian.org/debian bullseye InRelease
Get:4 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
Get:5 http://security.debian.org/debian-security bullseye-security/main Sources                                     [142 kB]
4 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@linuxhelp:~# apt list --upgradable
Listing... Done
curl/stable-security 7.74.0-1.3+deb11u2 amd64 [upgradable from: 7.74.0-1.3+deb11                                    u1]
libcurl3-gnutls/stable-security 7.74.0-1.3+deb11u2 amd64 [upgradable from: 7.74.                                    0-1.3+deb11u1]
libcurl4/stable-security 7.74.0-1.3+deb11u2 amd64 [upgradable from: 7.74.0-1.3+d                                    eb11u1]


thunderbird/stable-security 1:91.12.0-1~deb11u1 amd64 [upgradable from: 1:91.11.                                    0-1~deb11u1]

step 3:Install OpenJDK- Java by using the below command

As we know the key requirement to install Tomcat is Java, thus first we set up an open-source Java Development kit on Debian 11 using the terminal.

root@linuxhelp:~# apt install default-jdk
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  galera-4 gsasl-common libcgi-fast-perl libcgi-pm-perl
  libconfig-inifiles-perl libdbi-perl libfcgi-bin libfcgi-perl 

step 4:To check and confirm, that the Java has been installed successfully, use the below command

root@linuxhelp:~# java -version
openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Debian-1deb11u1, mixed mode, sharing)

step 5:We will create a new tomcat group and user to run the Tomcat service under /opt/tomcat directory (Tomcat installation) using the below commands.

root@linuxhelp:~# mkdir /opt/tomcat
root@linuxhelp:~# groupadd tomcat
root@linuxhelp:~# useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

step 6:Now, you shall download tomcat10 package by using the wget command in the following manner.

root@linuxhelp:~# wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.23/bin/apache-tomcat-10.0.23.tar.gz
--2022-08-06 02:57:52--  https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.23/bin/apache-tomcat-10.0.23.tar.gz
Resolving dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
Connecting to dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11972768 (11M) [application/x-gzip]
Saving to: ‘apache-tomcat-10.0.23.tar.gz’
apache-tomcat-10.0.23.tar.gz                               100%[====================================================
2022-08-06 02:57:53 (74.7 MB/s) - ‘apache-tomcat-10.0.23.tar.gz’ saved [11972768/11972768]

step 7:Then, extract the downloaded file by using the below command

root@linuxhelp:~# tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat --strip-components=1
apache-tomcat-10.0.23/conf/
apache-tomcat-10.0.23/conf/catalina.policy
apache-tomcat-10.0.23/conf/catalina.properties
apache-tomcat-10.0.23/conf/context.xml
apache-tomcat-10.0.23/conf/jaspic-providers.xml
apache-tomcat-10.0.23/conf/jaspic-providers.xsd
apache-tomcat-10.0.23/conf/logging.properties
apache-tomcat-10.0.23/conf/server.xml
apache-tomcat-10.0.23/conf/tomcat-users.xml

apache-tomcat-10.0.23/conf/tomcat-users.xsd
apache-tomcat-10.0.23/conf/web.xml
apache-tomcat-10.0.23/bin/
apache-tomcat-10.0.23/lib/
apache-tomcat-10.0.23/logs/

step 8:Assign Tomcat user permissions by using the below command

root@linuxhelp:~# chown -R tomcat: /opt/tomcat
root@linuxhelp:~# sh -c 'chmod +x /opt/tomcat/bin/*.sh'

step 9:Create a Systemd service file by using the below command

By default, we won’t have a Systemd unit file for Tomcat to run it in the background and to easily stop, start and enable its services. Thus, we create one, so that we could manage it without any issues.

root@linuxhelp:~# 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

step 10:In the above-given code for creating a systemd file, we have to mention the path of Java. However, the given one in the above code is the default path, still, to confirm the same you can run the below command

root@linuxhelp:~# sudo update-java-alternatives -l
java-1.11.0-openjdk-amd64      1111       /usr/lib/jvm/java-1.11.0-openjdk-amd64

step 11:Enable and start Tomcat service on Debian 11 by using the below command

root@linuxhelp: ~# sudo systemctl daemon-reload
root@linuxhelp:~# systemctl start tomcat
root@linuxhelp:~# systemctl enable tomcat
Created symlink /etc/systemd/system/multi-user.target.wants/tomcat.service → /et                                    c/systemd/system/tomcat.service.

step 12:Next, check the status from tomcat services by using the below command

root@linuxhelp:~# systemctl status tomcat
● tomcat.service - Tomcat webs servlet container
     Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset>
     Active: active (running) since Sat 2022-08-06 03:01:56 IST; 24s ago
   Main PID: 37170 (java)
      Tasks: 29 (limit: 4620)
     Memory: 127.1M
        CPU: 2.605s
     CGroup: /system.slice/tomcat.service
             └─37170 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.uti>

step 13:Add Roles and Admin username and password by using the below command

This step is important, without performing it we will get an error: “403 Access Denied on Tomcat 10/9/8 error” as we click on “Server Status“, “Manager App” and “Host Manager” links on the Apache Tomcat Web interface. Edit user configuration file.

root@linuxhelp:~# sudo nano /opt/tomcat/conf/tomcat-users.xml
At the end just before </tomcat-users> tag copy and paste the following lines.
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="h2s" password="pwd" roles="admin,admin-gui,manager,manager-gui"/>

step 14:Enable Tomcat and Host Manager Remote access by using the below command

By default, you won’t be able to access your installed Tomcat Manager sections (web interface) outside the local system. For that, 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

root@linuxhelp:~# 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" /> -->

step 15:Next for Host manager remote access use the below command:

root@linuxhelp:~# 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 like this-
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

step 16:Restart Tomcat service by using below commands

root@linuxhelp:~# sudo systemctl restart tomcat 

step 17:Next open 8080 port to allow traffic to the Tomcat service on the firewall

 root@linuxhelp:~# ufw allow 8080

step 18:Open the browser to browse the IP address port number and Access the Tomcat Web interface as shown in the below image

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

step 19:This is Home page of Apache Tomcat.

step 20:Finally check the server status as shown in the below image.

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to install and Configure Apache Tomcat 10 in Linux Debian 11.3. Your feedback is much welcome.

FAQ
Q
Does Tomcat use an Apache HTTP server?
A
Tomcat is primarily an application server, which serves requests to custom-built Java servlets or JSP files on your server.
Q
Is JSP a server-side Scripting language in apache tomcat?
A
Yes, JSP is a server-side Scripting language in apache tomcat.
Q
Why do we need JSPs in apache tomcat?
A
We need JSPs which is easy to code and read in apache tomcat
Q
What is the main purpose of Java servlets in apache tomcat?
A
The main purpose of the java servlets is for sending content to a client which generates dynamic content on the web in apache tomcat.
Q
What does Java Servlet do in Apache Tomcat?
A
A Java Servlet is a java software component that extends the capabilities of a server in apache tomcat.