How to Install Apache Tomcat Server on Debian 11

How to Install Apache Tomcat Server on Debian 11. In this article we will introduce what Tomcat is with its features and move onto installation steps with configuring Nginx as a reverse proxy for Tomcat. Let’s start. 

What is Apache Tomcat

Apache Tomcat is an open source web server with the Servlet container used for deploying Java based web applications. Tomcat server comes with Java Servlet, Java EL and JavaServer Pages that help you to run Java code via web browser. It is a cross platform, so it can be installed and run on Linux and Windows operating systems. It is one of the most widely used Java servers due to its good extensibility, proven core engine and well tested and durable. Tomcat allows developers to write and host Java based dynamic applications on the web. If you are looking to host applications that process thousands of requests then Tomcat is the best option for you.

In simple words, we can say that the Apache Tomcat is actually a server and servlet container to create dynamic websites. Both the server and servlet container combine to form a single server which is popularly known as the “Apache Tomcat”.

Pros of Tomcat

  • Open Source Software
  • Lightweight and flexible
  • Stable.
  • Extra level of security
  • One of the most widely used application servers
  • Provides the most common capabilities for Java web deployments.
  • Seamless integration with the Apache Web Server.

Follow this post below to show you how to install Apache Tomcat 10 on Debian 11.

Install Java JDK

Tomcat is based on Java so Java JDK must be installed on your server. If not installed you can install it by running the following command:

				
					apt-get install openjdk-11-jdk -y
				
			

After the successful installation, verify the Java version using the following command:

				
					java --version
				
			

You will get the following output:

				
					openjdk 11.0.14 2022-01-18
OpenJDK Runtime Environment (build 11.0.14+9-post-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 11.0.14+9-post-Debian-1deb11u1, mixed mode, sharing)
				
			

Install Apache Tomcat Server on Debian 11

Install Tomcat on Debian 11

Before starting it is a good idea to create a dedicated user and group to run Tomcat.

First, create a directory for Tomcat with the following command:

				
					mkdir /opt/tomcat
				
			

Next, create a group and user for Tomcat using the following command:

				
					groupadd tomcat
useradd -g tomcat -d /opt/tomcat -s /usr/sbin/nologin tomcat
				
			

Please download the latest version of Tomcat using the following command:

				
					wget https://downloads.apache.org/tomcat/tomcat-10/v10.0.20/bin/apache-tomcat-10.0.20.tar.gz
				
			

Once the download is completed, extract the downloaded file with the following command:

				
					tar -xvzf apache-tomcat-10.0.20.tar.gz
				
			

Next, copy the extracted directory to the /opt/tomcat directory:

				
					mv apache-tomcat-10.0.20/* /opt/tomcat/
				
			

Then change the ownership of the /opt/tomcat directory:

				
					chown -R tomcat:tomcat /opt/tomcat/
				
			

Once you are finished, you can proceed to the next step.

Create a Service File for Tomcat

Here you will need to create a systemd service file to manage the Tomcat using systemd.

You can create it with the following command:

				
					nano /etc/systemd/system/tomcat.service
				
			

Add the following lines based on your Tomcat path and Java path:

				
					[Unit]
Description=Apache Tomcat 10.x Web Application Container
Wants=network.target
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1G -Djava.net.preferIPv4Stack=true'
Environment='JAVA_OPTS=-Djava.awt.headless=true'
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
SuccessExitStatus=143
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target

				
			

Save and close the file then reload the systemd daemon to apply the changes:

				
					systemctl daemon-reload
				
			

Next, start the Tomcat service and enable it to start at system reboot:

				
					systemctl start tomcat
systemctl enable tomcat
				
			

You can now check the status of the Tomcat service with the following command:

				
					systemctl status tomcat
				
			

You should get the following output:

				
					● tomcat.service - Apache Tomcat 10.x Web Application Container
     Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-04-06 11:55:29 UTC; 6s ago
    Process: 40076 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
   Main PID: 40083 (java)
      Tasks: 34 (limit: 4679)
     Memory: 95.1M
        CPU: 4.899s
     CGroup: /system.slice/tomcat.service
             └─40083 /usr/lib/jvm/java-1.11.0-openjdk-amd64//bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Dj>

Apr 06 11:55:29 debian11 systemd[1]: Starting Apache Tomcat 10.x Web Application Container...
Apr 06 11:55:29 debian11 startup.sh[40076]: Tomcat started.
Apr 06 11:55:29 debian11 systemd[1]: Started Apache Tomcat 10.x Web Application Container.

				
			

By default, Tomcat listens on port 8080. You can check it with the following command:

				
					ss -antpl | grep 8080
				
			

You will get the following output:

				
					LISTEN 0 100 0.0.0.0:8080 0.0.0.0:* users:(("java",pid=40083,fd=43))

				
			

Configure Tomcat Web Management Interface

In the next part of How to Install Apache Tomcat Server on Debian 11, by default Tomcat can be accessed without any user or password. For security reasons, it is a good idea to enable authentication in Tomcat. You can do it by editing the Tomcat configuration file:

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

Add the following lines above the line </tomcat-users>:

				
					rolename="admin-gui,manager-gui"/>
<user username="admin" password="tomcatpassword" roles="manager-gui,admin-gui"/>

				
			

Save and close the file when you are done.

Also, Tomcat manager app and host manager app is accessed only from the localhost. So you will need to configure Tomcat so it can be accessed from the remote system.

For the Manager app, edit the context.xml file:

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

Remove these lines:

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

				
			

For the Host manager app, edit the context.xml file:

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

Remove the following lines:

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

Save and close the file then restart the Tomcat service to apply the configuration changes:

				
					systemctl restart tomcat
				
			

Once you are finished, you can proceed to the next step.

Configure Nginx as a Reverse proxy for Tomcat

It is also recommended to configure Nginx as a reverse proxy for Tomcat. First, install the Nginx web server package with the following command:

				
					apt-get install nginx -y
				
			

Once the Nginx is installed, create an Nginx virtual host configuration file:

				
					nano /etc/nginx/conf.d/tomcat.conf
				
			

Add the following lines:

				
					server {
  listen 80;

  server_name   tomcat.example.com;
  access_log /var/log/nginx/tomcat-access.log;
  error_log /var/log/nginx/tomcat-error.log;

  location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8080/;
  }
}

				
			

Save and close the file then verify the Nginx for any syntax configuration error:

				
					nginx -t
				
			

If everything is fine, you will get the following output:

				
					nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

				
			

Next, restart the Nginx service to apply the configuration changes:

				
					systemctl restart nginx
				
			

You can also check the Nginx status using the following command:

				
					systemctl status nginx
				
			

You should see the following output:

				
					● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-04-06 11:58:13 UTC; 59s ago
       Docs: man:nginx(8)
    Process: 40436 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 40437 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 40520 (nginx)
      Tasks: 3 (limit: 4679)
     Memory: 3.2M
        CPU: 56ms
     CGroup: /system.slice/nginx.service
             ├─40520 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─40522 nginx: worker process
             └─40523 nginx: worker process

Apr 06 11:58:13 debian11 systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 06 11:58:13 debian11 systemd[1]: Started A high performance web server and a reverse proxy server.

				
			

Access Tomcat Web Interface

  1. Open your web browser and access the Tomcat web interface using the URL http://tomcat.example.com. You should see the Tomcat default page:

2. Click on the Manager App. You will be asked to provide username and password as shown below:

3. Provide your admin username, password, and click on the Sign in button. You should see the Manager app dashboard on the following screen:

4. Click on the Host Manager App. You should see the Host Manager app dashboard on the following screen:

Great job, you have succesfully followed all the steps to install Apache Tomcat!

How to Install Apache Tomcat Server on Debian 11 Conclusion

In this article we explained steps how to install Apache Tomcat Server on Debian 11. We also explained configuring Nginx as a reverse proxy for Tomcat. You can now create and deploy Java based dynamic application on the Tomcat server.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x