There are a few steps that I generally take to setup a new Tomcat server instance, this enables the following:
- The manager console
- HTTP compression
- UTF-8 encoding
- tomcat-users.xml – add to bottom:
<user username="tomcat" password="s3cr3t" roles="manager-gui"/>
server.xml – add compression and URIEncoding, change port if desired:
<Connector port="8080" protocol="HTTP/1.1"
redirectPort="8443" compression="on" URIEncoding="UTF-8" />
- server.xml – relocate webapps by adding ../ to appBase
<Host name="localhost" appBase="../webapps"
- Restart your server, on Ubuntu use:
sudo service tomcat7 restart
This is a relatively common problem in JSP based apps as you need to understand the configuration. It’s further complicated if you use Apache HTTPD in front of the Apache Tomcat server to process requests as you need to know where each request is processed.
For this example, we will use the standard 404 error, but you can also intercept other errors for custom pages.
- create 404.jsp:
<% final SimpleDateFormat simpleDate = new SimpleDateFormat("EE MMM dd yyyy hh:mm:ss aa zzz");
final String dttm = simpleDate.format(new Date()); %>
<title>404 Not Found</title>
<li>Time: <%= dttm %></li>
<li>User-Agent: <%= request.getHeader("User-Agent") %></li>
<li>Server: <%= request.getServerName() %></li>
<li>Request: <%= request.getRequestURI() %></li>
<li>Remote: <%= request.getRemoteAddr() %></li>
<li>Referer: <%= request.getHeader("Referer") %></li>
- in WEB-INF/web.xml – add the following (NOTE: location within the file is important but outside the scope of this post)
- You might want to force the HTTP Header to give something other than a ‘404 status’ code, otherwise MSIE will show an unstyled ‘friendly error message’ if the user has not turned off the default setting. Unfortunately, this also means that search engines might index these pages that should not exist.
This one escaped me for a long time and I never saw a decent example of it in any of the documentation.
GZip compression saves on network bandwidth as files are compressed during transport between the HTTP Server and browser/client. If you already use Apache HTTP or a similar webserver to front Tomcat, this is not always necessary, but in cases where you expose your appserver directly, even if it is just for testing, you may want to add this configuration item as it increases the perceived speed of the application.
The solution is simple:
- To be safe, first stop the server and backup your configuration files
- Look in the /TOMCAT/conf installation folder.
- In the ‘server.xml’ file, you will find a line resembling…
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
- This one controls the HTTP/1.1 connections, add a new value to the list…
- NOTE You might also see a value for for AJP/1.3, unfortunately compression only works for HTTP:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
- Restart your server.
If you have ever looked at the console or logs while starting a Tomcat instance on Windows you have probably seen the following line about APR.
INFO: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path
As long as the “tcnative-1.dll” is in the Windows PATH, generally you can place it in c:\windows\system32, but any other location in the PATH will work should you need it to be portable, or have different versions in use.
NOTE: Other Operating Systems use a similar approach as Windows to add an environmental variable, optionally you can also add the appropriate location to the “java.library.path” attribute used when calling the VM, if you are more technically inclined. Also, be aware of 32 vs. 64 bit architectures when selecting your version!
If you do any development or even production testing with Apache Tomcat, you may have seen the following message in your logs.
“The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path”
Here’s a quick solution that will leave you with greatly improved performance.
- Go to the following URI’s:
- Download the appropriate version of tcnative-1.dll
- For Windows, place that file in c:\windows\system32\
- Restart your Tomcat server
- You are done!
Often you want to use Apache HTTP for static content, yet use Tomcat for JSP and other Java type work. This is a very common infrastructure for enterprise applications, particularly when using ‘pools’ of servers for performance, redundancy and security.
In order to accomplish this, all connections need to be handled by the Apache webserver, which will delegate appropriate requests to Tomcat for it to process.
Here’s a simple setup to get you started:
- First you need to get the connector appropriate to your installation:
- Next make sure the connector file is in the /conf folder of your Apache installation.
NOTE: I prefer to use this path and leave the version name to make maintenance and backups easier.
- Add the following line to httpd.conf
LoadModule jk_module conf/mod_jk-1.2.26-httpd-2.2.4.so
- Now, add the following to http.conf
- Add the c:/APACHEPATH/conf/workers.properties file with the following (minimal) contents:
- Finally, restart both Apache and Tomcat
- The following file should have been created in c:/TOMCATPATH/conf/auto/mod_jk.conf
########## Auto generated on …some datetime… ##########
LoadModule jk_module “C:/APACHEPATH/conf/mod_jk-1.2.26-httpd-2.2.4.so”
JkMount /webdav ajp13
JkMount /webdav/* ajp13
JkMount /servlets-examples ajp13
JkMount /servlets-examples/* ajp13
JkMount /jsp-examples ajp13
JkMount /jsp-examples/* ajp13
JkMount /balancer ajp13
JkMount /balancer/* ajp13
JkMount /host-manager ajp13
JkMount /host-manager/* ajp13
JkMount /tomcat-docs ajp13
JkMount /tomcat-docs/* ajp13
JkMount /manager ajp13
JkMount /manager/* ajp13
If all went well, you should be able to access your Tomcat server webapps on the regular HTTP port used by your Apache installation.
I often find myself administering WAMP (Windows, Apache, PHP/Perl/Python, mySQL) servers…. usually this occurs because it is better ‘supported’ (or perhaps ‘tolerated’) configuration in a corporate alternative to the more common LAMP (Linux… etc.) variety. This gives you the benefit of a centrally controlled operating system while maintaining a mostly open source server environment. Albeit with Microsoft’s poor security record, you’ll be patching it a LOT!
Many common distributions exist… here’s some helpful resources with downloads:
If you are a Java shop, you might also consider the following…
Configuration of each of these is a topic in it’s own right. If you need a shortcut to development, you may want to check out this!