Installing Java 8 and Tomcat 8 on Debian Jessie or Raspbian or RedHat

Apache Tomcat is a Servlet/JSP container and version 8.0 implements the Servlet 3.1 and JavaServer Pages 2.3 specifications. Please note that Apache Tomcat 8.0 requires a Java Standard Edition Runtime Environment (JRE) version 7 or later. So we start with installing a recent version of Oracle’s JRE.

Install Oracle JRE 8 on Debian Linux (or Raspbian)

To install Oracle’s Java Runtime with apt-get, we first need to entend the list of apt-get’s sources. Once that is done, an java-installer will actually install the Java SE Runtime Environment. Here are the steps to follow:

$ su root
# echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" > /etc/apt/sources.list.d/webupd8team-java.list
# echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" >> /etc/apt/sources.list.d/webupd8team-java.list
# apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886
# apt-get update
# apt-get install oracle-java8-installer
# java -version
# exit

As I’m writing this, the newly installed Java Runtime reports its version as 1.8.0_101:

java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) Client VM (build 25.101-b13, mixed mode)

With Java now installed we move on, installing Tomcat. However, it may be beneficial to have an dedicated user for Tomcat.
BTW, Java got installed into this location: /usr/lib/jvm/java-8-oracle

sudo adduser \
  --system \
  --shell /bin/bash \
  --gecos 'Tomcat Java Servlet and JSP engine' \
  --group \
  --disabled-password \
  --home /home/tomcat \
  tomcat

.. which should result in something like this:

Adding system user 'tomcat' (UID 108) ...
Adding new group 'tomcat' (GID 113) ...
Adding new user 'tomcat' (UID 108) with group 'tomcat' ...
Creating home directory '/home/tomcat' ...

Installing Tomcat 8.5.x

$ mkdir -p ~/tmp
$ cd ~/tmp
$ wget http://www.us.apache.org/dist/tomcat/tomcat-8/v8.5.5/bin/apache-tomcat-8.5.5.tar.gz
$ tar xvzf ./apache-tomcat-8.5.5.tar.gz
$ rm ./apache-tomcat-8.5.5.tar.gz
$ sudo mv ./apache-tomcat-8.5.5 /usr/share

To make it easy to replace this release with future releases, we are going to create a symbolic link that we are going to use when referring to Tomcat (after removing the old link, you might have from installing a previous version):

$ sudo rm -f /usr/share/tomcat
$ sudo ln -s /usr/share/apache-tomcat-8.5.5 /usr/share/tomcat

Since we created a tomcat user, he should also own all these files in

$ sudo chown -R tomcat:tomcat /usr/share/tomcat/*
$ sudo chmod +x /usr/share/tomcat/bin/*.sh

If Tomcat’s default HTTP port (8080) is already in use, you need to edit the server.xml configuration file, e.g.
edit /usr/share/tomcat/conf/server.xml and replace 8080 with 8000

Starting Tomcat

$ sudo /bin/su - tomcat -c /usr/share/tomcat/bin/startup.sh

Using CATALINA_BASE:   /usr/share/tomcat
Using CATALINA_HOME:   /usr/share/tomcat
Using CATALINA_TMPDIR: /usr/share/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar

Stopping Tomcat

$sudo /bin/su - tomcat -c /usr/share/tomcat/bin/shutdown.sh

Using CATALINA_BASE:   /usr/share/tomcat
Using CATALINA_HOME:   /usr/share/tomcat
Using CATALINA_TMPDIR: /usr/share/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar

Staring Tomcat when the server boots

To start Tomcat automatically, every time the server re-boots, save this script in /etc/init.d/tomcat

#!/bin/bash

### BEGIN INIT INFO
# Provides:        tomcat
# Required-Start:  $network
# Required-Stop:   $network
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start/Stop Tomcat server
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

start() {
 /bin/su - tomcat -c /usr/share/tomcat/bin/startup.sh
}

stop() {
 /bin/su - tomcat -c /usr/share/tomcat/bin/shutdown.sh 
}

case $1 in
  start|stop) $1;;
  restart) stop; start;;
  *) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac

Now change the permissions of the newly created file and add the correct symlinks automatically:

chmod 755 /etc/init.d/tomcat
update-rc.d tomcat defaults

Long Startup Time
Tomcat relies heavily relies on the SecureRandom class to provide random values, for instance to generate session ids. During startup, if entropy source that is used to initialize SecureRandom is short of entropy, this can lead to very long delays, which can be confirmed in the logs/catalina.out log file. Adding JAVA_OPTS="-Djava.security.egd=file:/dev/urandom" at the beginning of the bin/catalina.sh file, will significatinly speedup the startup time.

RedHat

While not my preferred Linux distribution, RedHat and CentOS are the standard in Corporate America.  So here are a few comments on how to install Java and Tomcat on RedHat:

Installing Java 8

sudo su
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.rpm"
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jre-8u60-linux-x64.rpm"
rpm -Uvh jdk-8u60-linux-x64.rpm
rpm -Uvh jre-8u60-linux-x64.rpm

This will install Oracle’s Java 8 or more specifically Java(TM) SE Runtime Environment (build 1.8.0_51-b16) and Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode) here: /usr/java/jdk1.8.0_51/

Later, if you decide to run Tomcat on port 80, you will also need to execute this:

setcap cap_net_bind_service+ep /usr/java/jdk1.8.0_51/jre/bin/java

and cat this content

/usr/java/jdk1.8.0_51/jre/lib/amd64/jli

into /etc/ld.so.conf.d/java.conf

Creating a Tomcat User account

useradd -r -m tomcat
passwd -l tomcat

 

Creating a service script

/etc/systemd/system/tomcat.service

# Systemd unit file for tomcat
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/java/latest/jre
Environment=CATALINA_PID=/usr/share/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/share/tomcat
Environment=CATALINA_BASE=/usr/share/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/usr/share/tomcat/bin/startup.sh
ExecStop=/usr/share/tomcat/bin/shutdown.sh
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target

 

  • sudo systemctl daemon-reload
  • sudo systemctl enable tomcat
  • sudo systemctl start tomcat

 

4 Replies to “Installing Java 8 and Tomcat 8 on Debian Jessie or Raspbian or RedHat”

  1. Nicely done! Thanks

  2. This comands not work for me
    $ sudo chown -R tomcat:tomcat /usr/share/tomcat/*
    $ sudo chmod +x /usr/share/tomcat/bin/*.sh
    No such file or directory error on both comands…

  3. This was exactly what I needed!
    Great job, thank you!

  4. Great article -thanks very much.

Leave a Reply