Follow Nirav Raval

Sunday, 12 July 2015

Configure Apache 2.4 Web Server with Tomcat 8.0 as a Balancer to run the Java Web Application

Hello Friends,
Today I am gonna show you the Apache Web Server version 2.4 to act as mediator to handle the application load by dividing it into more than one Application Servers.

Here I am using Tomcat 8 as an Application Server to run a Java Based Web application.

I have configure this in Windows 8 64 bit Operating System.

I assume that you have done the tomcat server configuration to set context outside webapps folder of it.
For that write your context name and path in server.xml file of tomcat server as below.

This must be written under <Host> tag.
<Context  docBase="G:\Deploy\SampleWeb"
          reloadable="true" />

To do this, follow below steps.
  • Download Apache 2.x from apache site.
  • Download Apache Tomcat 8 also.
  • Install Apache web server on your system.
  • Make two copy of tomcat and  set context of web application as shown above.
  • Both tomcat must set on different port.
  • I have set one Tomcat on 8080 and second on 8081 in <Connector> port in  Server.xml file. 
  • Now open httpd file of Apache web server from conf folder and add below configuration in it.

<VirtualHost *:80>
<Proxy balancer://mycluster>
        BalancerMember http://localhost:8080/ route=tomcat1
        BalancerMember http://localhost:8081/ route=tomcat2
        ProxySet lbmethod=byrequests
    ProxyPass / balancer://mycluster/


In above configuration, I have used byrequest method for load balancing. 
This means the load of requests is balanced based on each request i.e. First request served by tomcat 1, then second will be served by tomcat 2, then third will be served by tomcat 1 and so on.

To activate this you need to un-comment below module in httpd file:
LoadModule lbmethod_byrequests_module modules/

Some others modules also need to be un-comment as shown below.
  • LoadModule proxy_module modules/
  • LoadModule proxy_ajp_module modules/
  • LoadModule proxy_balancer_module modules/
  • LoadModule proxy_connect_module modules/
  • LoadModule proxy_express_module modules/
  • LoadModule proxy_fcgi_module modules/
  • LoadModule proxy_ftp_module modules/
  • LoadModule proxy_html_module modules/
  • LoadModule proxy_http_module modules/
  • LoadModule proxy_scgi_module modules/  

After adding above configuration in httpd file of Apache server, just restart apache server. Then start both tomcats. 

If any error shown on restart Apache server then read its log file and act accordingly.The error comes if the configuration is not properly done. Or any required module directive is not added.

I have given almost all modules to make un-comment. 

I have set domain name as in my host file (C:\Windows\System32\Drivers\etc\host)   to make run in local machine. You can use what you want.

Now this done. You can run your application with activated load balancer by Apache web server.

Just need to write in web browser and the request will be handled by two tomcats one by one on each request.

I hope this will helpful to understand the basic configuration for enable balancer in Apache web server.

Here, I have not covered any theory part, you can google it separately.

Thank you..!