# How do I properly run Tomcat alongside Apache?

I have installed Apache Tomcat and Apache Webserver on a Ubuntu 10.04 Server system. I basically followed these steps to configure the mod-jk connector and my webserver now seems to redirect all traffic to Tomcat.

This means regardless of wether I use port 80 or 8080 I am served the contents of the Tomcat ROOT directory. That's fine but I would still like to be able to use Apache to serve static and PHP content (like PHPMyAdmin).

-

If you're going with mod_jk, which is a popular option, Oscar's answer is correct. You need to limit what gets processed by Tomcat by changing something like this:

JkMount /* ajp13_worker

...to just the paths you'd like processed by Tomcat, e.g.:

JkMount /*.jsp ajp13_worker
JkMount /*.cfc ajp13_worker
JkMount /*.cfm ajp13_worker

If you're looking for additional options, mod_proxy_http or mod_proxy_ajp could also be used. I'll continue a couple quick examples for proxying *.jsp/*.cfm/*.cfc requests to Tomcat...

If you'd like to try both options, start by enabling both modules (and any dependencies), which is very simple with Ubuntu 10.04 ;-)

sudo a2enmod proxy_http proxy_ajp
sudo /etc/init.d/apache2 restart

I like to add my proxy configuration in /etc/apache2/conf/httpd.conf on Ubuntu, but you could also place this in just one or more VirtualHost configs.

For any proxy to be allowed, assuming you're running Apache and Tomcat on the same server, you'll want to add a directive like this:

<Proxy *>
Allow from 127.0.0.1
</Proxy>

Here's a configuration using mod_proxy_ajp:

ProxyPreserveHost On
ProxyPassMatch  ^/(.+\.cf[cm])(/.*)?$ajp://localhost:8009/$1$2 ProxyPassMatch ^/(.+\.jsp)$  ajp://localhost:8009/$1 Obviously, the regular expressions can be tweaked to suit your needs (the above two ProxyPassMatch directives could even be combined into a single regex fairly easily). To accomplish the same with mod_proxy_http, just change the protocol and port: ProxyPreserveHost On ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$  http://localhost:8080/$1$2
ProxyPassMatch  ^/(.+\.jsp)$http://localhost:8080/$1

If you'd like to proxy specific hosts, and maybe mask a context path, you might prefer something like this within a specific VirtualHost:

ProxyPreserveHost Off
ProxyPass        / http://example.site.com:8080/context/
ProxyPassReverse / http://example.site.com:8080/context/

Finally, if you need to get fancy (say, something odd like inspecting a query string or whatever else you can dream up with RewriteCond/RewriteRule), you could even leverage mod_rewrite with the proxy (P) flag to proxy to Tomcat. You'd need to enable one more module, mod_rewrite:

sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart

Then, within a VirtualHost, you can proxy CFML requests with:

RewriteEngine On
RewriteRule  ^/(.*\.cf[cm]l?)(/.*)?$ajp://localhost:8009/$1\$2  [P]

As you can see, you can get quite creative :P Hope that helps!

-
Thank you! Those are some great tips. –  mfellner Jul 28 '10 at 13:09

Use JkUnmount for stuff you don't want to be serve by tomcat:

JkMount /* yourworker
JkUnMount /*.php yourworker
JkUnMount /*.png yourworker
JkUnMount /*.jpg yourworker
etc...

(Not sure if you can do: JkUnMount /*.{php|jpg|gif|png|etc..})

Or if you only use jsp for tomcat try:

jkMount /*.jsp worker

-
Since I only run a single servlet application on Tomcat I decided to simply mount that directory. Thanks for the help! –  mfellner Jul 28 '10 at 13:11