Wednesday, March 17, 2010

Setting up Apache and Tomcat for web application deployment

Most of the web application in today's era, Apache and Tomcat is one of the preferred combination to deploy web application.

This combination is used to serve Static content like JS, CSS, HTML etc. from Apache and the dynamic content from Tomcat.

The question arises that since tomcat has its own web server, then why to have a separate server to serve static HTTP content. There are few reasons which are mentioned below for doing the same:
1. It's recommended in production envirronment for directly exposing the Tomcat server to outside world.
2. Since the task is divided between the two servers, its faster.

As mentioned on Tomcat wiki FAQ site:
"Why should I integrate Apache with Tomcat? (or not)
There are many reasons to integrate Tomcat with Apache. And there are reasons why it should not be done too. Needless to say, everyone will disagree with the opinions here. With the performance of Tomcat 5 and 6, performance reasons become harder to justify. So here are the issues to discuss in integrating vs not.
  • Clustering. By using Apache as a front end you can let Apache act as a front door to your content to multiple Tomcat instances. If one of your Tomcats fails, Apache ignores it and your Sysadmin can sleep through the night. This point could be ignored if you use a hardware loadbalancer and Tomcat's clustering capabilities.
  • Clustering/Security. You can also use Apache as a front door to different Tomcats for different URL namespaces (/app1/, /app2/, /app3/, or virtual hosts). The Tomcats can then be each in a protected area and from a security point of view, you only need to worry about the Apache server. Essentially, Apache becomes a smart proxy server.
  • Security. This topic can sway one either way. Java has the security manager while Apache has a larger mindshare and more tricks with respect to security. I won't go into this in more detail, but let Google be your friend. Depending on your scenario, one might be better than the other. But also keep in mind, if you run Apache with Tomcat - you have two systems to defend, not one.
  • Add-ons. Adding on CGI, perl, PHP is very natural to Apache. Its slower and more of a kludge for Tomcat. Apache also has hundreds of modules that can be plugged in at will. Tomcat can have this ability, but the code hasn't been written yet.
  • Decorators! With Apache in front of Tomcat, you can perform any number of decorators that Tomcat doesn't support or doesn't have the immediate code support. For example, mod_headers, mod_rewrite, and mod_alias could be written for Tomcat, but why reinvent the wheel when Apache has done it so well?
  • Speed. Apache is faster at serving static content than Tomcat. But unless you have a high traffic site, this point is useless. But in some scenarios, tomcat can be faster than Apache httpd. So benchmark YOUR site. Tomcat can perform at httpd speeds when using the proper connector (APR with sendFile enabled). Speed should not be considered a factor when choosing between Apache httpd and Tomcat
  • Socket handling/system stability. Apache has better socket handling with respect to error conditions than Tomcat. The main reason is Tomcat must perform all its socket handling via the JVM which needs to be cross platform. The problem is socket optimization is a platform specific ordeal. Most of the time the java code is fine, but when you are also bombarded with dropped connections, invalid packets, invalid requests from invalid IP's, Apache does a better job at dropping these error conditions than JVM based program. (YMMV)"

Installation of Apache
Make sure that Apache 2.2.X server is installed on your machine with so enabled. The quick information on installing Apache is:
1. Download Apache 2.2.X from Apache site http://httpd.apache.org/download.cgi
2. gunzip -d httpd-2.2.9.tar.gz
3. tar -xvf httpd-2.2.9.tar
4. ./configure --enable-so
Also you can include various options while configuring apache. For example to support mod_proxy, mod_ssl module append below mentioned configuration settings to the configure command:


--enable-rewrite --enable-ssl --enable-proxy 

The most commonly used modules are configured using below mentioned command.



./configure --enable-so --enable-rewrite --enable-ssl --enable-proxy --enable-mem-cache 

5. make
6. make install
7. cd /usr/local/apache2/bin
8. ./apachectl start



This will install Apache in your /usr/local/apache2 folder. Verify the installation by opening http://localhost:<PORT> url.



In case you need to change the port where Apache is listening, go to <APACHE_HOME>/conf/httpd.conf file and update the "Listen 80" port statement with port number where you want Apache to run on.

Installation of Tomcat 6.X
1. Download Tomcat 6.X from Apache site http://tomcat.apache.org/download-55.cgi
2. Untar the file and, you are ready to go.
3. cd <TOMCAT_HOME>/bin
4. ./startup.sh

By default tomcat will be running on 8080 port. The same can be changed by going to <TOMCAT_HOME>/conf/server.xml file and changing the below mentioned statement with the appropriate port number:
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8" />

Verify the installation by opening http://localhost:<PORT> URL.

Installation of mod_jk connector

mod_jk connector is used for connecting Tomcat to Apache server. It comes as a separate file and not compiled in Apache server. Download mod_jk connector from Apache site  http://tomcat.apache.org/download-connectors.cgi

Stop the Apache server using ./apachectl stop command inside <APACHE_HOME>/bin folder.

Rename the downloaded file to mod_jk.so and copy it inside <APACHE_HOME>/modules folder. Now you know why we required to configure the Apache server with "so" enabled option.

Now create a new file called workers.properties inside  <APACHE_HOME>/conf folder. The content of the workers.properties file is:

worker.list=worker1
#set properties for worker1
worker.worker1.type=ajp13
worker.worker1.host=<HOST_NAME_TOMCAT>
worker.worker1.port=<AJP_PORT_OF_TOMCAT>

Replace the <HOST_NAME_TOMCAT> with the hostname/ip address where the Tomcat is running. and change the <AJP_PORT_OF_TOMCAT> to the AJP connector port of tomcat. Its mentioned inside <TOMCAT_HOME>/conf/server.xml. By default its value is 8009.


The next step is to configure the Apache server for mod_jk connector. Open <APACHE_HOME>/conf/httpd.conf file.

After the "Listen 80" line, copy the below mentioned content over there.

LoadModule jk_module modules/mod_jk.so
#LoadModule rsp_module modules/mod_rsp22.so

JkWorkersFile /usr/local/apache2/conf/workers.properties
JkLogFile /usr/local/apache2/logs/mod_jk.log
JkShmFile /usr/local/apache2/logs/mod_jk.shm
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
JkOptions     +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMount /* worker1
JkUnMount /img/* worker1

The last line means that all the requests coming to Apache server will be routed to Tomcat server which is mentioned at worker1 inside workers.properties file. This can be changed to serve different files from different workers. As the name suggests, JkUnMount command can be used to serve the particular form of URL from Apache instead of Tomcat server.

Other Installation related Posts

No comments:

Post a Comment

Share it