BLOG POSTS
Install Tomcat on Linux – Step-by-Step Guide

Install Tomcat on Linux – Step-by-Step Guide

Installing Apache Tomcat on Linux is a fundamental skill for any developer or sysadmin working with Java web applications. Tomcat serves as the backbone for countless enterprise applications, from small internal tools to massive e-commerce platforms handling millions of requests daily. This guide walks you through the complete installation process, covering everything from initial setup to production-ready configuration, plus troubleshooting the weird issues that always seem to pop up at 3 AM.

Understanding Apache Tomcat Architecture

Tomcat operates as a servlet container and web server, implementing the Java Servlet and JavaServer Pages (JSP) specifications. Unlike full-featured application servers like WildFly or WebLogic, Tomcat focuses specifically on web components, making it lightweight and perfect for most web application deployments.

The core components include:

  • Catalina – The servlet container that handles servlet lifecycle
  • Coyote – HTTP connector managing network traffic
  • Jasper – JSP engine for compiling JSP pages
  • Cluster – Session replication and load balancing support

Prerequisites and System Requirements

Before diving into installation, ensure your Linux system meets these requirements:

Component Minimum Requirement Recommended
RAM 512 MB 2 GB+
CPU 1 core 2+ cores
Disk Space 50 MB 1 GB+ (including logs)
Java Version OpenJDK 8 OpenJDK 11 or 17

For production deployments requiring more resources, consider upgrading to a dedicated server to handle enterprise workloads effectively.

Step-by-Step Installation Guide

Step 1: Install Java Development Kit

Tomcat requires Java to run. Install OpenJDK using your distribution’s package manager:

# Ubuntu/Debian
sudo apt update
sudo apt install openjdk-11-jdk

# CentOS/RHEL/Rocky Linux
sudo dnf install java-11-openjdk-devel

# Verify installation
java -version
javac -version

Set the JAVA_HOME environment variable:

# Find Java installation path
sudo find /usr -name "java" -type f 2>/dev/null | grep bin

# Add to ~/.bashrc or /etc/environment
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin

# Reload environment
source ~/.bashrc

Step 2: Create Tomcat User

Running Tomcat as root is a security nightmare. Create a dedicated user:

# Create tomcat user and group
sudo groupadd tomcat
sudo useradd -M -s /bin/nologin -g tomcat -d /opt/tomcat tomcat

Step 3: Download and Install Tomcat

Navigate to the official Apache Tomcat download page for the latest version. At the time of writing, Tomcat 10.1.x is the stable release:

# Download Tomcat (replace with latest version)
cd /tmp
wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.1.15/bin/apache-tomcat-10.1.15.tar.gz

# Extract to /opt/tomcat
sudo mkdir -p /opt/tomcat
sudo tar xzf apache-tomcat-10.1.15.tar.gz -C /opt/tomcat --strip-components=1

# Set proper ownership
sudo chown -R tomcat: /opt/tomcat
sudo sh -c 'chmod +x /opt/tomcat/bin/*.sh'

Step 4: Configure Tomcat Users

Edit the tomcat-users.xml file to create admin accounts:

sudo nano /opt/tomcat/conf/tomcat-users.xml

Add these roles and users before the closing </tomcat-users> tag:

<role rolename="manager-gui" />
<role rolename="manager-script" />
<role rolename="manager-jmx" />
<role rolename="manager-status" />
<role rolename="admin-gui" />
<role rolename="admin-script" />

<user username="admin" password="StrongPassword123!" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script" />
<user username="deployer" password="DeployPass456!" roles="manager-script" />
<user username="tomcat" password="TomcatPass789!" roles="manager-gui" />

Step 5: Create Systemd Service

Create a systemd service file for easy management:

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

Add this configuration:

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

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

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

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable tomcat
sudo systemctl start tomcat
sudo systemctl status tomcat

Performance Tuning and Configuration

Default Tomcat settings work for development, but production requires optimization. Here’s a comparison of common configuration scenarios:

Scenario Heap Size (Xmx) Max Threads Connection Timeout Use Case
Development 512MB 200 20000ms Local testing
Small Production 2GB 400 10000ms < 1000 concurrent users
High Traffic 8GB+ 800-1000 5000ms Enterprise applications

Modify /opt/tomcat/conf/server.xml for production tuning:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="10000"
           redirectPort="8443"
           maxThreads="400"
           minSpareThreads="25"
           maxSpareThreads="75"
           enableLookups="false"
           acceptCount="100"
           compression="on"
           compressionMinSize="2048"
           compressableMimeType="text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json" />

Security Hardening

Default Tomcat installations are surprisingly insecure. Implement these hardening measures:

Remove Default Applications

# Remove sample applications that expose security information
sudo rm -rf /opt/tomcat/webapps/docs
sudo rm -rf /opt/tomcat/webapps/examples
sudo rm -rf /opt/tomcat/webapps/host-manager
sudo rm -rf /opt/tomcat/webapps/manager

Hide Server Information

Edit /opt/tomcat/conf/server.xml and modify the Connector:

<Connector port="8080" protocol="HTTP/1.1"
           server="Apache"
           connectionTimeout="20000"
           redirectPort="8443" />

Configure Firewall

# UFW (Ubuntu)
sudo ufw allow 8080/tcp
sudo ufw enable

# firewalld (CentOS/RHEL)
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

Real-World Use Cases and Examples

Tomcat excels in several deployment scenarios:

  • Microservices Architecture – Deploy Spring Boot applications as embedded JAR files
  • Legacy Application Migration – Host traditional WAR files during cloud migration
  • API Gateway – Serve REST APIs with frameworks like Jersey or Spring MVC
  • Content Management – Power systems like Alfresco or custom CMS solutions

For high-availability setups, consider VPS clustering with load balancers and session replication.

Common Issues and Troubleshooting

Port Already in Use

If Tomcat fails to start with “Address already in use” errors:

# Check what's using port 8080
sudo netstat -tlnp | grep 8080
sudo lsof -i :8080

# Kill the process or change Tomcat's port in server.xml

Permission Denied Errors

# Fix ownership issues
sudo chown -R tomcat:tomcat /opt/tomcat
sudo chmod -R 755 /opt/tomcat/bin
sudo chmod -R 644 /opt/tomcat/conf

Out of Memory Errors

Monitor memory usage and adjust heap size:

# Check current memory usage
sudo -u tomcat /opt/tomcat/bin/catalina.sh version

# Increase heap in systemd service file
Environment='CATALINA_OPTS=-Xms1024M -Xmx2048M -server -XX:+UseG1GC'

SSL/HTTPS Configuration Issues

Generate a self-signed certificate for testing:

# Generate keystore
sudo keytool -genkey -alias tomcat -keyalg RSA -keystore /opt/tomcat/conf/keystore.jks

# Add HTTPS connector to server.xml
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
           maxThreads="150" SSLEnabled="true"
           keystoreFile="/opt/tomcat/conf/keystore.jks"
           keystorePass="your_password"
           clientAuth="false" sslProtocol="TLS" />

Monitoring and Maintenance

Set up log rotation to prevent disk space issues:

sudo nano /etc/logrotate.d/tomcat
/opt/tomcat/logs/catalina.out {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    create 644 tomcat tomcat
    postrotate
        systemctl restart tomcat
    endscript
}

Monitor performance with JConsole or integrate with tools like Prometheus for production environments. The official monitoring documentation provides comprehensive guidance for enterprise deployments.

Regular maintenance includes updating Java versions, applying Tomcat security patches, and monitoring application logs for performance bottlenecks. Consider automated deployment strategies using tools like Ansible or Docker for consistent environment management across development and production systems.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked