How to Install Apache Tomcat on Debian 9

Install Apache Tomcat on Debian 9

In this tutorial we are going to learn how to install Apache Tomcat on Debian 9. We will also install and configure its prerequisites. Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, published by the Apache Software Foundation.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Debian 9 Stretch minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Apache Tomcat on Debian 9

Step 1. First, before we install packages, it’s a good idea to update repository and software packages. Run the following command on your Debian 9 OS:

sudo apt update
sudo apt upgrade

Step 2. Installing Java.

Apache Tomcat is written in Java application, so Java runtime environment needs to be installed:

sudo apt install default-jdk

Once Java is installed, you can verify the Java version by using the following command:

java -version

Step 3. Create Tomcat Group and User.

Tomcat should not be run as root user. So, create a normal user for running the Tomcat service:

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

Step 4. Installing the latest Apache Tomcat on Debian 9.

First, download the latest version of the Apache Tomcat from the website:

cd /tmp
wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.zip

Then extract the Tomcat archive and move it to your desired (/opt/tomcat) directory:

unzip apache-tomcat-*.zip
sudo mv apache-tomcat-*/ /opt/tomcat

Next, we will create a symbolic link named latest which will point to the Tomcat installation directory:

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

Now change the proper ownership using following command:

sudo chown -R tomcat: /opt/tomcat
sudo chmod +x /opt/tomcat/latest/bin/*.sh

Step 5. Create Systemd Unit for Tomcat.

Now you will need to create a systemd service file to manage the Tomcat process so you can run Tomcat as a service:

nano /etc/systemd/system/tomcat.service

Add the below information to Tomcat systemd service file:

[Unit]
Description=Tomcat 8.5 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

Notify systemd that we created a new unit file and start the Tomcat service by executing:

systemctl daemon-reload
systemctl start tomcat 
systemctl enable tomcat

Step 6. Configure Firewall Apache Tomcat.

In order to test Apache Tomcat in a web browser, you need to modify the firewall rules:

sudo ufw allow 8080

Step 7. Configure Tomcat Web Management Interface.

In order to use the manager webapp that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the tomcat-users.xml file:

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

Place the following two lines just above the last line:

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

Restart Apache Tomcat to put your modifications into effect:

systemctl restart tomcat

Step 8. Accesing Apache Tomcat.

The Apache Tomcat is now accessible on the default port, which is 8080. The admin UI should be accessible at:

http://<your_domain_or_IP_address>:8080

Congratulation’s! You have successfully install and configured Tomcat on your Debian Stretch. Thanks for using this tutorial installing Apache Tomcat on Debian 9 (Stretch) system.