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

Change Timezone on Ubuntu 18.04: Command Line and GUI Methods

Setting the correct timezone on your Ubuntu machine is more important than it sounds. Your…

6 minutes ago

Install PrestaShop on Ubuntu 18.04 with Nginx and MySQL

PrestaShop is a free, open-source e-commerce platform built with PHP and MySQL. It comes with a…

21 minutes ago

Set Up SSH Keys on Ubuntu 18.04: Passwordless Login Guide

SSH keys give you a more secure and convenient way to connect to remote servers. Instead…

26 minutes ago

Install FFmpeg on Ubuntu 18.04: apt, Snap, and Usage Examples

FFmpeg is a free, open-source command-line tool for working with multimedia files. It can convert video…

29 minutes ago

Set Up Nginx Server Blocks on Ubuntu 18.04: Host Multiple Sites

Nginx server blocks let you run more than one website on a single server. Each block…

23 hours ago

Install Tor Browser on Ubuntu 18.04: Anonymous Browsing Guide

Tor Browser is a modified version of Firefox that routes all your web traffic through the Tor…

23 hours ago