How to Install Apache Tomcat on Debian 11

how to install apache tomcat on debian 11

In this tutorial, we are going to explain how to install Apache Tomcat on Debian 11 OS.

Apache Tomcat or Tomcat is open-sourced application written in Java used for rendering Java web pages and executing Java web servlets.

Tomcat is developed and maintained by an open community of developers, which is growing on a daily basis. In this tutorial will install Tomcat on Debian 11 and show you how to configure a reverse proxy with Apache as a web server.

For this setup, we will need up to 15 minutes for everything to be configured properly. Let’s get started!

Prerequisites

  • A server with Debian 11 as OS
  • Valid domain pointed to the servers IP address
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

Before we start with the installation, we need to update the system packages to their latest version available:

sudo apt-get update -y && sudo apt-get upgrade -y

Step 2. Install Java

Since Tomcat is written in Java, we need to install it with the following command:

sudo apt install default-jdk -y

To verify the installed version, execute the following command:

java --version

You should get output similar to this:

root@host:~# java --version
openjdk 11.0.18 2023-01-17
OpenJDK Runtime Environment (build 11.0.18+10-post-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Debian-1deb11u1, mixed mode, sharing)

Step 3. Create Tomcat User

To create a Tomcat user, execute the following command:

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

Step 4. Install Tomcat

Before we can install Tomcat and make service files, we need to download it first:

cd /opt

wget https://downloads.apache.org/tomcat/tomcat-10/v10.1.6/bin/apache-tomcat-10.1.6.tar.gz 

tar -xzvf apache-tomcat-11.0.0-M3.tar.gz -C /opt/tomcat --strip-components=1

Set the right permissions to the Tomcat directory:

chown -R tomcat:tomcat /opt/tomcat/

Also, set the execute permissions on scripts in the bin directory of the Tomcat installation:

chmod -R u+x /opt/tomcat/bin

Step 5. Create Tomcat Service File

Create a systemd file:

nano /etc/systemd/system/tomcat.service

Paste the following lines into it:

[Unit]
Description="Tomcat Service"
After=network.target

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

Environment="JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64"
Environment="JAVA_OPTS=-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

Save the file, close it and start the service.

sudo systemctl start tomcat

To check the status of the service, execute the following command:

root@host:/opt/tomcat# systemctl status tomcat
● tomcat.service - "Tomcat Service"
     Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-03-03 16:05:53 CST; 1min 19s ago
   Main PID: 11146 (java)
      Tasks: 29 (limit: 4675)
     Memory: 166.0M
        CPU: 8.531s
     CGroup: /system.slice/tomcat.service
             └─11146 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager=or>

Mar 03 16:05:53 host.test.vps systemd[1]: Starting Tomcat webs servlet container...
Mar 03 16:05:53 host.test.vps startup.sh[11139]: Tomcat started.
Mar 03 16:05:53 host.test.vps systemd[1]: Started Tomcat webs servlet container.

Now, the service is up and running, and you can access Tomcat at http://YourServerIPAdrress:8080.

Step 6. Install Apache

To install the Apache Web server execute the following command:

sudo apt-get install apache2 -y

After installation, start and enable the service:

sudo systemctl start apache2 && sudo systemctl enable apache2

To check the status of the Apache service, execute the following command:

sudo systemctl status apache2

You should get the following output:

root@host:/opt/tomcat# sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-03-02 17:21:52 CST; 23h ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 420 (apache2)
      Tasks: 9 (limit: 4675)
     Memory: 27.9M
        CPU: 4.532s
     CGroup: /system.slice/apache2.service
             ├─ 420 /usr/sbin/apache2 -k start
             ├─ 428 /usr/sbin/apache2 -k start
             ├─ 429 /usr/sbin/apache2 -k start
             ├─ 430 /usr/sbin/apache2 -k start
             ├─ 431 /usr/sbin/apache2 -k start
             ├─ 432 /usr/sbin/apache2 -k start
             ├─ 598 /usr/sbin/apache2 -k start
             ├─3916 /usr/sbin/apache2 -k start
             └─5728 /usr/sbin/apache2 -k start

Mar 02 17:21:52 host.test.vps systemd[1]: Starting The Apache HTTP Server...
Mar 02 17:21:52 host.test.vps systemd[1]: Started The Apache HTTP Server.

Step 7. Create Reverse Proxy

We need to create an Apache configuration file and set up the reverse proxy so you can access it via the domain name.

Go into the Apache directory and create a configuration file for the Tomcat.

cd /etc/apache2/sites-available/

touch tomcat.conf

Open the file, paste the following lines of code, save the file and close it.

<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /opt/tomcat

ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>

ProxyPass / http://YourServerIP:8080/
ProxyPassReverse / http://YourServerIP:8080/
<Location />
Order allow,deny
Allow from all
</Location>


ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Enable the Apache configuration for Tomcat and rewrite the module.

a2dissite 000-default.conf

sudo a2enmod rewrite

sudo a2ensite tomact.conf

sudo a2enmod proxy

sudo a2enmod proxy_http

Check the syntax:

apachectl -t

You should receive the following output:

root@vps:~# apachectl -t
Syntax OK

If the syntax is OK, restartd the Apache service.

systemctl restart apache2

Once the Apache service is restarted, you can access Tomcat via domain at http://yourdomain.com

Congratulations! You successfully installed Tomcat on Debian 11 with Apache as a reverse proxy. If you find it difficult to complete these steps, feel free to contact us anytime you want. We are available 24/7.

If you liked this post on how to install Apache Tomcat on Debian 11, please share it with your friends on social networks or simply leave a reply below. Thanks.

Leave a Comment