Setting up Apache and Tomcat with mod_jk on Windows

Setting up Apache and Tomcat with mod_jk on Windows

To be honest, I find it a little pointless to be writing this in the first place, as there are already so many guides on the internet on setting up Apache with Tomcat using mod_jk. In fact, there are better ways, such as using mod_proxy which comes bundled with Apache by default. However, some of you may be tasked with supporting legacy applications, such as I am, and could find this useful.

For those who aren’t familiar, mod_jk is an apache module that “links” Apache with Tomcat, allowing Apache to act as the front end, for Java servlets in Tomcat. At its foundation, it’s just a glorified proxy.

Anyway, let’s get started.

Install Java

First, install Java. This is important, as Tomcat can’t run without this. I’m using Java 8 Update 171, but have used Update 162 and earlier without any issue. Because this is self-explanatory, I’m not going to go into any further detail.

Install Tomcat

The Tomcat binaries can be downloaded from here. Tomcat 9 works for my purposes, and I recommend using the latest and greatest. I haven’t tested this process using a previous version, but I assume versions 7 and 8 would work similarly. Run through the installation, selecting all the defaults. We don’t need anything fancy.

tomcat_install

Create a test app

We’re now going to create a super simple Java test app. Navigate to C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\ and create a new folder. We’ll name it test in our case.

Within that folder, create a new file and name it index.jsp. Open that up, and enter the following code:

<%="Test tomcat app."%>

At this point, restart the Tomcat Service from within service.msc, and then open a browser. Navigating to http://localhost:8080/test should result in the following:

tomcat_install

Cool! All that’s working.

Install C++ Redistributable

Prior to installing Apache, you’ll need the C++ Redistributable installed. 2015 worked for my purposes.

Install Apache

Apache doesn’t have a nice Windows installer that you can download (at least not from the Project’s Website itself). Instead, head on over to ApacheHaus, download the ZIP file, and then extract it so C:\Apache24.

Once the files are extracted, install the Windows service by using the following command:

C:\Apache24\bin\httpd.exe -k install

Copy the mod_jk module to your Apache installation

Navigate here and download the correct zip file that correlates to the version of Apache you installed.

Extract the zip file, and copy mod_jk.so to the C:\Apache24\modules folder.

Edit httpd.conf file

Edit the httpd.conf file located at C:\Apache24\conf\httpd.conf and add the following somewhere around line ~140-ish.

LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkShmFile     logs/mod_jk.shm
JkLogFile     logs/mod_jk.log
JkLogLevel    info
JkMount  /test/* worker1
LoadModule mime_module modules/mod_mime.so

Create workers.properties file

Your workers.properties file should reside in the same conf folder as httpd.conf and should contain the following:

worker.list=worker1

#Set properties for worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.socket_keepalive=True
worker.worker1.connection_pool_timeout=600

Reboot

Reboot (or just restart both the Tomcat9 and Apache services), and then navigate to http://localhost/test in your browser.

If you see the same results in your browser as the screenshot above, congrats! You’re all done. Apache is now handing off the requests to tomcat. You know this, because you’re no longer hitting port 8080. You’re hitting port 80, which is then proxied to Tomcat. For each subsequent app you want to add, simply create a new folder in the webapps directory, and then add a new JkMount entry in your httpd.conf file.

Leave a Reply

Your email address will not be published. Required fields are marked *