{"id":1773,"date":"2012-12-03T13:42:42","date_gmt":"2012-12-03T05:42:42","guid":{"rendered":"http:\/\/rmohan.com\/?p=1773"},"modified":"2012-12-03T13:42:42","modified_gmt":"2012-12-03T05:42:42","slug":"21-apache-tomcat-configuration-tips","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=1773","title":{"rendered":"21 Apache Tomcat Configuration Tips"},"content":{"rendered":"<h3>Tip #1 \u2013 Watch Out for White Space<\/h3>\n<p>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 \u201cC:\/ProgramFiles\u201d is acceptable because this path does not contain any white space characters.<\/p>\n<h3>Tip #2 \u2013 Check The System Variables<\/h3>\n<p>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\u2019s installation directory.<\/p>\n<h3>Tip #3 \u2013 Set Username and Password for Tomcat Manager<\/h3>\n<p>If you are using the Tomcat Manager, set a username and password. Set this username\u2019s role to \u201cmanager,tomcat\u201d.<\/p>\n<h3>Tip #4 \u2013 Read the Log Files<\/h3>\n<p>By default, Tomcat\u2019s log files are stored in the CATALINA_HOME\/log directory. Use them to solve a myriad of problems.<\/p>\n<h3>Tip #5 \u2013 Configuring the Admin Web Application<\/h3>\n<p>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.<\/p>\n<p>If you are not using the admin webapp, delete admin.xml.<\/p>\n<h3>Tip #6 \u2013 The Admin Webapp vs. the Manager Webapp<\/h3>\n<p>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.<\/p>\n<h3>Tip #7 \u2013 Two Options For Deploying a WAR File<\/h3>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>In either case, you can deploy your webapp without editing server.xml and without restarting Tomcat. One caveat: Do not turn off liveDeploy.<\/p>\n<h3>Tip #8 \u2013 Configure Virtual Hosts in server.xml<\/h3>\n<p>Your test environment can be set up as a virtual host by adding a host element to the engine element in server.xml. Don\u2019t forget to create the appropriate DNS entry, too.<\/p>\n<h3>Tip #9 \u2013 When Usage Increases, Balance the Load<\/h3>\n<p>Tomcat provides several load balancing techniques that allow requests to be spread across multiple servers. See also the following link:\u00a0<a href=\"http:\/\/people.apache.org\/~markt\/presentations\/2009-04-01-TomcatTuning.pdf\">http:\/\/people.apache.org\/~markt\/presentations\/2009-04-01-TomcatTuning.pdf<\/a><\/p>\n<h3>Tip #10 \u2013 Implement Custom Error Pages<\/h3>\n<p>Default error pages do not give the end users useful information. Create your own error pages and give the end users meaningful messages.<\/p>\n<h3>Tip #11 \u2013 Use a Logging Toolkit<\/h3>\n<p>Eliminate System.err and System.out from application code. Use a logging toolkit (e.g., Log4J) instead.<\/p>\n<h3>Tip #12 \u2013 Create Users to Access the Manager Webapp<\/h3>\n<p>If you plan to use the manager webapp, the default configuration won\u2019t 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.<\/p>\n<h3>Tip #13 \u2013 Filter by IP or Host<\/h3>\n<p>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.<\/p>\n<h3>Tip #14 \u2013 Strip Down server.xml<\/h3>\n<p>Remove comments, connectors, and any other components you don\u2019t 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.<\/p>\n<p>Remove<\/p>\n<p>&lt;Connector port=\u201d8009? enableLookups=\u201dfalse\u201d redirectPort=\u201d8443? protocol=\u201dAJP\/1.3?&gt;&lt;\/Connector&gt;<\/p>\n<p>from server.xml if you are using standalone Tomcat (i.e., without the Apache server).<\/p>\n<h3>Tip #15 \u2013 Thread Pool<\/h3>\n<p>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.<\/p>\n<h3>Tip #16 \u2013 Compress Network Traffic<\/h3>\n<p>Use the compression and compressableMimeType attributes of the connector element to configure GZIP compression and to specify the MIME types that should be compressed.<\/p>\n<h3>Tip #17 \u2013 Production Logging<\/h3>\n<p>Tomcat\u2019s default configuration creates duplicate log files. Remove duplicate logging in .handlers (in logging.properties).<\/p>\n<h3>Tip #18 \u2013 Rotate The Tomcat Log Files<\/h3>\n<p>This example rotates log files. Adjust the values to suit your environment.<\/p>\n<p>1catalina.java.util.logging.FileHandler.pattern = ${catalina.base}\/logs\/catalina.%g.log<\/p>\n<p>1catalina.java.util.logging.FileHandler.limit = 20000000<\/p>\n<p>1catalina.java.util.logging.FileHandler.count = 5<\/p>\n<h3>Tip #19 \u2013 Use Clustering With Load Balancing<\/h3>\n<p>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\u2019s sessions will crash.<\/p>\n<h3>Tip #20 \u2013 Disable the Shutdown Port<\/h3>\n<p>Turn off Tomcat\u2019s 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.<\/p>\n<h3>Tip #21 \u2013 Ignore Interfaces That Are Not Required<\/h3>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tip #1 \u2013 Watch Out for White Space <\/p>\n<p>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 \u201cC:\/ProgramFiles\u201d is acceptable because this path does not contain any white space characters.<\/p>\n<p> Tip #2 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1773"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1773"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1773\/revisions"}],"predecessor-version":[{"id":1776,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1773\/revisions\/1776"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}