Demystifying Tomcat and cPanel

What is Tomcat?

1. A Java driven Web Application Server
− http://tomcat.apache.org/

2. Provides support for Java web applications
− JSP Documents
− WAR (Web Application aRchive) files

3. Has a self-contained HTTP server

The course of a Tomcat request

Benefits (and down falls) of Tomcat

1. Easy to install
2. 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.

1. FreeBSD is not currently supported.

2. Click here for cPanel’s system requirements

An EasyApache 3 equipped cPanel system.

Tomcat is no longer managed through WHM -> Plugins

An experienced Tomcat admin!

1. Application deployment will require administrative action
2. 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?

The Java Development Kit (JDK)
Contains the JRE (Java Runtime Environment)
Installed to /usr/local/jdk/
– Commonly referred to as $JAVA_HOME

 

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

 

Apache Tomcat Connectors (mod_jk)
Provides the ability for Apache to communicate with Tomcat
Installed as mod_jk.so in your Apache modules directory

 

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
/usr/local/jakarta/tomcat

CATALINA_BASE
/usr/local/jakarta/tomcat

JAVA_HOME
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

 

 

 Start up :: jsvc :: Logging
1. Logs to /usr/local/jakarta/tomcat/logs/catalina.out

2. 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

1. Tomcat loads the JVM at start up
2. JVM start up information is logged to catalina.out
3. 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

1. 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

1. Pre-loading all libraries can take quite some time
2. Applications will be inaccessible until pre-loading completes
3. Start time in catalina.out indicates that start up has completed

INFO: Server startup in 3740 ms

Start up :: Customizing the Environment

1. /var/cpanel/tomcat.options
2. Each argument is defined on a separate line
3. 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/

1. Most configurations are stored in XML formatted documents
2. Tomcat must be restarted for changes to take effect
− /scripts/restartsrv_tomcat

Configuration :: server.xml

1. Configures Virtual Hosts, Connectors, and more.
2. Each instance of Tomcat has it’s own server.xml

Configuration :: Connectors

1. Facilitates communication between applications and the clients

2. 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″ />

 

The Front End
Apache Connectors (mod_jk)

The Front End :: Apache Connectors
1. Installed in Apache module directory (mod_jk.so)
2. Forwards applications requests to the Tomcat workers
3. 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

1. Defines parameters for communicating with Tomcat workers
2. 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
1. Useful for troubleshooting communication failures with mod_jk
2. 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

1. Creates <Host> container in server.xml
2. Creates default cp_jkmount.conf for provided site
3. Copies jsptest.jsp into the provides site’s document root

Enabling Tomcat Support :: WHM

Removing Tomcat Support

/scripts/remservlets –domain=example.com

1. Remotes <Host> container from server.xml
2. Removes cp_jkmount.conf for provided site

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

1. The appBase is scanned for WAR files every 10 seconds
2. 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:

1. Application is now accessible via:

− http://example.com:8080/appname
2. 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>

Resources

Tomcat Project Page
JDK Download Site
Useful Tomcat configuration tips

Similar Posts:

    None Found

One comment on “Demystifying Tomcat and cPanel

Leave a Reply

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