You can configure the Apache web server to forward requests to Tomcat. Thus, you can speak to both servers on ports 80 or 443 and get rid of the :8080 for your Tomcat applications. I’m somehow doing that very often, so here is small how-to for copy&paste purposes.

Install jk

As you might know, while Tomcat is Java stuff Apache is written in C. So in general it’s not that easy to get them talking to each other. The key to achieve an integration is called mod_jk (see The Apache Tomcat Connector). So first of all you need to install it:

aptitude install libapache2-mod-jk

If it is installed you can configure an AJP worker in /etc/libapache2-mod-jk/workers.properties :

# Defining a worker named ajp13_worker and of type ajp13
# Note that the name and the type do not have to match.
#
worker.ajp13_worker.port=8009
worker.ajp13_worker.host=localhost
worker.ajp13_worker.type=ajp13

As soon as this is done the bridge is ready to close the gap between Apache and Tomcat.

Configure Tomcat

We need to configure an AJP connector on port 8009 . So open /etc/tomcat7/server.xml and add another connector next to the other ones:

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

If you’re lucky there is already such a connector defined in the comments. So just remove the comment…

Configure Apache to speak through jk

Here I’ll show you how to setup a virtual host. For example, copy the following to /etc/apache2/sites-available/012-yourapp.conf :

<VirtualHost *:80>
      ServerAdmin some@body.tld
      ServerName yourapp.yourserver.tld
      ServerAlias ya.yourserver.tld

      RewriteEngine on
      RewriteRule ^/(.*)$ /YourApp/$1 [L,PT]

      JkMount /* ajp13_worker
</VirtualHost>

Ok, let me shortly explain what I did there.

  1. Everything that arrives at this vhost gets forwarded to our previously defined AJP worker (line 9)
  2. I assume your Tomcat webapp is running on server:8080/YourApp , therefor I configured a substitution of the URL to insert /YourApp (line 7). Of course you need to have mod_rewrite installed and enabled. (You may skip this line if you’re fine with having /YourApp in all your URLs)
  3. The rest should be clear. The vhost is available at http://yourapp.yourserver.tld , as well as at http://ya.yourserver.tld (lines 3&4). You can also use SSL, just configure line 1 to listen at *:433 and add the SSL stuff to the body of your vhost. (SSL exmaple)

Afterwards, enable the vhost to populate it:

a2ensite 012-yourapp

Give it a try

If this is done just restart everything:

service tomcat7 restart
service apache2 restart

Now Apache forwards all requests to http://yourapp.yourserver.tld to your Tomcat webapp at http://yourserver.tld:8080/YourApp .


Martin Scharm

stuff. just for the records.

Do you like this page?
You can actively support me!

4 comments

Michael Rennecke | Permalink |

I use mod_proxy for it and configure a reverse proxy. It works fine with Tomcat 6 and Apache, Nginx and HAProxy. If Tomcat talks to Apache via HTTP instead of AJP Connector, then there are some limitations like no domain model clustering and no large packages (>8k)

Nick | Permalink |

Thanks – I was missing the ajp connector def in mine and the JkMount directive also I had way too much in my workers.properties. So now mission complete : LAMP on raspberry pi …

Nick

Simon | Permalink |

Hi, this is JUST what I was looking for, and had great hopes I could finally finish my Apache/Tomcat set up, but despite following your tutorial to the letter (well, almost) I still get an error when I browse to http://web.me.local/sample/ (I didn’t use the Rewrite rule). It is a -

Service Unavailable The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

…. error.

Any clues? (I have left a connector in my server.xml for port 8080, and I can get to my site using web.me.local:8080/sample/ with no problems)

Thanks!

P.S. Love the anti spam thing!

Simon | Permalink |

Hi, this is JUST what I was looking for, and had great hopes I could finally finish my Apache/Tomcat set up, but despite following your tutorial to the letter (well, almost) I still get an error when I browse to http://web.me.local/sample/ (I didn’t use the Rewrite rule). It is a -

Service Unavailable The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

…. error.

Any clues? (I have left a connector in my server.xml for port 8080, and I can get to my site using web.me.local:8080/sample/ with no problems)

Thanks!

P.S. Love the anti spam thing!

Leave a comment

There are multiple options to leave a comment: