Performance
- Use the latest JVM that Tomcat and your applications will support
- Use JAVA_OPTS (or CATALINA_OPTS) to:
- Configure heap sizes, garbage collection settings, and other settings (discussed in detail in class)
- In $CATALINA_BASE/conf/server.xml:
- Remove any (<Connector/>) elements you don’t need.
- For those that remain, configure minSpareThreads, maxThreads, and acceptCount according to the volume you anticipate
- Don’t set acceptCount or maxThreads too high - it may be better to deny a few requests than to overload the server.
- In the (<Host/>) element, turn off autoDeploy if you don’t need it.
- In $CATALINA_BASE/conf/context.xml:
- Comment out the WatchedResource to disable frequent checking of your applications’ web.xml files for changes.
- Consider disabling session persistence across restarts if not needed.
- In $CATALINA_BASE/conf/web.xml, set the development init-param of the jsp servlet to false.
- Use HTML (instead of JSP) pages wherever possible.
- Uninstall applications you don’t need (see #1 in Security below).
- Use the APR or NIO connectors (Tomcat 8.5 will default to NIO; BIO no longer available).
Security
- Uninstall all applications that you don’t need. Candidates:
- / (ROOT)
- docs
- examples
- manager and host-manager
- Consider running Tomcat as its own user, and give that user permissions only on the JVM and Tomcat’s directories.
- Consider running Tomcat in -security mode.
- In $CATALINA_BASE/conf/server.xml:
- Change the shutdown port to -1 to disable the shutdown port if you are running Tomcat as a service. Otherwise, at least change the shutdown command.
- Remove connectors that you don’t need
- ajp13 can be removed if you’re running Tomcat standalone.
- http and/or https can be removed if you’re running Tomcat behind Apache httpd or IIS.
- For each (<Connector/>), consider setting the server attribute to report a bogus server header.
- If you’re not running host-manager and/or manager, take out the (<Resource>) tag setting up the default realm and the <Realm> tag that implements it.
- If you are using the applications in d) above, configure the UserDatabaseRealm to use hashed passwords, or use a different realm (such as JDBCRealm, JNDIRealm, or JAASRealm) with hashed passwords.
- At the Engine or Host levels, or in separate context files, consider using the RequestFilterValve to restrict which client IP address(es) are permitted access.
- $CATALINA_BASE/conf/web.xml:
- In the default servlet, make sure the value of the listings init-param is false.
- Make sure the invoker, cgi, and ssi servlets are commented out (they all are by default in Tomcat 5.0 and later).
- Set the session timeout to a shorter interval.
- Add error-page elements to specify your own error pages for HTTP status codes 400-417 and 500-509.
- In Tomcat 7 and later, you can add a default error-page that simplifies this greatly.
- Make sure that any desired welcome pages are registered.
- If you enable remote access via JMX, require SSL and/or authentication; don’t just leave the JMX port open for all visitors.
- Check all code you deploy for SQL injection, XML injection, and XSS vulnerabilities.
Important: There are many more steps you could take to harden tomcat. Please consult http://tomcat.apache.org/tomcat-8.5-doc/security-howto.html for additional ideas (adjust URL to your Tomcat version).