Demystifying Tomcat and cPanel
Stephen Bee
All trademarks used herein are
the sole property of their
respective owners.
What is Tomcat?
A Java driven Web Application Server
− http://tomcat.apache.org/
Provides support for Java web applications
− JSP Documents
− WAR (Web Application aRchive) files
Has a self-contained HTTP server
The course of a Tomcat request
Browser Client
Apache Web Server
mod_jk
Tomcat Web Server
Port 8080 Apache/Tomcat Connector
Port 8009
Tomcat Server
JVM
Benefits (and down falls) of Tomcat
Easy to install
JVM and application libraries are pre-loaded
– Pro: Things only need to be compiled once
– Con: Requires a hefty chunk of memory
Installation
Installation Requirements
Any supported Linux distribution.
FreeBSD is not currently supported.
Click here for cPanel's system requirements
Installation Requirements
An EasyApache 3 equipped cPanel system.
Tomcat is no longer managed through WHM -> Plugins
Installation Requirements
An experienced Tomcat admin!
Application deployment will require administrative action
cPanel only configures and removes virtual hosts
Installing Tomcat in WHM
WHM -> Software -> Apache Update
Confirming the Installation Worked
http://server.example.com:8080/jsp-examples/
What's Installed?
What's Installed?
The Java Development Kit (JDK)
Contains the JRE (Java Runtime Environment)
Installed to /usr/local/jdk/
– Commonly referred to as $JAVA_HOME
What's Installed?
The binary Tomcat distribution
Is pre-compiled Java byte code. Is simply downloaded and extracted
Installs to /usr/local/jakarta/apache-tomcat-${VERSION}
/usr/local/jakarta/tomcat/ is a symbolic link to the installation directory
– Is also known as $CATALINA_HOME and/or $CATALINA_BASE
What's Installed?
Apache Tomcat Connectors (mod_jk)
Provides the ability for Apache to communicate with Tomcat
Installed as mod_jk.so in your Apache modules directory
What's Installed?
cPanel provisioned maintenance scripts
Used in favour of the Tomcat initialization scripts
Creates and removes Tomcat virtual hosts
The start up process
Start up :: Initialization Scripts
/usr/sbin/stoptomcat
/usr/sbin/starttomcat
/scripts/restartsrv_tomcat
Start up :: /usr/sbin/starttomcat
Called from /etc/rc.local during boot process
Defines the following important environment variables:
CATALINA_HOME CATALINA_BASE JAVA_HOME
/usr/local/jakarta/tomcat /usr/local/jakarta/tomcat The path to the JDK
Start up :: jsvc
Called by /usr/sbin/starttomcat
Located at /usr/local/jakarta/tomcat/bin/jsvc
Acts as a wrapper for and daemonizes Tomcat
− Has control of Tomcat's ports
http://commons.apache.org/daemon/jsvc.html
jsvc
client Apache
Tomcat
Start up :: jsvc :: Logging
Logs to /usr/local/jakarta/tomcat/logs/catalina.out
Three log levels exist: SEVERE, WARNING, and INFO
SEVERE: Error starting endpoint
java.net.BindException: Address already in use:8080
at org.apache.tomcat.util.net.PoolTcpEndpoint.initEndpoint(PoolTcpEndpoint.java:298)
Tail the logs for SEVERE and WARNING messages only:
tail -f /usr/local/jakarta/tomcat/logs/catalina.out | egrep -A2 '(SEVERE|WARNING):' &
Start up :: Starting the JVM
Tomcat loads the JVM at start up
JVM start up information is logged to catalina.out
All libraries are pre-loaded into memory for efficiency
[Opened /usr/local/jdk1.6.0_02/jre/lib/rt.jar]
[Loaded java.lang.Object from /usr/local/jdk1.6.0_02/jre/lib/rt.jar]
[Loaded java.io.Serializable from /usr/local/jdk1.6.0_02/jre/lib/rt.jar]
[Loaded java.lang.Comparable from /usr/local/jdk1.6.0_02/jre/lib/rt.jar]
[Loaded java.lang.CharSequence from /usr/local/jdk1.6.0_02/jre/lib/rt.jar]
[Loaded java.lang.String from /usr/local/jdk1.6.0_02/jre/lib/rt.jar]
...... truncated .....
Start up :: Auto Deploying Applications
By default, all WAR files are automatically deployed at start up
− Can be disabled on a per-host basis with the
deployOnStartup attribute
Loaded org.apache.catalina.startup.HostConfig
DeployedApplication from file:/usr/local/jakarta/apache-tomcat-5.5.25/server/lib/catalina.jar
May 22, 2008 5:12:48 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive example.war
Start up :: Knowing when it's completed
Pre-loading all libraries can take quite some time
Applications will be inaccessible until pre-loading completes
Start time in catalina.out indicates that start up has completed
INFO: Server startup in 3740 ms
Customizing the Environment
Start up :: Customizing the Environment
/var/cpanel/tomcat.options
Each argument is defined on a separate line
Arguments are parsed, and passed to jsvc
Complete list of JVM and jsvc options:
− http://blogs.sun.com/watt/resource/jvm-options-list.html
− /usr/local/jakarta/tomcat/bin/jsvc -help
Customization :: Resource Constraints
Place the following in /var/cpanel/tomcat.options
-Xmx200M
-Xms100M
The following should now be output from /scripts/restartsrv_tomcat
Adding in custom Tomcat option(s): -Xmx200M
Adding in custom Tomcat option(s): -Xms100M
Tomcat Configuration
Configuration :: General
Configuration files reside in /usr/local/jakarta/tomcat/conf/
Most configurations are stored in XML formatted documents
Tomcat must be restarted for changes to take effect
− /scripts/restartsrv_tomcat
Configuration :: server.xml
Configures Virtual Hosts, Connectors, and more.
Each instance of Tomcat has it's own server.xml
<Server>
<Service name=”Catalina”>
<Connector port=”8080” ... /> <Connector port=”8009” ... />
<Engine name=”Catalina” defaultHost=”localhost”>
Host
Host
<Host name=”tomcat.com” appBase=”/home/tomcat/public_html”>
<Alias>www.tomcat.com</Alias>
<Context path=”” reloadable=”true” docBase=”/home/tomcat/public_html”>
</Host>
http://tomcat.apache.org/tomcat-5.5-doc/config/index.html
Configuration :: Connectors
Facilitates communication between applications and the clients
Each Connector creates a worker process with a unique port
Standalone Web Server - Operates over HTTP
<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25"
maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
AJP – A more efficient binary format
<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
Configuration :: Context
Creates a mapping to a web application
The default (blank) context is always required
<Context path="/register” docBase="/home/joe/public_html/register.war"/>
http:// www.example.com / register / Checkout
Virtual Host Context Application
Parameter
The Front End
Apache Connectors (mod_jk)
The Front End :: Apache Connectors
Installed in Apache module directory (mod_jk.so)
Forwards applications requests to the Tomcat workers
Configured on a per-site basis
Module Documentation:
http://tomcat.apache.org/connectors-doc/reference/apache.html
Front End :: Module Configuration
/usr/local/apache/conf/httpd.conf
LoadModule jk_module modules/mod_jk.so
/usr/local/apache/conf/jk.conf
JkWorkersFile /usr/local/jakarta/tomcat/conf/workers.properties
JkLogFile /usr/local/apache/logs/mod_jk.log
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
Front End :: workers.properties
Defines parameters for communicating with Tomcat workers
Multiple workers can be defined, if required.
/usr/local/jakarta/tomcat/conf/workers.properties
workers.tomcat_home=/usr/local/jakarta/tomcat
workers.java_home=/usr/local/jdk/jre
worker.list=ajp12, ajp13
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13
worker.ajp13.lbfactor=1
Front End :: Site Configuration
Site configurations are included from the Apache userdata directories
# find /usr/local/apache/conf/ -iname cp_jkmount.conf | head -2
/usr/local/apache/conf/userdata/std/1/tom/tomcat.com/cp_jkmount.conf
/usr/local/apache/conf/userdata/std/2/tom/tomcat.com/cp_jkmount.conf
#
Which are then included into the relevant VirtualHost in httpd.conf
<VirtualHost 10.0.0.1:80>
ServerName tomcat.com
.....
Include "/usr/local/apache/conf/userdata/std/2/tom/tomcat.com/*.conf"
</VirtualHost>
Front End :: cp_jkmount.conf
JkMount is used to map a context path to a Tomcat worker
Example Configuration
<IfModule mod_jk.c>
JkMount /*.jsp ajp13
JkMount /servlet/* ajp13
JkMount /servlets/* ajp13
JkMount /*.do ajp13
</IfModule>
Front End :: mod_jk.log
Useful for troubleshooting communication failures with mod_jk
Log levels can be adjusted in /usr/local/apache/conf/jk.conf
− http://tomcat.apache.org/connectors-doc/reference/apache.html
Example Log Ouput
ajp_service::jk_ajp_common.c (2046): (ajp13) receiving from tomcat failed, recoverable operation attempt=0
ajp_connect_to_endpoint::jk_ajp_common.c (891): Failed opening socket to (127.0.0.1:8009) (errno=111)
ajp_send_request::jk_ajp_common.c (1311): (ajp13) error connecting to the backend server (errno=111)
Tying It All Together
Enabling Tomcat on your sites
Enabling Tomcat Support :: CLI
/scripts/addservlets --domain=example.com
Creates <Host> container in server.xml
Creates default cp_jkmount.conf for provided site
Copies jsptest.jsp into the provides site's document root
Enabling Tomcat Support :: WHM
Confirming that it works
http://www.example.com/jsptest.jsp
Removing Tomcat Support
/scripts/remservlets --
domain=example.com
Remotes <Host> container from server.xml
Removes cp_jkmount.conf for provided site
The User End
Deploying Applications
WAR Deployment :: The easy way
unzip filename.war
# unzip example.war
Archive: example.war
creating: META-INF/
inflating: META-INF/MANIFEST.MF
inflating: helloworld.jsp
creating: WEB-INF/
inflating: WEB-INF/web.xml
inflating: index.html
#
How Deployment Works
The appBase is scanned for WAR files every 10 seconds
WAR files are 'exploded' into the site's work directory
/usr/local/jakarta/tomcat/work/Catalina/example.com/war_file_name/
− Check catalina.out to confirm application
deployment
INFO: Deploying web application archive example.war
− Documentation for Automatic Application Deployment
WAR Deployment :: The other way
Drop the WAR file in a directory in the user's public_html directory
What Happens:
Application is now accessible via:
− http://example.com:8080/appname
mod_jk must be configured to recognize the application now
WAR Deployment :: Configuring mod_jk
Add a JkMount for the application to site include and restart httpd
Example Configuration
<IfModule mod_jk.c>
JkMount /*.jsp ajp13
JkMount /servlet/* ajp13
JkMount /servlets/* ajp13
JkMount /*.do ajp13
JkMount /appname/* ajp13
</IfModule>
Q&A
Resources
Tomcat Project Page
JDK Download Site
Useful Tomcat configuration tips