How to Install and Configure Apache Tomcat 10 on Ubuntu: A Step-by-Step Guide

How to Install and Configure Apache Tomcat 10 on Ubuntu: A Step-by-Step Guide

Installing Apache Tomcat is an essential step for anyone who wants to develop and deploy Java-based web applications. Tomcat is a powerful web container that allows you to run your Java code in a web environment, providing you with a platform to build and deploy your web applications.

To install Tomcat 10 on Ubuntu, you can follow these steps:

Update the package list on your Ubuntu machine

sudo apt-get update

Install Java

sudo apt-get install default-jdk

Verify the installation by checking the Java version:

java -version

Creating a System User

Running Apache Tomcat under the root user is a security risk. We’ll create a new system user and group with home directory /opt/tomcat10 that will run the Apache Tomcat service:

sudo useradd -m -U -d /opt/tomcat10 -s /bin/false tomcat10

Download the latest version of Tomcat 10 from the Apache Tomcat website

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.7/bin/apache-tomcat-10.1.7.tar.gz -P /tmp

Extract the downloaded file to the /opt/tomcat10 directory:

sudo tar -zxvf /tmp/apache-tomcat-10.1.7.tar.gz -C /opt/tomcat10 --strip-components 1

Change the directory ownership to user and group tomcat10:

sudo chown -R tomcat10: /opt/tomcat10

The shell scripts inside the Tomcat’s bin directory must be executable:

chmod +x /opt/tomcat10/bin/*.sh

Create a Systemd Service

sudo nano /etc/systemd/system/tomcat10.service

Paste the following configuration:

[Unit]
Description=Tomcat 10 servlet container
After=network.target

[Service]
Type=forking

User=tomcat10
Group=tomcat10

Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"

Environment="CATALINA_BASE=/opt/tomcat10"
Environment="CATALINA_HOME=/opt/tomcat10"
Environment="CATALINA_PID=/opt/tomcat10/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

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

RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

/etc/systemd/system/tomcat10.service

Modify the JAVA_HOME variable if the path to your Java installation is different.

Reload the systemd daemon

sudo systemctl daemon-reload

Enable and start the Apache Tomcat service:

sudo systemctl enable tomcat10

Verify that Tomcat is running:

sudo systemctl status tomcat10

You should see output indicating that the service is active and running.

You can start, stop and restart Tomcat same as any other systemd service:

service tomcat10 start
service tomcat10 stop
service tomcat10 restart

Configuring Firewall

If your server is protected by a firewall and you want to access Tomcat from the outside of your local network, you need to open port 8080.

sudo ufw allow 8080/tcp

You can now access the Tomcat web interface by navigating to http://localhost:8080 in your web browser.