Install Tomcat on Ubuntu 16.04

Published on: Fri, Mar 23, 2018 at 1:04 pm EST

This guide explains how to install Apache Tomcat on an Ubuntu 16.04 server.

Prerequisites

  • Deploy a new Ubuntu 16.04 server instance.
  • Create a non-root sudo user.
  • Log into the machine via SSH as the user you created.

Update your system

Make sure that you have the latest stable version of Ubuntu 16.04.

sudo apt-get update 

Install Java.

sudo apt-get install default-jdk 

Update the JAVA_HOME variable

Find the directory for JAVA_HOME.

update-alternatives --config java 

Copy the directory, and then type the following.

nano /etc/environment 

This will open up the file that contains environment variables.

Add JAVA_HOME.

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java"

The path in quotes is the path that you are using.

Save and close the file.

Reload the environment variables so that they take effect.

source /etc/environment 

Check to make sure that it worked:

echo $JAVA_HOME 

You will see the path you've entered.

Create a user to run the Tomcat web server

We will call this user 'tomcat', but you can use any name for this user.

groupadd tomcat
useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Make sure that you know the version of Tomcat that you want to install.

For this tutorial, we will use version 8.5.24, and the download link is as follows.

http://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz

Remember to choose the download with a .gz extension.

Make a directory for the Tomcat files.

mkdir /opt/tomcat 

Download Tomcat.

cd 
wget your-tomcat-link 

Unzip the files into the tomcat folder.

tar xvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1

You can press the "Tab" key after typing apache, since you only have one download in your folder.

Make sure that the folders have the correct permissions.

cd /opt/
sudo chown -R tomcat tomcat/

Display the JAVA_HOME path again, so that you can copy it.

nano /etc/systemd/system/tomcat.service 

Configure Tomcat

Copy and paste the settings below into the tomcat.service file you have just created. Edit the JAVA_HOME path to the one that you copied. After this, save and close the file.

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Reload the SystemD daemon so that the service settings that were added are included.

sudo systemctl daemon-reload

Start the Tomcat service.

sudo systemctl start tomcat 

Check the status of your Tomcat server.

sudo systemctl status tomcat 

Browse to http://your_ip_address:8080

You will see the Tomcat server landing page.