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"
          path="/
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.
ServerName www.nirav.com:80

<VirtualHost *:80>
    ServerName www.nirav.com
    ServerAlias nirav.com
   
   
<Proxy balancer://mycluster>
        BalancerMember http://localhost:8080/ route=tomcat1
        BalancerMember http://localhost:8081/ route=tomcat2
        ProxySet lbmethod=byrequests
    </Proxy>
    ProxyPass / balancer://mycluster/
</VirtualHost> 


_____________________________________________________________

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/mod_lbmethod_byrequests.so

Some others modules also need to be un-comment as shown below.
  • LoadModule proxy_module modules/mod_proxy.so
  • LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
  • LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
  • LoadModule proxy_connect_module modules/mod_proxy_connect.so
  • LoadModule proxy_express_module modules/mod_proxy_express.so
  • LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
  • LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
  • LoadModule proxy_html_module modules/mod_proxy_html.so
  • LoadModule proxy_http_module modules/mod_proxy_http.so
  • LoadModule proxy_scgi_module modules/mod_proxy_scgi.so  

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 www.nirav.com 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 www.nirav.com/SampleWeb 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..!

No comments:

Post a Comment