čtvrtek 19. června 2014

How to install (manually) and configure Apache HTTP server in front of Tomcat (on Windows platform)


Hi!

I decided to write this post about installing the Apache HTTP server in front of Tomcat. I believe that this information will be useful for your server installation. Note that most of the informations I got from the documentation Apache HTTP server and Apache Tomcat.

What is the motivation for intalling Apache HTTP server in front of Tomcat? Tomcat standalone is comparable with performance to the native web servers. So why?

By this document the reasons for such configuration can be:
  • We want different contexts to be served by different Tomcat workers to provide a development environment where all the developers share the same web server but own a Tomcat worker of their own.
  • We want different virtual hosts served by different Tomcat processes to provide a clear separation between sites belonging to different companies.
  • We want to provide load balancing, meaning run multiple Tomcat workers each on a machine of its own and distribute the requests between them.

This is illustrated by the picture from people.apache.org:





A few facts to start: We use operation system Microsoft Windows Server 2008 and will install and configure following apps:


Step by step we describe this installation and configuration. Please note that all installations we will make manually without msi files and so on. It's much more better for understanding "how it works".

1. Apache Tomcat instalation


- download and unzip x64 version of Apache Tomcat 7 to folder "C:\Tomcat"

- we skip detailed configuration of Tomcat as connectors, ports, Tomcat users and so on a keep default settings

- manually install Tomcat as windows service, default name service is "Tomcat7". If we want, we can edit the file "service.bat" and set properties like Java Heap Size, path to JDK and so on:

       

C:\Tomcat\bin>service.bat install


- start Tomcat7 service from command line:


       

C:\>net start Tomcat7


- when we would like to monitor Tomcat, so we can run a GUI application for monitoring and configuring Tomcat services as service:

       

C:\Tomcat\bin>tomcat7w.exe //MS//Tomcat7



2. Apache HTTP server instalation


- download and unzip Apache HTTP server version 2.4.9 for x64 to folder "C:/Apache24". Because of using MS Windows server there are few options where download Apache HTTP server. Server Apache Lounge has provided up-to-date Windows binaries and popular third-party modules.

- if we use Apache Lounge windows binaries it's necessary to download and install required Visual C++ Redistributable for Visual Studio 2012 : VC11 vcredist_x64/86.exe

- to quick configuration we must edit the file http.conf and set (uncomment) line as ServerName, ServerRoot and ServerDocs:

       
# set your IP address
ServerName xxx.xxx.xxx.xxx:80
ServerRoot "C:/Apache24"
DocumentRoot "C:/www"


- next step is test running server by command:

       

C:/Apache24/bin>httpd.exe



- if there are no errors on startup server, it means everthing is ok, so we can install Apache as windows service:

       

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


- to monitoring Apache server we can run

       

C:/Apache24/bin>ApacheMonitoring.exe



3. Set Apache HTTP server in front of Tomcat


- to connect Tomcat to the web server Apache, we can use mod_jk (= Tomcat redirector module) described in detail here

- a Tomcat worker is a Tomcat instance that is waiting to execute servlets on behalf of some web server

- we use worker type ajp13 - this worker knows how to forward requests to out-of-process Tomcat workers using the ajpv13 protocol

- first we must install mod_jk module/library to Apache server, it means copy library mod_jk.so to folder Apache/modules, in our example to "C:/Apache24/modules"

- then we edit http.conf in Apache and alow this module by adding this line:

       

# Load mod_jk module
LoadModule jk_module modules/mod_jk.so



- basic simple configuration JK module is is the following:

       

# Where to find workers.properties
JkWorkersFile conf/workers.properties

# Where to put jk logs
JkLogFile logs/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# Send servlet for context /examples to worker named worker1
JkMount /my_app/* worker1



- in the previous step we set path to file workers.properties, where Tomcat workers are defined. In this file we configure Tomcat workers = Tomcat instances that are waiting to execute servlets or any other content on behalf of some web server desribed in detail here

- so we create file workers.properties in Apache24 conf folder, in our case "C:/Apache24/conf":

       

worker.list=worker1
worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13
worker.worker1.mount=/my_app/*



- it's DONE :-).

4. Testing all configuration


- in the end we must restart Apache HTTP server to apply changes and now we can test all configuration. Apache HTTP server should forward servlet requests to a Tomcat process (the worker) running behind it.

       

application that is running on Tomcat server on 8080 port:
http://server.com:8080/my_app

now is this app accessible through Apache HTTP on port 80 as:
http://server.com/my_app



Good luck,
Radek