Thursday, December 20, 2007

CentOS 5 + Tomcat5 + Java 6

I just installed a CentOS 5 server and I wanted to run Tomcat5 with the Sun Java 6 JRE, not the JRE that's packaged with CentOS 5. Naturally everything broke. It took about an hour of poking and debugging, but in the end the fix was surprisingly simple. Here's how I did it:
  • Removed all of the original java packages (java-*, gcc-java glib-java)
  • Installed the JDK 6 RPM downloaded from the Sun website.
  • Create two symbolic links:
    • ln -s /usr/java/latest /usr/lib/jvm/java
    • ln -s /usr/java/latest /usr/java/default-exports
That's it.

If you want to run Tomcat5 on port 80, it needs to have root privileges to bind to a low order port. In its default configuration Tomcat5 cannot do this because it is a pure Java application and must run cross-platform. Dropping privileges is not platform agnostic so Tomcat5 does not support it.

It is dangerous to run Tomcat5 as root, so only do it if you know what you are doing! I took this approach on this server because it is an intranet application server that is not exposed to the outside world. Do not do this if you are exposing a Tomcat server to the outside world...

Here's what to do:
  • Update the /etc/tomcat5/server.xml file and look for <Connector port="8080" Change the 8080 to 80.
  • In the file /etc/sysconfig/tomcat5 change "#TOMCAT_USER=tomcat" to "TOMCAT_USER=root"

If you are exposing your server to the outside world, your best bet is to do it in a way that it is only root while it is binding to the low order port. Here is a tutorial that appears to explain how to do this. Your other option is to run Apache on the same server and use it as a proxy. The upside to this approach is that you can use Apache to serve static content, like images, more efficiently than with Tomcat.