How To

Install Tomcat 9 on Ubuntu 18.04: Systemd Service Setup Guide

Apache Tomcat is an open-source Java application server that implements the Java Servlet, JavaServer Pages, and WebSocket specifications. It is one of the most widely used servers for running Java-based web applications in production.

This guide shows you how to install Tomcat 9 on Ubuntu 18.04, configure it as a systemd service, and set up the web management interface.

Prerequisite: You need sudo access on your Ubuntu server.

Install OpenJDK and Create the Tomcat User

Tomcat requires Java to run. Install the default OpenJDK package:

bashsudo apt updatesudo apt install default-jdk

Running Tomcat as root is a security risk. Create a dedicated system user with a home directory at /opt/tomcat:

bashsudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat

The -s /bin/false flag prevents this user from logging in to the system directly.

Install Tomcat 9 on Ubuntu

Check the Tomcat 9 downloads page to confirm the latest version before running these commands.

Download the archive to the /tmp directory:

bashwget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-9.0.27.tar.gz -P /tmp

Extract it to the /opt/tomcat directory:

bashsudo tar xf /tmp/apache-tomcat-9*.tar.gz -C /opt/tomcat

Create a symlink named latest that points to the installed version. This makes future upgrades straightforward — you only update the symlink instead of editing the service file:

bashsudo ln -s /opt/tomcat/apache-tomcat-9.0.27 /opt/tomcat/latest

Set the correct ownership and make the startup scripts executable:

bashsudo chown -RH tomcat: /opt/tomcat/latestsudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'

Create the Systemd Service

Create a service file so Tomcat starts automatically with the system:

bashsudo nano /etc/systemd/system/tomcat.service

Paste the following configuration:

ini[Unit]Description=Tomcat 9 servlet containerAfter=network.target[Service]Type=forkingUser=tomcatGroup=tomcatEnvironment="JAVA_HOME=/usr/lib/jvm/default-java"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.shExecStop=/opt/tomcat/latest/bin/shutdown.sh[Install]WantedBy=multi-user.target

Reload systemd, start the Tomcat service, and enable it at boot:

bashsudo systemctl daemon-reloadsudo systemctl start tomcatsudo systemctl enable tomcat

Verify that Tomcat is running:

bashsudo systemctl status tomcat

If the output shows active (running), the service started successfully.

Open the Firewall Port and Test

If UFW is active on your server, allow traffic on port 8080:

bashsudo ufw allow 8080/tcp

In production environments, it is best practice to run Tomcat behind a reverse proxy like Nginx and restrict direct access to port 8080 from external traffic.

Open http://your_server_ip:8080 in your browser. You should see the Tomcat welcome page. The Manager App is available at /manager/html and the Host Manager at /host-manager/html.

Configure the Web Management Interface

To log in to the manager apps, create an admin user in the tomcat-users.xml file:

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

Add these lines before the closing </tomcat-users> tag. Replace the username and password with something secure:

xml&lt;role rolename="admin-gui"/&gt;&lt;role rolename="manager-gui"/&gt;&lt;user username="admin" password="admin_password" roles="admin-gui,manager-gui"/&gt;

By default, the Manager and Host Manager apps are restricted to connections from localhost only. To allow remote access, open the context.xml file for each app and comment out the RemoteAddrValve block. You can also whitelist a specific IP address instead of removing the restriction entirely.

Restart Tomcat after any configuration change:

bashsudo systemctl restart tomcat

Tomcat 9 is now running as a managed service on your Ubuntu 18.04 server. Use the Manager dashboard to deploy, start, and stop your Java web applications. Leave a comment below if you run into any issues during setup.

Cyber Defence

Recent Posts

Install Nextcloud on Ubuntu 18.04 with Apache and MySQL

Nextcloud is a free, open-source, self-hosted file sharing and collaboration platform. It gives you a private…

10 hours ago

Install Plex Media Server on Ubuntu 18.04: Step-by-Step Setup

Plex is a self-hosted streaming media server that organizes your video, music, and photo collections into…

10 hours ago

Install Visual Studio Code on Ubuntu 18.04: Microsoft Repo Setup

Visual Studio Code is a free, open-source, cross-platform code editor from Microsoft. It ships with built-in…

10 hours ago

Install Odoo 11 on Ubuntu 16.04 with Git and Python Virtualenv

Odoo is one of the most widely used open-source ERP platforms in the world. It handles…

10 hours ago

Secure Nginx with Let’s Encrypt on Ubuntu 16.04: SSL Setup Guide

Let's Encrypt is a free, automated, and open certificate authority run by the Internet Security Research…

10 hours ago

Install Nginx on Ubuntu 16.04: UFW, PPA, and Config Structure

Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server and reverse proxy. It handles…

2 days ago