20 November 2010

Installing Tomcat onto Ubuntu Apache2

This post describes how to install Tomcat 6 on an Ubuntu 10.10 server and connect it up with the Apache2 server using the jk module. Although there are plenty of posts about this, none gave a full list of instructions; this is what I aim to provide - let me know if you have any corrections.

I assume that you have Apache2 installed.

You will need to do most of the following actions at a root shell prompt. Get this from your login using: sudu su - You will need to edit various text files. You can do this using vi at the shell prompt. However I usually do this on my local computer using my preferred editor, transferring files to and fro using FTP.

Installing Tomcat

This command gets Tomcat 6 - and Java if need be:
aptitude install tomcat6

I'm not sure if that gets all the Tomcat files, so enter this to get them all:
apt-get install tomcat6 tomcat6-admin tomcat6-common tomcat6-user tomcat6-docs tomcat6-examples

On my system, Java was installed to here: /usr/lib/jvm/default-java/

You now need to set up Java environment variables in text script file /etc/environment
Add this to the PATH variable: /usr/lib/jvm/default-java/bin after a colon separator. Add these lines:


You will now need to reboot so these changes are used.

Tomcat should now be running. You can start, stop or restart it as follows:

/etc/init.d/tomcat6 stop
/etc/init.d/tomcat6 status
/etc/init.d/tomcat6 restart

or more easily like this:

service tomcat6 restart

Tomcat should now be running on port 8080 at your host, eg http://www.example.com:8080/ If you go there, you should see an It works page. To test it further, I put a JSP file in here: /var/lib/tomcat6/webapps/ROOT/ and saw that it ran OK. I then put a servlet WAR file in /var/lib/tomcat6/webapps/. This was soon expanded automatically by Tomcat and I was able to navigate to the servlet directory (still on the 8080 port).

You may find it useful to run the manager and host-manager Tomcat applications. To enable these you need to edit the Tomcat configuration files in /etc/tomcat6/. Edit tomcat-users.xml to include the following:

<role rolename="manager"/>
<role rolename="admin"/>
<user name="admin" password="secret_password" roles="manager,admin"/>

You should now be able to access these apps at /manager/html and /host-manager/html. You will need to enter your credentials; possibly a couple of times on first access.

The Tomcat logs are at /var/log/tomcat6/. By default Tomcat creates a different log file for each day. Keep an eye on these, and the disk space they consume.

Connecting Tomcat to Apache

Hopefully you already know these commands to test your Apache configuration and restart Apache. You'll need these later.

apache2ctl configtest
apache2ctl restart

First, tell Tomcat to listen out for connections from Apache. Edit /etc/tomcat6/server.xml to uncomment this line:

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Restart Tomcat.

Install the jk module that acts as the connector between Apache and Tomcat:
apt-get install libapache2-mod-jk

You should see a file called jk.load in /etc/apache2/mods-available/. I edited this to contain:

LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
JkWorkersFile /etc/libapache2-mod-jk/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkShmFile /var/log/apache2/jk-runtime-status
JkLogLevel info

You can also add JkMount instructions here, but I found that I needed to put them in each VirtualHost file in /etc/apache2/sites-available/. Eg in my default VirtualHost file I added:

JkMount /*.jsp ajp13_worker
JkMount /manager ajp13_worker
JkMount /manager/* ajp13_worker
JkMount /host-manager ajp13_worker
JkMount /host-manager/* ajp13_worker

An alternative is to define JkMount commands in jk.load and add a JkMountCopy command to copy these settings to all virtual hosts.

You now need to edit the jk worker file /etc/libapache2-mod-jk/workers.properties. Make sure it has code as follows, ie to tell jk to use the Tomcat connection we defined earlier:


Restart apache.

I was now able to access the Tomcat manager here: http://www.example.com/manager/html.

Edit your other VirtualHost files to expose any other apps that you need.

You'll find the Apache and jk logs in here: /var/log/apache2/. As above, keep an eye on these files and the disk space they use.

If you see "missing uri map" in log file mod_jk.log then you need to define JkMount in your VirtualHost.

As a final task, if you wish, disable the Tomcat 8080 port by editing /etc/tomcat6/server.xml and then restart Tomcat.


Unknown said...

btw... You can do "service tomcat6 start" as well... That works for anything in /etc/init.d. :)

Chris Cant said...

Ta - I've added that.

Anonymous said...

I've followed your instructions works well. Only one problem is I'm deploying opengts (war). Everytime I login it logs me out straight away. I can't find any clues on any of the possible logs (catalina.out, modjk.log, apache2, access.log) runout of ideas.

Chris Cant said...

A quick search found this:


Anonymous said...

I've seen that, its not cookies because if I run a standalone version of tomcat it works fine and every other sties requiring cookies is fine. I'm stumped.

Jeremy said...

This was a fantastic write-up. Thanks for this.

Anonymous said...

Thank you. The "missing uri map" tip was very helpful. I was going crazy trying to figure it out.