• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Install Tomcat 9 on Debian 10 Linux

Written by Admin, Updated On May 27, 2020
apache, debian, tomcat
How to Install Tomcat 9 on Debian 10

Apache Tomcat is an opensource web server that used to implement Java Servlet, JavaServer Pages, Java Expression Language, and Java WebSocket technologies. In this tutorial, you will learn how to Install Apache Tomcat on Debian 10.

Prerequisites#

Before you start to install Apache Tomcat on Debian 10. You must have the non-root user account on your system with sudo privileges.

Install OpenJDK#

Java SE 8 or later required to be installed on the server before we start to install Tomcat 9.0. Run following commands to install OpenJDK package.

sudo apt install default-jdk

Now Java is installed on your system.

Create Tomcat User#

Its recommended that, Tomcat should not run as root user due to security risk. So we will create a new non-root user that will be used to run the Tomcat service.

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

Now you are ready to install Tomcat on your Debian system.

Install Tomcat#

At the time of writing this tutorial, the latest Tomcat version is 9.0.34. Its best practice to check the latest version at Tomcat 9 download page.

Now, navigate to the /tmp directory to download the latest Tomcat binary release:

cd /tmp
wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz

Once download complete extract archive and move to /opt/tomcat directory.

sudo tar -xf apache-tomcat-9.0.34.tar.gz
sudo mv apache-tomcat-9.0.34 /opt/tomcat/

Now, Create a symbolic link by setting latest as name, that points to the Tomcat installation directory. Later, when upgrading Tomcat, you can easily migrate to another Tomcat version just by changing the symlink to point to the desired version.

sudo ln -s /opt/tomcat/apache-tomcat-9.0.34 /opt/tomcat/latest

Set Permissions#

You should change the ownership of the /opt/tomcat directory to previously created tomcat user and group. So that the user can have access to the installation directory. Run the below command:

sudo chown -R tomcat: /opt/tomcat

Make a script inside the bin directory executable:

sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'

Create Systemd Unit File#

Generate a new unit file to run Tomcat as a service. Open your favorite text editor to create a tomcat.service file inside /etc/systemd/system/ directory:

sudo nano /etc/systemd/system/tomcat.service

Now, add the following code into the file.

[Unit]
Description=Tomcat 9.0 servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"

Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

After that reload systemd daemon to notify systemd that a new file created and start the Tomcat service:

sudo systemctl daemon-reload
sudo systemctl start tomcat

Check the status of the Tomcat service using below command:

sudo systemctl status tomcat
● tomcat.service - Tomcat 9.0 servlet container
Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset:
Active: active (running) since Sat 2019-11-09 13:53:51 PST; 5s ago
Process: 5752 ExecStart=/opt/tomcat/latest/bin/startup.sh (code=exited, status
Main PID: 5759 (java)

If everything is okay and no errors then enable the Tomcat service to auto-start at boot:

sudo systemctl enable tomcat

Adjust the Firewall#

If you are running Firewall then update settings then you should open port 8080 to access Tomcat from outside of your local system.

sudo ufw allow 8080/tcp

Configure Tomcat Web Management Interface#

At this point, Tomcat is installed and time to create user and roles to access web interface. The tomcat-users.xml file contains Tomcat users and their roles. Edit tomcat-users.xml file by running following command:

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

We will define a new user in this file with access to the tomcat manager-gui and admin-gui. It strongly recommended to set strong password for users.

<tomcat-users>
<!--
    Comments
-->
   <role rolename="admin-gui"/>
   <role rolename="manager-gui"/>
   <user username="admin" password="admin_password" roles="admin-gui,manager-gui"/>
</tomcat-users>

Save and close the above file.

By default the Tomcat web management interface does not allow access the web interface from a remote IP. It’s a security risk to allow access from a remote IP or from anywhere. If you need to access the web interface from anywhere open the following files and make file content as given below.

Open Manager app context file using below command:

sudo nano /opt/tomcat/latest/webapps/manager/META-INF/context.xml
<Context antiResourceLocking="false" privileged="true" >
<!--
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
</Context>

Run below command to open Host Manager app context file:

sudo nano /opt/tomcat/latest/webapps/host-manager/META-INF/context.xml
<Context antiResourceLocking="false" privileged="true" >
<!--
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
</Context>

Save and close the files and restart the Tomcat server, type:

sudo systemctl restart tomcat

It is also allowed to set a specific IP to access web interface instead of from anywhere. Do not comment the blocks add your public IP to the list. For example, your public IP is 152.18.101.2 then it should look like below:

<Context antiResourceLocking="false" privileged="true" >
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|152.18.101.2" />
</Context>

You can add more IP address with vertical bar separator. Again, Restart the Tomcat service for changes to take effect:

sudo systemctl restart tomcat

Testing Tomcat#

Open your favorite web browser and type: http://your_domain_or_IP_address:8080

It should appear page as given below if your installation is successful.

tomcat-home

Visit http://your_domain_or_IP_address:8080/manager/html to open Tomcat web application manager dashboard. Enter the credentials which we created previously in tomcat-users.xml file.

tomcat-manager

The Virtual Host Manager App is available at http://your_domain_or_IP_address:8080/host-manager/html. By using this app you can manage virtual hosts.

tomcat-host-manager

Conclusion#

You have successfully installed Tomcat 9.0 on your Debian 10 system. To learn more about the Apache Tomcat visit the official Apache Tomcat 9.0 Documentation.

If you face any problem or have suggestions, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install and Configure Redis on CentOS 8
Next Article   How to Install Tomcat 9 on CentOS 8

Related Posts

  • Upgrade Debian 10 Buster to Debian 11 Bullseye

    How to Upgrade Debian 10 Buster to Debian 11 Bullseye?

    July 9, 2022
  • How to Install and Configure Fail2ban on Debian 10

    How to Install and Configure Fail2ban on Debian 10

    June 6, 2021
  • How to Install LibreOffice 7.0 on Debian 10

    How to Install LibreOffice 7.0 on Debian 10

    January 25, 2021

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook