Tuesday 2 February 2016

Configuring Apache in front of JBoss Application Server Using mod_jk

This article provides a step by step guide on configuring Apache HTTP server as the Web server in front of the JBoss application server.

Apache servers balances the load among the JBoss applications servers via the mod_jk connector. The Jboss servers are configured in a cluster and hence when a server goes down, the subsequent user requests can be forwarded to a different server.

Why to Configure Apache HTTP server in front of JBoss Application Server?
  • Application servers are good at hosting Web applications, but they are not as good when it comes to serving static content, providing load balancing, URL rewriting, security etc. Following are some of the advantages of using Apache HTTP web server in front of an application server such as JBoss.
  • Apache is a high performance and highly secure Web server with advanced features
  • Apache comes with a lot of extension modules providing essential hosting features such as logging, virtual hosting, URL rewriting etc.
  • Apache can be used as a load balancer distributing load across multiple JBoss instances. Mod_jk connector supports advanced load balancing configurations.
  • A single Apache server instance can serve multiple domains using virtual hosting and at the same time they can all be serviced by different JBoss servers. This ensures process isolation for web applications hosted in each domain.
Apache to JBoss Request Routing – How It Works?

There are two different ways of configuring Apache to JBoss routing. One is to use the Tomcat connector available for Apache called the mod_jk connector. This is an Apache module specifically written by Tomcat team. This module routes the Apache to JBoss request via the AJP (Apache Jserv Protocol).

Second method is to use an Apache module called mod_proxy. This proxy can either use HTTP or AJP for routing request from Apache to JBoss. The advantage of mod_proxy is that it is available as a built in module in Apache 2.x versions.

When it comes to load balancing and failure detection, mod_jk is better than mod_proxy. The only disadvantage is the need to separately deploy  mod_jk module.

In either case, if the Apache connector is using AJP protocol, the AJP protocol listener must be enabled on the JBoss server. By default JBoss binds AJP service to the port 8009. Whenever a request is received by Apache, it looks at the AJP module configuration file(workers.properties) and if the request is intended for the JBoss server, the request is routed through AJP protocol to the port 8009(Please see the diagram above).


Configuring Apache to JBoss HTTP Request Routing Using Mod_Jk
  • The mod_jk HTTP connector has a single component, the mod_jk.so module loaded by the web server. This module receives client requests and forwards them to the container, in this case JBoss EAP 6. JBoss EAP 6 must also be configured to accept these requests and send replies back to the web server.
  • In order for JBoss EAP 6 to be able to communicate with the Apache HTTP server, it must have the AJP/1.3 connector enabled
  • In a managed domain, in server groups using the ha and full-ha profiles, and the ha or full-ha socket binding group. The other-server-group server group is configured correctly in a default installation.
  • In a standalone server, the standalone-ha and standalone-full-ha profiles are configured for clustered configurations.

To configure mod_jk integration, Download tomcat-connectors from the Tomcat site. 

[jboss@middlewaretech mod_jk]$ tar -zxvf tomcat-connectors-1.2.32-src.tar.gz
[jboss@middlewaretech mod_jk]$ cd tomcat-connectors-1.2.32-src/native/
[jboss@middlewaretech native]$ ./configure --with-apxs=/u03/local/opt/httpd-2.2.31/bin/apxs --enable-api-compatibility

Here /u03/local/opt/httpd-2.2.31/ is HTTP_HOME directory

[jboss@middlewaretech native]$ make
[jboss@middlewaretech native]$ make install


after completed this activity you will get mod_jk.so file in /u03/local/opt/httpd-2.2.31/modules/mod_jk.so

[jboss@middlewaretech modules]$cd /u03/local/opt/httpd-2.2.31/modules

[jboss@middlewaretech modules]$ ls -ltr
-rwxrwxr-x 1 jboss dba 878259 Feb  2 02:35 mod_jk.so

 if get it , going well
Installation part has been completed, let's start configuration part

Now create a new file named mod-jk.conf inside apache/conf folder. Add the following content to this file. 

LoadModule jk_module /u03/local/opt/httpd-2.2.31/modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkShmFile logs/mod_jk.shm
JkLogFile logs/mod_jk.log
JkLogLevel info
JkMount /app/* server1
JkMount /app/* server2

The directive JkMount here ensures that all HTTP requests with context /app/ are forwarded to the server1 & server2 application server configured in workers.properties (see below).
Open httpd.conf in apache/conf folder and add the following line at the end of the file. This enables the mod-jk.conf file as an extension to the default configuration file.

Include conf/mod-jk.conf

Create a new file workers.properties inside apache/conf folder. Add the following content to the file. Note that the name of the JBoss node (server1, server2) corresponds to the JkMount configuration in mod-jk.conf file. The host in this case is given as 192.168.1.12 since I have the JBoss running on the same machine. In actual deployments replace 192.168.1.12 with the ip address/host name of the JBoss machine.

worker.list=server1,server2

# Define server1
# modify the host as your port & host IP or DNS name.
worker.server1.port=8089
worker.server1.host=192.168.1.12
worker.server1.type=ajp13

# Define server2
# modify the host as your  port & host IP or DNS name.
worker.server2.port=8039
worker.server2.host=192.168.1.12
worker.server2.type=ajp13

The above configuration indicates that there are two application server machine available for handling requests in the 192.168.1.12 and it is available through AJP protocol via port 8089 & 8039.
Restart apache and JBoss and access your Web application through Apache server using this 
URL – http:// 192.168.1.12:80/app.


1 comment:

Unknown said...

What is the jboss domain. Xml corresponding change for AJP?