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

xargs Command in Linux: Build and Execute Commands from Input

The xargs command in Linux reads items from standard input and passes them as arguments to another…

1 day ago

locate Command in Linux: Find Files Fast with a Database Search

The locate command in Linux searches for files and directories by name. It queries a pre-built database…

1 day ago

Listing Linux Services with systemctl: A Complete Guide

Most modern Linux distributions use systemd as the default service manager. Knowing how to list…

1 day ago

How to Truncate Files in Linux: Empty Files Without Deleting

Truncating a file in Linux means removing its contents while leaving the file itself in…

1 day ago

ss Command in Linux: Display Socket Statistics and Connections

The ss command in Linux lists open sockets and active network connections. It replaced the deprecated netstat command and…

2 days ago

ftp Command in Linux: Connect to Servers and Transfer Files

The ftp command in Linux connects to a remote FTP server and transfers files. FTP transmits everything…

2 days ago