Tip #1 – Watch Out for White Space
When installing Tomcat under Windows, the path to the installation directory should not contain white space. The default (c:/Program Files) is not acceptable, because it contains a white space character. Installing under “C:/ProgramFiles” is acceptable because this path does not contain any white space characters.
Tip #2 – Check The System Variables
Regardless of the operating system that Tomcat is installed under, the installer should check the system variables to make sure they are set correctly. In particular, CATALINA_HOME should be set to Tomcat’s installation directory.
Tip #3 – Set Username and Password for Tomcat Manager
If you are using the Tomcat Manager, set a username and password. Set this username’s role to “manager,tomcat”.
Tip #4 – Read the Log Files
By default, Tomcat’s log files are stored in the CATALINA_HOME/log directory. Use them to solve a myriad of problems.
Tip #5 – Configuring the Admin Web Application
If you are using the admin webapp that comes with Tomcat, make sure the path specified in the docBase attribute of the Context element is absolute, not relative. The admin context can be specified in admin.xml (for auto deployment) or server.xml.
If you are not using the admin webapp, delete admin.xml.
Tip #6 – The Admin Webapp vs. the Manager Webapp
The manager webapp that comes with Tomcat is a scaled-down, simplified version of the admin webapp. The Manager webapp lets you install new webapps on a non-persistent basis for testing, and also allows you to stop, reload, remove, or undeploy webapps. For better security, use manager rather than admin wherever possible.
Tip #7 – Two Options For Deploying a WAR File
If you have a WAR file, you can deploy it by simply copying it into CATALINA_BASE/webapps. When Tomcat notices the new WAR file sitting there, it will unpack the file into the appropriate subdirectory and create a context for it. The context it creates is defined in the DefaultContext element in server.xml.
If it is not appropriate to use the default context, create a context fragment for the webapp, put it in a file named x.xml (where x is the name of your webapp), and put the file into CATALINA_BASE/webapps. The webapp itself can then be stored anywhere on the filesystem.
In either case, you can deploy your webapp without editing server.xml and without restarting Tomcat. One caveat: Do not turn off liveDeploy.
Tip #8 – Configure Virtual Hosts in server.xml
Your test environment can be set up as a virtual host by adding a host element to the engine element in server.xml. Don’t forget to create the appropriate DNS entry, too.
Tip #9 – When Usage Increases, Balance the Load
Tomcat provides several load balancing techniques that allow requests to be spread across multiple servers. See also the following link: http://people.apache.org/~markt/presentations/2009-04-01-TomcatTuning.pdf
Tip #10 – Implement Custom Error Pages
Default error pages do not give the end users useful information. Create your own error pages and give the end users meaningful messages.
Tip #11 – Use a Logging Toolkit
Eliminate System.err and System.out from application code. Use a logging toolkit (e.g., Log4J) instead.
Tip #12 – Create Users to Access the Manager Webapp
If you plan to use the manager webapp, the default configuration won’t help you much because there are no users with the manager role. Add the manager role and at least one user who has the manager role in CATALINA_HOME/conf/tomcat-users.xml.
Tip #13 – Filter by IP or Host
If only certain machines are allowed to access your webapp, create a valve to let only those machines in. Specify the machines by IP address or hostname.
Tip #14 – Strip Down server.xml
Remove comments, connectors, and any other components you don’t need from server.xml to make the file more readable. However, since comments can be important, keep a copy of the original file around for future reference.
Remove
<Connector port=”8009? enableLookups=”false” redirectPort=”8443? protocol=”AJP/1.3?></Connector>
from server.xml if you are using standalone Tomcat (i.e., without the Apache server).
Tip #15 – Thread Pool
If using multiple CPUs, raising the thread pool above the default 250 may prove beneficial. On the other hand, reducing the thread count may be beneficial if your server is running slowly.
Tip #16 – Compress Network Traffic
Use the compression and compressableMimeType attributes of the connector element to configure GZIP compression and to specify the MIME types that should be compressed.
Tip #17 – Production Logging
Tomcat’s default configuration creates duplicate log files. Remove duplicate logging in .handlers (in logging.properties).
Tip #18 – Rotate The Tomcat Log Files
This example rotates log files. Adjust the values to suit your environment.
1catalina.java.util.logging.FileHandler.pattern = ${catalina.base}/logs/catalina.%g.log
1catalina.java.util.logging.FileHandler.limit = 20000000
1catalina.java.util.logging.FileHandler.count = 5
Tip #19 – Use Clustering With Load Balancing
When balancing the load between multiple Tomcat instances, if an instance fails, another instance can seamlessly take over its sessions if clustering is configured. Without clustering, a failing instance’s sessions will crash.
Tip #20 – Disable the Shutdown Port
Turn off Tomcat’s default shutdown port by setting the port attribute to -1 in the server element. This makes it impossible to shut down Tomcat via IP:port. Shutdown will only be possible by a kill command issued by the user who owns the Tomcat instance.
Tip #21 – Ignore Interfaces That Are Not Required
Connectors listen to all interfaces by default. Use the address attribute of the connector element to force the connector to ignore all interfaces that are not used by your webapp.
Recent Comments