Quick install Tomcat on Ubuntu

Let's grab the default Tomcat and Java APT packages on Ubuntu.

Check what version of Tomcat is available:

$ apt-cache search tomcat

From the output of running apt-cache on Ubuntu 20.04 LTS, we find that tomcat9 is available. Let's install it.

$ sudo su -
$ apt install tomcat9

By default Tomcat runs on port 8080. Let's check it:

$ curl http://localhost:8080/
<h1>It works!</h1>

Okey so we are done - that didn't take many seconds!

Java was automatically installed as a dependency of tomcat9 and Tomcat was configured and started as a Systemd service.

Deploying a WAR file

Hotdeploying a web application to Tomcat is easy. Just drop your WAR file in the hotdeployment folder.

But where is the hotdeployment folder and what user or group should we make the WAR file readable to?

Let's check with Systemd:

$ systemctl status tomcat9

tomcat9.server - Apache Tomcat 9 Web Application Server Loaded: loaded (/lib/systemd/system/tomcat9.service; enabled; vendor preset: enabled) Active: active (running) since Thur 2020-07-30 11:57:46 UTC; 1min 0s ago Docs:
Main PID: 3796 (java) Tasks: 29 (limit 1075) Memory: 70.3M CGroups: /system.slice/tomcat9.service 3796 /usr/lib/jvm/default-java/bin/java ...

From the output we can see that the Systemd unit file for tomcat9 is stored under /lib/systemd/system/tomcat9.service.

$ less /lib/systemd/system/tomcat9.service

# Configuration Environment="CATALINA_BASE=/var/lib/tomcat9" ... # Security User=tomcat Group=tomcat ...

According to the unit file, the tomcat9 service is running under the tomcat user and group, and CATALINA_BASE is set to /var/lib/tomcat9.

The hotdeployment folder is therefore /var/lib/tomcat9/webapps.

Let's remove the default root web application to make room for our own.

$ cd /var/lib/tomcat9/webapps
$ rm -rf ROOT/

Assuming you have copied your own ROOT.war to the hotdeployment folder, you would then set ownership of the file to root:tomcat and give the group read permission.

$ chown root:tomcat ROOT.war
$ chmode g+r,go-wx ROOT.war

Tomcat should start to hotdeploy the file within 5 seconds of getting read access.