Install Apache Tomcat on Debian 1

How to Install Apache Tomcat 10 on Debian 10

Apache Tomcat is an open-source web application server used by Java applications for hosting purposes. It is very easy to install Apache Tomcat on Debian systems. Apache Tomcat consists of Java Servlet, JavaServer Pages (JSP), and Java EL.

Java-based applications require Apache Tomcat to host Java applications and run Java codes. Tomcat consists of rich features and functionality, including an extra level of security, lightweight application, flexibility, well-documented guidelines, and maturity. 

This article will cover how to install and configure Apache Tomcat on Debian and run your Java-based application. So, let’s get started.

Install Apache Tomcat 10 on Debian 10

Before proceeding with the Tomcat installation, make sure that your Debian 10 machine is configured with sudo privilege or root access. The machine should also have a domain name if it is a server machine. 

1. Update Packages

The first step is to update the system packages to the latest version using this command:

sudo apt-get update -y

Wait for all the packages to be updated before proceeding with the Java installation.

2. Install Java

Since Apache Tomcat is a Java-based application, you will install Java JRE first. Execute the following command to install Java JRE:

sudo apt install default-jre

Secondly, install the default Java JDK by running this command:

sudo apt install default-jdk -y
Install Apache Tomcat on Debian

The installation will take some time. Wait for it to complete. After the installation, you can verify the Java version. For example:

java --version
Install Apache Tomcat on Debian

If you see a similar output, you have successfully installed Java JDK.

3. Install Apache Tomcat on Debian

To install Apache Tomcat 10, you will add a user to run the Apache Tomcat. We will use the useradd command to add the user. 

For example:

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

Secondly, you will download the Apache Tomcat 10 using the wget command. This package does not exist in APT manager. Hence, we will use the wget command.

wget https://mirrors.estointernet.in/apache/tomcat/tomcat-10/v10.0.7/bin/apache-tomcat-10.0.7.tar.gz
Install Apache Tomcat on Debian

Once the download has been completed, you will extract the downloaded file using the tar command. Extract the downloaded archive file to the /opt/tomcat directory folder. You can also extract to any other folder, but make sure to use that directory name. For example:

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

Next, set the ownership to the extracted directory using the chown command. For example:

sudo chown -R tomcat:tomcat /opt/tomcat/

4. Create Tomcat Admin User

In this step, you will create an admin user. Next, you will allow that user to access the Tomcat Admin interface. To perform this step, edit the tomcat-users.xml file using the nano or vi editor. For example:

<!-- manager section user role -- >
<role rolename="manager-gui" />
<user username="manager" password="pasword" roles="manager-gui" />
<!-- admin section user role -- >
<role rolename="admin-gui" />
<user username="admin" password="password" roles="manager-gui,admin-gui" />
Install Apache Tomcat on Debian

Save the file by pressing “Ctrl + X”. After that, type “y”, and hit the “Enter” key.

5. Allow Remote Access

By default, you can access Apache Tomcat from local systems only. To configure Tomcat accessibility from remote systems, you will edit the context.xml file. Firstly, open the file using the editor. 

For example: 

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

Modify the “allow” tag in the file.

Remove the following lines from the file:

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

Save the file by pressing “Ctrl + X”. After that, type “y”, and hit the “Enter” key. Now, proceed to the next step. 

6. Create a File to Manage Tomcat Service

Now that you have allowed the remote access, the next step is to create a systemd unit file. This file will be responsible for managing the Tomcat services. Firstly, create the file using the nano command.

For example: 

nano /etc/systemd/system/tomcat.service

Secondly, add the following lines in the tomcat.service file.

[Unit]
Description=Tomcat
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
Create a File to Manage Tomcat Service

Now, save the file and exit from the editor. Thirdly, restart the daemon to implement the changes you have recently made. 

sudo systemctl daemon-reload

Next, start the Tomcat service and enable it using the systemctl command. For example:

systemctl start tomcat
systemctl enable tomcat

To check the status of the Tomcat service, use the systemctl command. For example:

systemctl status tomcat

Currently, the Tomcat service is listening on port 8080. To confirm this, use the ss command. For example 

ss -antpl | grep 8080

7. Configure Nginx for Tomcat

Since we have allowed remote access to Tomcat, we will use Nginx as a reverse proxy to access the Tomcat application.

Firstly, install the Nginx web server using this command:

sudo apt-get install nginx -y
Configure Nginx for Tomcat

Secondly, you will create a virtual host configuration file using the editor. For example:

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

Thirdly, add the following lines in the tomcat.conf file:

server {
  listen 80;
  server_name tomcat.example.com;
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/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 the file and exit the editor. To ensure that you have successfully made the changes, run the following command: 

sudo nginx -t

You will get a similar output:

Configure Nginx for Tomcat

Next, restart the Nginx to implement the changes using the systemctl command.

sudo systemctl restart nginx

To check the status of Nginx, run the systemctl command. For example:

sudo systemctl status nginx

Now, enable the Nginx service: 

sudo systemctl enable tomcat

Lastly, allow the ufw firewall.

sudo ufw allow 8080/tcp

8. Access the Apache Tomcat Application

To access the Apache Tomcat, open your web browser and type the Tomcat application URL. For example: 

http://tomcat.<your_URL>.com

Or, you can access it locally by typing “http://username.local:8080” and you will be directed to the main page of Apache Tomcat on your localhost server.

Access the Apache Tomcat Application

From this portal, you can check the Server Status, Manage App and access the Host Manager. All of these actions would require your admin username and password. 

Congratulations! you have successfully installed Apache Tomcat 10 on Debian 10 server. You can now easily deploy your Java-based applications including Java Servlet, JavaServer Pages (JSP), and Java EL using Tomcat. 

We hope this step-by-step guide will assist you in installing Apache Tomcat 10 on Debian 10 successfully. 

If this guide helped you, please share it.

Leave a Reply
Related Posts