Installing Apache 2 and Tomcat 7

Overview

A tomcat instance is installed in a glimpse of an eye: after following a few steps as outlined in the tomcat documentation, it is possible to easily deploy WAR containers using the tomcat manager application. However, often this tomcat instance is configured for port 8080, and therewith not quite ready for final deployment.

While it is easy to change the port on which tomcat is hosted to the default port 80, it is not a recommended practice to manage a web server entirely using a tomcat instance. Luckily, it is relatively easy to set up an Apache web server as the ‘main’ web server and to let this server forward particular requests to a tomcat instance.

To forward particular requests from an Apache instance to a tomcat instance, a number of steps need to be performed:

  • Apache needs to be installed
  • Tomcat needs to be installed (on the same machine)
  • A ‘virtual host’ needs to be configured in Apache. Such a virtual host can be used to forward request directed to a particular domain name to an apache site. An apache site can contain static and dynamic web content in from of html files, php files or cgi script, among others. However, using a specific connector an apache ‘site’ can also point to a tomcat server.
  • On this tomcat server, it must be specified, which web applications should be served in response to the requests send by the Apache server.

In the following, all of these steps are described in some detail. In particular, it is discussed how an instance of Apache 2 can be connected to an instance of Tomcat 7. In the example given here, all applications of the tomcat server are mapped to a virtual host of an apache server (you could also configure only a single application (WAR) to be mapped to a domain of your apache server).

Installing Apache 2 in Ubuntu

Since apache is part of the Ubuntu package it can be installed using the command:

sudo apt-get install apache2

Installing Tomcat 7 in Ubuntu

At the time of this writing, Tomcat 7 cannot be installed using apt-get. Instead, the package for Tomcat 7 must be installed manually. The instructions on Diego Benna’s blog for conducting this process are excellent (also work for Ubuntu 9.10). Also, double check with Sebastian Mogilowskis’ blog post (steps 1-6).

Install Apache-Tomcat connector

In order to forward requests from Apache to Tomcat, an Apache 2 connector must be installed. This can also be achieved using aptitude:

sudo apt-get libapache2-mod-jk

Also configure the Apache wokrker and JK configuration file as shown in Sebastian Mogilowskis’ blog post (steps 7).

Setting up virtual host in Apache 2

A virtual host in Apache 2 can be set up in a number of simple steps. The created virtual host will provide access to your tomcat instance at an URL managed by your apache server.

  • Create a new directory under /var/www with the name ‘tomcat’
  • Also create the sub-directory /var/www/tomcat/logs
  • Create a file ‘tomcat’ under /etc/apache2/sites-available tomcat (for instance using sudo nano /etc/apache2/sites-available). Paste the following contents into this file (if you do not want to specify an IP address, replace XX.XX.XX.XX with *):

<virtualhost XX.XX.XX.XX>

JkMount /* default

ServerName http://www.yourserver.com

ServerAdmin servermaster@yourserver.com

DocumentRoot /usr/share/tomcat7/webapps

ErrorLog /var/www/tomcat/logs/error.log

CustomLog /var/www/tomcat/logs/access.log common

</virtualhost>

  • Replace XX.XX.XX.XX with the IP address of your server
  • Replace http://www.yourserver.com with the url you want the virtual server to persist on (you will need control of the DNS entries for this url. You will need to point them to your server IP address)
  • Enable the new site using ‘sudo a2ensite tomcat’
  • Reload the apache instance using ‘/etc/init.d/apache2 reload’ and ‘/etc/init.d/apache2 restart’

Setting up virtual host in Tomcat 7

In order for tomcat to react to the forwarded requests from the Apache server, a host needs to be configured in tomcat’s server.xml. This can be achieved through the following steps:

  • Add the following <host> element to the server.xml (eg in /usr/share/tomcat7/conf):

<Host name=”http://www.yourserver.com” appBase=”webapps” unpackWARs=”true” autoDeploy=”true”>

<Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”

prefix=”yourservercom_access_log.” suffix=”.txt”

pattern=”%h %l %u %t &quot;%r&quot; %s %b” resolveHosts=”false”/>

</Host>

  • Replace http://www.yourserver.com with the domain name on which your tomcat server should be hosted
  • Restart tomcat using ‘/etc/init.d/tomcat7 restart’

Conclusion

After these steps you should be able to access all the web applications registered in your tomcat servers webapps folder through the domain name you have specified.

E.g. the tomcat manager application under http://www.yourserver.com/manager

Test Environment

Ubuntu 9.10

Tomcat 7.0.12

Resources

Blog post “Install Tomcat 7 on Debian (Lenny) with virtual hosts and Apache2 integration”

Diego Benna’s Blog “Connect Tomcat 7 with Apache2 (mod_jk) and install Virtual Host”

Diego Benna’s Blog “Install Tomcat 7 on Ubuntu 10.10 and Ubuntu 10.04

Apache 2 Documentation Virtual Hosts

13 thoughts on “Installing Apache 2 and Tomcat 7

  1. Pingback: Nguni Phakela's Blog » Blog Archive » Getting Tomcat 7 to talk to Apache 2

  2. You said
    “Create a file ‘tomcat’ under /var/www/tomcat (for instance using sudo nano /var/www/tomcat/tomcat). ”

    I doubt that it should be in “/etc/apache2/sites-available/” .
    Can you please confirm this?

    Reply
    • Hi, Sorry but I believe the process will be quite different to the one described here. Since you probably won’t use Apache. But its quite easy to install Tomcat standalone on Windows using the provided installers. Max

      Reply
  3. I have managed to get tomcat setup and processing requests but I would like to only forward ,do and .jsp requests to tomcat and have static content (jpg, css etc) processed by apache.

    How is this achieved?

    I have modified apache mod_jk config as follows (abode is webapp name)

    JkMount /*.jsp ajp13
    JkMount /*.do ajp13

    but apache does not serve up static content

    Reply
  4. This post is very helpful.your instruction says that tomcat has to be installed in the same machince as that of the apache web server. Does this instruction work with tomcat being remote to the apache web server on different machince?
    Can you please post the changes to the instructions that needs to be done if the servers are remote

    Reply
    • The here instructions don’t work if Apache / Tomcat are on different machines since they are based on a local connector. Probably configuring some kind of a proxy to forward a certain url pattern from the Apache server to the Tomcat server might be the solution. However, performance will of course be far worse than using the local connector as described here.

      Reply
  5. Pingback: Tomcat hinter einem Webserver » Promachos

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s