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.
Before you start to install Apache Tomcat on Debian 10. You must have the non-root user account on your system with sudo privileges.
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.
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:
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
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
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
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
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
18.104.22.168 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|22.214.171.124" /> </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
Open your favorite web browser and type:
It should appear page as given below if your installation is successful.
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.
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.
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.