Close

How to setup Apache 2.4 to forward some URLs to Tomcat 8 on Ubuntu 16.04?

[Last Updated: Apr 5, 2017]

Server Software Ubuntu Linux 

This is a quick walk-through for setting up Apache server 2.4 to use with Tomcat 8 via Apache mod_jk. We will probably want to do this, because we want Apache server to serve static content and forward some of the URLs to Tomcat for dynamic content. In this guide, we will not cover in-depth concepts as there are plenty of pages describing them on the internet.

First install Java 8.

Then install Tomcat 8

If we haven't installed Apache web server yet, we have to do so now:

$ sudo apt-get install apache2

Apache2 server should be running now:

$ systemctl status apache2

We can access the default page of Apache server at http://server_domain_or_IP

Now, we are going to setup Apache server to forward some of our URLs to Tomcat.

Install mod_jk:

 $ sudo apt-get install libapache2-mod-jk

We need to know workers.properties location:

$ more /etc/apache2/mods-available/jk.conf

Look for JkWorkersFile entry. I found this:

 JkWorkersFile /etc/libapache2-mod-jk/workers.properties

Edit the above properties file:

$ sudo nano /etc/libapache2-mod-jk/workers.properties

set the followings

 ....
workers.tomcat_home=/var/lib/tomcat8
 ....
workers.java_home=/usr/lib/jvm/java-8-openjdk-amd64/jre

In above settings, your Tomcat home might be different than '/var/lib/tomcat8' depending on your Tomcat installation.

Also make sure you have similar lines:

 ....
worker.list=ajp13_worker
 ....
worker.ajp13_worker.port=8009
worker.ajp13_worker.host=localhost
worker.ajp13_worker.type=ajp13
 ....
worker.ajp13_worker.lbfactor=1
 ....

Remember 'worker.list=ajp13_worker', we will be using it shortly.

Save and exit.

Now find 000-default.conf:

$ ls /etc/apache2/sites-available/
000-default.conf  default-ssl.conf
 $ sudo nano /etc/apache2/sites-available/000-default.conf

Add JkMount mapping:

 <VirtualHost *:80>
   .....
   .....
   JkMount /manager/* ajp13_worker
   ....
 </VirtualHost>

In above mapping, we are specifying that the url pattern '/manager/*' will be forwarded to Tomcat. Similarly, we can map any URL pattern mapped to Tomcat server. Also 'ajp23_worker' is same as 'worker.list' value, we found in the last step.

Now we need one more modification in tomcat server.xml (/var/lib/tomcat8/ is Tomcat home location):

$ sudo nano /var/lib/tomcat8/conf/server.xml

Following line should be uncommented:

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

Save and exit.

That's it. Restart Tomcat and Apache2:

sudo systemctl restart tomcat8
sudo systemctl restart apache2

We can access apache default page at, http://server_domain_or_IP

We can also access Tomcat manager page at, http://server_domain_or_IP/manager/