DEV Community

Ahmad Zia
Ahmad Zia

Posted on

Managing Apache Tomcat with systemd on Linux – A DevOps Guide

As a DevOps engineer, automation and service management are critical. While working with Java-based web applications, Apache Tomcat is one of the most widely used tools to serve Java servlets and JSPs. However, starting and stopping Tomcat manually using shell scripts (startup.sh and shutdown.sh) isn’t ideal in a production environment.

In this guide, we’ll walk through setting up Tomcat as a systemd service, allowing us to manage it easily using systemctl — just like any other system service!

✅ What is Tomcat?

Apache Tomcat is an open-source application server developed by the Apache Software Foundation. It's used to host Java-based web applications, similar to how Apache HTTPD serves websites.

🛠️ Setup Steps

1. Install Required Software

First, install Tomcat and Java:

sudo yum install java-17-openjdk -y

Enter fullscreen mode Exit fullscreen mode

Download and extract Tomcat from the official website. You’ll typically extract it using:

tar -xvzf apache-tomcat-9.x.xx.tar.gz

Enter fullscreen mode Exit fullscreen mode

2. Start Tomcat Manually (Initial Test)

To make sure everything works initially, run:

cd apache-tomcat-9.x.xx/bin./startup.sh

Enter fullscreen mode Exit fullscreen mode

Then, open your browser and visit:

http://<your_server_ip>:8080

Enter fullscreen mode Exit fullscreen mode

You should see the Tomcat welcome page.

❌ The Problem

By default, Tomcat requires manual start/stop using shell scripts. That’s not ideal for production or automation. We need to integrate it into our system’s service manager – systemd.

✅ The DevOps Solution: Use systemctl

We’ll create a custom systemd service file for Tomcat, enabling us to manage it using:

sudo systemctl start tomcat
sudo systemctl stop tomcat
sudo systemctl enable tomcat
Enter fullscreen mode Exit fullscreen mode

🧰 Step-by-Step Guide

🔹 Step 1: Create a Non-root Tomcat User

It’s a security best practice to run services as non-root users.

sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
Enter fullscreen mode Exit fullscreen mode

🔹 Step 2: Move Tomcat Files

Assuming you’ve already extracted Tomcat:

sudo mv apache-tomcat-9.x.xx /opt/tomcat
sudo chown -R tomcat: /opt/tomcat

Enter fullscreen mode Exit fullscreen mode

🔹 Step 3: Create systemd Service File

Create a new file:

sudo nano /etc/systemd/system/tomcat.service
Enter fullscreen mode Exit fullscreen mode

Paste the following configuration:


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

[Service]
User=tomcat
Group=tomcat
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
WorkingDirectory=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Enter fullscreen mode Exit fullscreen mode

🔎 Note: Replace JAVA_HOME with the actual path of your installed JDK, if different.

🔹 Step 4: Reload systemd and Start Tomcat

sudo systemctl daemon-reload
sudo systemctl start tomcat

Enter fullscreen mode Exit fullscreen mode

To make Tomcat start automatically at boot:

sudo systemctl enable tomcat
Enter fullscreen mode Exit fullscreen mode

To check the status:

sudo systemctl status tomcat
Enter fullscreen mode Exit fullscreen mode

🎯 Final Test

Visit your server IP on port 8080 again:

http://<your_server_ip>:8080
Enter fullscreen mode Exit fullscreen mode

If you see the Tomcat page — congratulations! You’ve successfully integrated Tomcat into your Linux service system.

Top comments (0)