Master Tomcat: From Installation to Multi‑Instance Deployment
This guide walks you through Tomcat's concepts, its role as a Java servlet container, step‑by‑step installation and configuration, detailed directory structure, deployment of a sample zrlog application, and how to set up multiple Tomcat instances on a single server, complete with code examples and log configuration.
Tomcat Overview
Tomcat concept
Tomcat role
Installation and deployment steps
Directory structure
Example: deploying zrlog
Multi‑instance deployment
Pre‑knowledge
Tomcat Concept
Tomcat is an open‑source, lightweight application server written in Java, part of the Apache Software Foundation. It implements Java Servlet, JSP, and EL specifications, enabling dynamic web applications on the Java platform.
Tomcat Role
Tomcat serves as a Java Servlet container, providing:
Support for Servlet and JSP, allowing developers to create dynamic web pages and handle user requests.
Web application deployment via WAR files placed in the webapps directory.
HTTP/HTTPS services, static resource serving, and integration with IDEs for development and debugging.
Installation and Deployment
Environment Information
IP
Hostname
Linux System
Specs
10.0.0.21
tomcat
Ubuntu22.04
2c4g
Before installing Tomcat, install JDK 8 (or OpenJDK 8) because Tomcat runs on Java.
mkdir -p /app/tools && cd /app/tools
wget https://download.java.net/openjdk/jdk8u44/ri/openjdk-8u44-linux-x64.tar.gz
tar -xf openjdk-8u44-linux-x64.tar.gz
ln -s /app/tools/java-se-8u44-ri/ /app/tools/jdk
cat >> /etc/profile <<EOF
export JAVA_HOME=/app/tools/jdk
export PATH=$JAVA_HOME/bin:$PATH
EOF
source /etc/profile
java -versionDownload and extract Tomcat 9.0.50:
wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.50/bin/apache-tomcat-9.0.50.tar.gz
tar -xf apache-tomcat-9.0.50.tar.gz
ln -s $(pwd)/apache-tomcat-9.0.50 tomcatStart Tomcat:
cd tomcat/bin
./startup.sh
# Verify process and port 8080
ps -ef | grep java
ss -lntup | grep 8080Tomcat Directory Structure
The main directories after installation are:
bin : executable scripts such as startup.sh, shutdown.sh, catalina.sh.
conf : configuration files like server.xml, web.xml, catalina.properties.
logs : log files ( catalina.out, daily logs, access logs).
webapps : default web applications and deployment directory for WAR files.
bin Directory
startup.sh– start Tomcat on Linux. shutdown.sh – stop Tomcat on Linux. catalina.sh – core script handling start, stop, and JVM options.
conf Directory
server.xml– defines ports, connectors, virtual hosts, and logging. web.xml – auxiliary configuration.
<?xml version="1.0" encoding="UTF-8"?>
<!-- 8005 shutdown port -->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
<Service name="Catalina">
<!-- HTTP connector -->
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm"/>
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t \"%r\" %s %b"/>
</Host>
</Engine>
</Service>
</Server>Log Format
%h – client IP address
%l – remote logical username (usually empty)
%u – authenticated user
%t – request time
%r – request line (method and URI)
%s – HTTP status code
%b – bytes sent
" – double quote
logs Directory
catalina.out– Tomcat application log. catalina.YYYY‑MM‑DD.log – daily rotated log. localhost_access_log.YYYY‑MM‑DD.txt – access log.
Example: Deploying zrlog Application
Download the zrlog WAR package and deploy it as the ROOT application:
mkdir -p /app/war && cd /app/war
wget https://dl.zrlog.com/release/zrlog.war
mv /app/tools/tomcat/webapps /app/tools/tomcat/webapps_bak_$(date +%Y%m%d)
mkdir /app/tools/tomcat/webapps
mv zrlog.war /app/tools/tomcat/webapps/ROOT.war
ls -l /app/tools/tomcat/webappsAccess the application at http://10.0.0.21:8080 and configure the database connection.
Start a MySQL 5.7 container for the database:
docker run -d --name mysql_5.7 -p 3306:3306 --restart always -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=1 mysql:5.7
docker exec -it mysql_5.7 bash -c "mysql -uroot -p1 -e 'create database zrlog;'"Tomcat Multi‑Instance Deployment
Background
When a server has abundant resources, you can run multiple Tomcat instances to better utilize CPU and memory.
Deployment Steps
Prepare Multiple Tomcat Directories
cd /app/tools
tar -xf apache-tomcat-9.0.50.tar.gz
cp -r apache-tomcat-9.0.50 tomcat_8081
cp -r apache-tomcat-9.0.50 tomcat_8082Modify Ports
Each instance must use unique ports (shutdown, HTTP, HTTPS). Example for the first instance:
sed -i 's/8005/8006/g' tomcat_8081/conf/server.xml
sed -i 's/8080/8081/g' tomcat_8081/conf/server.xml
sed -i 's/8443/8444/g' tomcat_8081/conf/server.xmlAnd for the second instance:
sed -i 's/8005/8007/g' tomcat_8082/conf/server.xml
sed -i 's/8080/8082/g' tomcat_8082/conf/server.xml
sed -i 's/8443/8445/g' tomcat_8082/conf/server.xmlStart Instances
/app/tools/tomcat_8081/bin/startup.sh
/app/tools/tomcat_8082/bin/startup.shVerify that ports 8081 and 8082 are listening:
ss -lntup | grep 8081
ss -lntup | grep 8082Test Access
Open http://10.0.0.21:8081 and http://10.0.0.21:8082 in a browser to confirm both instances are running.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
