How to install Tomcat 9 on Debian 9 Stretch Linux

Objective

The objective is to install Tomcat 9 on Debian 9 Stretch Linux.

Operating System and Software Versions

  • Operating System: – Debian 9 Stretch
  • Software: – Oracle Java JDK 1.8.0_131, Tomcat 9.0.0.M21

Requirements

Privileged access to your Debian system is required in order to perform Tomcat and Oracle Java installation.

Difficulty

MEDIUM

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Instructions

Oracle Java JDK Installation

Tomcat’s prerequisite is Java JDK. Whether your install OpenJDK or Oracle’s JDK is up to you. This guide uses Oracle’s JDK, so let’s get java first out of our way. List all available Oracle JDK downloads using curl command. If curl is not yet available on your system you can install by # apt install curl:

$ curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html | grep "otn-pub" | cut -d \" -f12

Copy the link for 64-bit Linux tarball and download it. Your download link will be different so update the below command with the download URL retrieved by previous command: Example:



$ curl -LOb "oraclelicense=a" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz

If all went well you should have Oracle Java JDK inside your current directory:

$ ls
jdk-8u131-linux-x64.tar.gz

Next step is to decompress the above tarball:

# mkdir /opt/java-jdk
# tar -C /opt/java-jdk -zxf jdk-8u131-linux-x64.tar.gz

Set Oracle Java JDK as default on your system. Update the below command where necessary:

# update-alternatives --install /usr/bin/java java /opt/java-jdk/jdk1.8.0_131/bin/java 1
# update-alternatives --install /usr/bin/javac javac /opt/java-jdk/jdk1.8.0_131/bin/javac 1

Oracle Java JDK should now be installed as default. Query java version to see if it is installed correctly:

# java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

Install Tomcat

At this stage java is installed and to continue with our Tomcat installation we need download Tomcat source tarball from the official tomcat website http://tomcat.apache.org/download-90.cgi. Download the latest Tomcat version using your browser or simply used wget or curl. Example:

$ wget http://apache.uberglobalmirror.com/tomcat/tomcat-9/v9.0.0.M21/bin/apache-tomcat-9.0.0.M21.tar.gz

The Tomcat tarball should be now available within your current working directory:

$ ls 
apache-tomcat-9.0.0.M21.tar.gz  jdk-8u131-linux-x64.tar.gz

Next, create a dedicated user tomcat to run tomcat server:

# useradd -rs /bin/false tomcat

Extract Tomcat tarball:

# mkdir /opt/tomcat
# tar -C /opt/tomcat/ -zxf apache-tomcat-9.0.0.M21.tar.gz

The following step is optional, but highly recommended as it makes things much easier when updating tomcat with new version. Create a symbolic link pointing to latest tomcat directory:

# ln -s /opt/tomcat/apache-tomcat-9.0.0.M21/ /opt/tomcat/tomcat-latest

Make our previously created user tomcat an owner of the entire directories:

# chown -R tomcat.tomcat /opt/tomcat/tomcat-latest /opt/tomcat/apache-tomcat-9.0.0.M21


Note, when upgrading tomcat, simply unlink the current symbolic link and recreate new as shown in previous steps.

Next, create systemd startup script /etc/systemd/system/tomcat.service pointing to our new /opt/java-jdk and /opt/tomcat/tomcat-latest.

Below you can find the content of new /etc/systemd/system/tomcat.service systemd file:

[Unit]
Description=Tomcat9
After=network.target

[Service]
Type=forking
User=tomcat
Group=tomcat

Environment=CATALINA_PID=/opt/tomcat/tomcat-latest/tomcat8.pid
Environment=TOMCAT_JAVA_HOME=/usr/bin/java
Environment=CATALINA_HOME=/opt/tomcat/tomcat-latest
Environment=CATALINA_BASE=/opt/tomcat/tomcat-latest
Environment=CATALINA_OPTS=
Environment="JAVA_OPTS=-Dfile.encoding=UTF-8 -Dnet.sf.ehcache.skipUpdateCheck=true -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+UseParNewGC -XX:MaxPermSize=128m -Xms512m -Xmx512m"

ExecStart=/opt/tomcat/tomcat-latest/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

[Install]
WantedBy=multi-user.target

Lastly, reload systemd, start and enable tomcat to start after reboot:

# systemctl daemon-reload
# systemctl start tomcat
# systemctl enable tomcat

Access Tomcat

The last step is to confirm that Tomcat is up and running. Tomcat should be now listening on port 8080:

# ss -ant | grep 8080
LISTEN     0      100         :::8080

To access tomcat navigate your browser to an IP address of you new Tomcat server. Example http://10.1.1.125:8080/.

tomcat 9 on debian 9 stretch linux installation