Monthly Archives: June 2013

Installing Tomcat 7 with Apache and mod_jk

Installing Java, Tomcat 7, Apache 2 and mod_jk in Ubuntu 12.04

Why would you need to install Apache and mod_jk to run a Java web application on your server? Well, you don’t have to, but there are a lot of reasons why you would want to. Removing :8080 from all the requests, rewriting URLs and handling static assets, performance (loadbalancing/clustering/CDN/etc.) and a whole host of other reasons. This guide assumes you have a brand new server up and running with nothing really installed yet.

The following is an extremely vanilla setup that I would consider my normal baseline configuration, the bare minimum to get Tomcat and Apache talking successfully. This will mainly aid in allowing all your web requests to be funneled through port 80 (instead of the normal Tomcat 8080) and allowing static assets to be served that exist outside of your application’s WAR file- think uploaded user images and such.

Install Java

First things first, without Java installed on your server, none of this works.

Go to the Oracle Java Download page and choose the proper JDK for your environment. Remember: A JRE is not enough for running Tomcat or other Java application/container servers.

Follow the instructions for unpacking and installing the JDK to your file system. This example assumes you installed the JDK to /usr/local

Install Tomcat

Download the latest Tomcat version, for this post we’ll be using Tomcat 7.

Unpack Tomcat and move the contents to where you want them. For this post, we’ll be using /usr/share/tomcat7

Install Apache and mod_jk

For Ubuntu, we’ll simply use the apt package manager to install these:

Configure the default site to use the default AJP worker. For this example, we will be configuring all requests to go to Tomcat

Edit /etc/apache2/sites-available/default:

Server Specific Configurations

These steps are not entirely necessary and can vary from OS to OS depending on how servers in your environment are normally set up.

Update alternatives to point to your Java install:

Edit /etc/environment and add the following:

Create a tomcat user for running the server and assign ownership of $TOMCAT_HOME (this is necessary for the Tomcat init script in the next step):

Install this tomcat7 init.d startup script that I created for better startup/shutdown of your server to /etc/init.d/tomcat7.