Tomcat 8.5.24 Reference
Prerequisites
This guide uses Tomcat 8.5.24 with JDK 1.8+. Ensure your Java environment meets these requirements before proceeding.
Overview
What is Tomcat
Tomcat is an open-source Servlet container developed by the Apache Software Foundation. It implements the Servlet and JSP specifications while providing additional web server capabilities such as management interfaces, security realms, and Tomcat valves.
While Tomcat includes an embedded HTTP server and can serve as a standalone web server, it differs fundamentally from Apache HTTP Server, which is written in C. These two servers are not bundled together, though they can be configured to work in tandem.
Tomcat provides a web-based configuration tool, though all settings can also be managed through XML configuration files.
Key Directories
- /bin - Executable scripts for Unix (.sh) and Windows (.bat) systems
- /conf - Configuration files directory
- /logs - Default logging location
- /webapps - Deployment directory for web applications
Standard Web Application Structure
|-- webapp
|-- META-INF
| `-- MANIFEST.MF
|-- WEB-INF
| |-- classes
| | |-- *.class
| | `-- *.xml
| |-- lib
| | `-- *.jar
| `-- web.xml
|-- resources
`-- static-assets
Structure breakdown:
webapp: Deployment root directory. WAR files are essentially compressed webapp directories.META-INF: Contains metadata generated by development tools.WEB-INF: Protected directory inaccessible to clients; server-side code only./WEB-INF/classes: Compiled Java classes and resource files./WEB-INF/lib: Required JAR libraries./WEB-INF/web.xml: Application deployment descriptor defining servlets, components, initialization parameters, and security constraints.
Installation
Environment Setup
Upload apache-tomcat-8.0.52.tar.gz and JDK installation package to the target server.
# Extract and configure JDK
tar -xvf jdk-1.8.0 -C /application/
ln -s /application/jdk1.8.0_xxx /application/jdk
# Configure environment variables
cat >> /etc/profile <<'EOF'
export JAVA_HOME=/application/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
EOF
source /etc/profile
java -version
# Install Tomcat
tar -xvf apache-tomcat-8.0.52.tar.gz -C /application/
ln -s /application/apache-tomcat-8.0.52 /application/tomcat
echo 'export TOMCAT_HOME=/application/tomcat' >> /etc/profile
source /etc/profile
# Set ownership
chown -R root.root /application/jdk /application/tomcat/
Controlling Tomcat
# Start Tomcat
/application/tomcat/bin/startup.sh
# Stop Tomcat
/application/tomcat/bin/shutdown.sh
Logging
Check catalina.out for runtime issues and debugging information.
Management Interface Security
Modify conf/tomcat-users.xml to enable web-based management (not recommended for production):
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="secure_pass" roles="manager-gui,admin-gui"/>
Remove unused applications to reduce attack surface:
mv docs/ examples/ host-manager/ manager/ /tmp/
mv ROOT/* /tmp/
Core Configuration
The main configuration file is conf/server.xml. Deploy WAR files to the webapps directory for automatic deployment.
To change the default application path, add a Context element:
<Context path="" docBase="/data/webapps/myapp" debug="0" reloadable="false" crossContext="true"/>
Running Multiple Instances
Instance Setup
Copy the Tomcat directory to create additional instances:
cp -a /application/tomcat /application/tomcat-node2
Modify port settings in conf/server.xml:
<!-- Shutdown port: line 22 -->
<Server port="8011" shutdown="SHUTDOWN">
<!-- HTTP connector: line 69 -->
<Connector port="8081" protocol="HTTP/1.1" ... />
<!-- Host configuration: line 123 -->
<Host name="localhost" appBase="/data/www/www" unpackWARs="true" autoDeploy="true">
Multi-Tenant Deployment
Create a shared application base:
mkdir -p /data/www/www/ROOT
Deploy a simple test page:
<html>
<body>
<center>Current time: <%=new java.util.Date()%></center>
</body>
</html>
Access at http://server:8081 to verify the second instance.
Load Balancing with Nginx
Nginx Configuration
upstream tomcat_cluster {
server 10.0.0.1:8080 weight=1 max_fails=3 fail_timeout=20s;
server 10.0.0.2:8081 weight=1 max_fails=3 fail_timeout=20s;
}
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://tomcat_cluster;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
Performance Tuning
The default Tomcat configuration is not optimized for production use. Without proper tuning, instances may crash or require frequent restarts. Performance optimization spans three areas: operating system parameters, Tomcat settings, and JVM configuration.
System-Level Considerations
Maximize available memory, CPU frequency, and file system I/O throughput. Under high concurrency, CPU processing power directly impacts response times.
Tomcat Configuration Optimization
Architecture Decision
Implement request routing where Apache handles static content while Tomcat processes dynamic JSP files. Three integration methods exist: JK, http_proxy, and ajp_proxy. JK offers proven stability; the latter two provide simpler configuration with comparable flexibility.
Connector Protocol Selection
Tomcat supports bio, nio, and apr connector types with significant performance differences. APR provides the best throughput while bio is the slowest. Tomcat 7 defaults to APR when the native library is available; otherwise it falls back to bio.
Thread Pool Configuration
Default configuration creates a maximum of 200 threads with 5 idle threads pre-spawned. Modify conf/server.xml to enable the thread pool executor:
<Executor name="appThreadPool" namePrefix="catalina-exec-"
maxThreads="500" minSpareThreads="20" maxSpareThreads="50" maxIdleTime="60000"/>
Apply the executor to the connector:
<Connector executor="appThreadPool"
port="8080" protocol="HTTP/1.1"
URIEncoding="UTF-8"
connectionTimeout="30000"
enableLookups="false"
disableUploadTimeout="false"
connectionUploadTimeout="150000"
acceptCount="300"
keepAliveTimeout="120000"
maxKeepAliveRequests="1"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg,image/png"
redirectPort="8443" />
Parameter reference:
maxThreads: Maximum concurrent threads; default 200minSpareThreads: Minimum idle threads at startup; default 10maxSpareThreads: Maximum idle threads before cleanup; default 50URIEncoding: URL encoding format (default ISO-8859-1)connectionTimeout: Socket timeout in milliseconds; default 20000enableLookups: DNS resolution for remote addresses; set tofalsefor performanceacceptCount: Request queue depth when all threads busy; default 100keepAliveTimeout: Connection retention time for keep-alive requestsmaxKeepAliveRequests: Maximum requests per connection before closure; 1 disables keep-alivecompression: GZIP response compression; reduces bandwidth usage by approximately 33%
For advanced connector options, consult the official Tomcat documentation:
- https://tomcat.apache.org/tomcat-7.0-doc/config/http.html
- https://tomcat.apache.org/tomcat-7.0-doc/config/ajp.html
JVM Configuration
Tomcat runs as a Java process, so JVM parameters directly impact performance. Garbage collection pauses can interrupt request processing. Proper GC tuning requires understanding your application's characteristics, hardware configuration, and response time requirements.
Environment Variable Distinction
Two variables control JVM options:
JAVA_OPTS: Applied to start, stop, and run commandsCATALINA_OPTS: Applied to start and run commands only
Use CATALINA_OPTS for Tomcat-specific settings; use JAVA_OPTS when other Java applications share the environment.
64-bit System Configuration
Edit bin/catalina.sh to add memory and GC parameters:
CATALINA_OPTS="-server \
-Xms6000M \
-Xmx6000M \
-Xss512k \
-XX:NewSize=2250M \
-XX:MaxNewSize=2250M \
-XX:PermSize=128M \
-XX:MaxPermSize=256M \
-XX:+AggressiveOpts \
-XX:+UseBiasedLocking \
-XX:+DisableExplicitGC \
-XX:+UseParNewGC \
-XX:+UseConcMarkSweepGC \
-XX:MaxTenuringThreshold=31 \
-XX:+CMSParallelRemarkEnabled \
-XX:+UseCMSCompactAtFullCollection \
-XX:LargePageSizeInBytes=128m \
-XX:+UseFastAccessorMethods \
-XX:+UseCMSInitiatingOccupancyOnly \
-Duser.timezone=Asia/Shanghai \
-Djava.awt.headless=true"
Key memory parameters:
-Xms/-Xmx: Initial and maximum heap size. Setting identical values prevents dynamic resizing overhead.-Xss: Stack size per thread. 512k provides adequate nesting depth for typical applications.-XX:NewSize/-XX:MaxNewSize: Young ganeration size. Target 50-75% of total heap for web applications.-XX:PermSize/-XX:MaxPermSize: Permanent generation size for class metadata.
GC-related parameters:
-XX:+UseParNewGC: Parallel young generation collection-XX:+UseConcMarkSweepGC: Concurrent mark-sweep for old generation-XX:+UseCMSCompactAtFullCollection: Memory defragmentation during full GC-XX:MaxTenuringThreshold: Object age before promotion to old generation
Platform-specific notes:
On 32-bit systems, JVM memory cannot exceed 2GB. 64-bit systems have no such limitations but require careful heap sizing to avoid excessive garbage collection pauses.