JavaTechie

Its all about Technology

Apache 2.x + Tomcat 4.x + Load Balancing February 12, 2009

Filed under: Apache, Java — javatechie @ 10:41 am
Tags: , ,

This article contains step by step instructions for configuring an Apache 2.x web server which handles static content and delegates JSP (Java Server Pages) and Servlet requests to two Tomcat 4.x servers using AJP 13 connectors and a load balancing worker.

Introduction

Apache 2.0 is a standards compliant, fast and mature web server which excels at delivering static content such as static HTML pages and images. The Tomcat web server is great for serving Java Server Pages and servlets, but it is not as fast as Apache for delivering static content.

In order to build a fast, scalable web application, the requirements call for an Apache server that delegates servicing of JSP and servlet requests to multiple tomcat servers by using an Apache module, mod_jk, that performs load balancing with session affinity, also known as “sticky” sessions.

Session affinity explained. When a client browser requests a JSP page for the first time, the load balancer redirects the request received by Apache to one of the two tomcat servers; further requests originating from the same client session will be automatically forwarded to the same tomcat server, so that the user’s session data is retrieved.

This document describes how I configured Apache 2.x to dispatch JSP and servlet requests to two Tomcat 4.x instances listening on different ports. This setup was done on a Linux system. Your mileage may vary.

Read more

 

How to Remove jsessionid from URL December 31, 2008

Filed under: Java — javatechie @ 9:05 am
Tags: ,

This is standard J2EE behavior.  When you start a HTTP session, the
session ID is appended to the URL and a cookie is set.  If the browser
has cookies enabled, the JSESSIONID is not used on subsequent requests.

Here is a urlrewrite rule to get rid of it, substitute JSESSIONID for
the name of the parameter your app server uses to track sessions.

<outbound-rule encodefirst=”true”>
<name>Strip URL Session ID’s</name>
<from>^(.*?)(?:\;JSESSIONID=[^\?#]*)?(\?[^#]*)?(#.*)?
$</from>
<to encode=”false”>$1$2$3</to>
</outbound-rule>

Just be aware that removing the session id will make sessions not work
for clients that do not support cookies.

 

How can you set the session timeout in tomcat higher? July 4, 2008

Filed under: Java — javatechie @ 12:02 pm
Tags: ,

You can specify the timeout of a session in the deployment descriptor of your web application (web.xml):

<web-app>
  <session-config>
    <session-timeout>60</session-timeout>
  </session-config>

  ...
</web-app>

The number within the session-timout element must be expressed in minutes.

 

Integrating Apache 1.3.x and Tomcat 4.x with mod_jk on Linux July 2, 2008

Filed under: Apache, Java — javatechie @ 11:18 am
Tags: , ,

This FlashGuideTM covers integrating Apache 1.3.x and Tomcat 4.x on Unix via mod_jk. These instructions have been tested on SuSE 9.0, Red Hat 6.2 and Fedora Core 2. There are two ways to integrate Apache and Tomcat: mod_jk and mod_jk2. Mod_jk is the older but more stable version, which supports load balancing and non-standard web application locations. Mod_jk2 is newer, has bugs, and, as of 11/15/04, is no longer being actively developed.

These instructions are for the current version of Tomcat 4.1.31, but have worked the same for all previous versions of Tomcat.

If you are using an older version of Tomcat, note that there are known bugs when using mod_jk and Tomcat 4.0.1 or 4.0.2, so you must use 4.0.3 or greater.

1. Building Apache 1.3.x on Linux

Unless you can find a binary distribution of Apache with DSO support enabled, you will have to follow these instructions to build it yourself.

1. Check your prerequisites:

1. You will need GCC installed
2. You will need /usr/ccs/bin and the gcc executables in your $PATH

2. Download the latest Apache source from http://httpd.apache.org/download.cgi – currently, the latest is 1.3.33.
3. Unpack the distribution into a development directory (I used /usr/local)

The distribution directory will be something like apache_1.3.33
4. Cd into the distribution directory (e.g. /usr/local/apache_1.3.33)
5. Configure the makefile:

./configure –with-layout=Apache –prefix=/usr/local/apache –enable-rule=SHARED_CORE –enable-module=so

Read More

 

Multiple Instances of Tomcat with Apache January 7, 2008

Filed under: Java — javatechie @ 6:28 am
Tags: ,

Learn how to use mod_jk to forward requests to specific hosts when more than one Tomcat instance is running.

Recently, I was asked to reorganize some of our Web applications to improve their stability. The major push was to get each of our applications running in its own instance of Tomcat. These applications all are in various stages of development, and if a single instance allocates all of the memory available, the entire Tomcat server must be restarted. This, in turn, brings down all of the other applications.
(more…)

 

Tomcat 5 on Fedora Core 6: In Five Easy Steps January 7, 2008

Filed under: Java — javatechie @ 6:25 am
Tags: , ,

Install FC6 and Apache Web Server

Stick the Fedora Core 6 DVD in and reboot. Then follow the instructions. I took all defaults where I can, including letting it “delete all Linux partitions” and automatically partition the disk. I choose to install “Web Server” and “Development Tools” when offered the chance. This installs the Apache Web Server 2.2.3 on the box.

Install Tomcat

Tomcat 5 is included in Fedora Core 6, but not installed by default. So I have to bring it in from the repository:

[root@root]# yum install tomcat5 tomcat5-webapps tomcat5-admin-webapps

This installs Tomcat 5.5.17 and a lot of their dependencies onto the system.

Read more

 

Virtual Hosting with Tomcat January 7, 2008

Filed under: Apache — javatechie @ 6:04 am
Tags: , , ,

This is a guide on setting up Tomcat to do virtual hosting and make it behave like a simple webserver with jsp and servlet support, for many different sites all hosted on the same IP address. The aim is to have a single directory for each virtual host, which can be manipulated individually without hassles from managing multiple .war files and other configuration difficulties.

To configure Tomcat for a virtual host, you need a directive in the server.xml file, and a ROOT.xml file in the conf/Catalina/$host directory. Here’s the minimal setup required for a copy of Tomcat serving directly on Port 80, using no connectors or other configuration difficulties.

This was written for Tomcat 5 on linux, with Tomcat installed in /usr/local/tomcat

We start with the simplest configuration, of one website, called ‘localhost’ which keeps it’s files in /usr/local/tomcat/webapps/localhost . We’re not using any .war files here – all the files are placed straight into the directory.

Read more

 

Tomcat 5.5.17, with mod_jk on Windows January 7, 2008

Filed under: Uncategorized — javatechie @ 5:43 am
Tags:

Requirements

  • Java 1.5 JDK (SDK)
  • Apache 2.0
  • mod_jk 1.2

Download

Configuration

  • Java JDK path : C:\jdk1.5.0_07
  • Apache path : C:\www\Apache2
  • Tomcat path : C:\www\tomcat5
  • Apache’s webroot : C:\www\webroot
  • JSP/Servlet webroot (via Apache2/mod_jk) : C:\www\tomcat5\webapps
  • JSP/Servlet webroot (via native Tomcat server, port 8080) : C:\www\tomcat5\webapps
  • Access JSP/Servlet pages via Apache2/mod_jk : http://localhost/[jkmount-mapped url of your webapp]
  • Access native Tomcat server : http://localhost:8080/

Unpack and place Tomcat5 and mod_jk

  • Unpack file apache-tomcat-5.5.17.tar.gz under directory C:\www
  • Rename directory C:\www\apache-tomcat-5.5.17 to C:\www\tomcat5
  • Rename file mod_jk-apache-2.x.xx.so to mod_jk.so, and place under directory C:\www\Apache2\modules

Configuration and Setup

  • Set Environmental Variables : JAVA_HOME=C:\jdk1.5.0_07 and CATALINA_HOME=C:\www\tomcat5
    • Set variables under Start » Settings » Control Panel » System » Advanced » Environment Variables » System variables » New…
  • Create empty file C:\www\Apache2\conf\workers.properties, insert code…

workers.tomcat_home=C:/www/tomcat5workers.java_home=C:/jdk1.5.0_07ps=\

# Define worker 'example'worker.list=example

# Set properties for worker 'example' (ajp13)worker.example.type=ajp13worker.example.host=localhostworker.example.port=8009

worker.example.cachesize=10worker.example.cache_timeout=600worker.example.socket_keepalive=1worker.example.recycle_timeout=300

Edit file C:\www\Apache2\conf\httpd.conf, insert code...

LoadModule jk_module modules/mod_jk.so

<ifmodule>

  JkWorkersFile conf/workers.properties  JkLogFile logs/mod_jk.log  JkLogLevel error  JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "  JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories  JkRequestLogFormat "%w %V %T"

  Alias /jsp-examples "C:/www/tomcat5/webapps/jsp-examples/"

  <directory>      Options Indexes +FollowSymLinks      AllowOverride None      Allow from all </directory>

  Alias /servlets-examples "C:/www/tomcat5/webapps/servlets-examples/"

  <directory>      Options Indexes +FollowSymLinks      AllowOverride None      Allow from all  </directory>

 <location>      AllowOverride None      deny from all</location>

  JkMount /jsp-examples/*.jsp example  JkMount /servlets-examples/* example

</ifmodule>

Install and Start Tomcat

Tomcat can be installed as a Service or started as a Standalone Console application. Note to make sure that Apache has been started at this point.


 

Tomcat Configuration for Removing webapps name from URL January 5, 2008

Filed under: Java — javatechie @ 12:32 pm
Tags:

To change Host Name Locally modify hosts file which is located under below specified path and add new hostname whatever you like for testing locally.

C:\WINDOWS\system32\drivers\etc\hosts

To modify URL and remove webapps name from url you have to do below steps.

Create below specified file under specified path and insert that code. This for default localhost application.

conf/Catalina/localhost/ROOT.xml

<?xml version='1.0' encoding='utf-8'?>
<Context displayName="localhost" docBase="" path=""
workDir="work/Catalina/localhost/_">
</Context>

For your custom application you have to add code as specified below.

Where $host is your custom web application.

Add a configuration file for the host

mkdir conf/Catalina/$host
cat >conf/Catalina/$host/ROOT.xml
<?xml version='1.0' encoding='utf-8'?>
<Context displayName="$host" docBase="" path=""
workDir="work/Catalina/$host/_">
</Context>

Main step is to add ROOT folder under your webapps/$host folder like specified below

$tomcatdir/webapps/$host/ROOT/all the files
Next You have to modify server.xml file to add context paths and host
Example:
<Server port="8005" shutdown="SHUTDOWN">
  <!-- Comment these entries out to disable JMX MBeans support used for the
       administration web application -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
  <!-- Global JNDI resources -->
  <GlobalNamingResources>
    <!-- Test entry for demonstration purposes -->
    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
       description="User database that can be updated and saved"
           factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <!-- Define the Tomcat Stand-Alone Service -->
  <Service name="Catalina">
   <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
    <Connector port="8080" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true" />
    <!-- Note : To disable connection timeouts, set connectionTimeout value
     to 0 -->
     <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009"
               enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
    <!-- Define the top level container in our container hierarchy -->
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>
        <Context path="" docBase="webapps/ca"
        debug="5" reloadable="true" crossContext="true">
 <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="username" password="usrpwd" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/beta2?autoReconnect=true"/>
</Context>
<Context path="" docBase="webapps/cl" debug="5" reloadable="true" crossContext="true"></Context>
      <Host name="localhost" appBase="webapps/ca"
       unpackWARs="true" autoDeploy="true"
       xmlValidation="false" xmlNamespaceAware="false">
       </Host>
          <Host name="consumer" debug="0" appBase="C:/tomcat-5.5/webapps/cl" unpackWARs="true"                                    autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
           <Logger className="org.apache.catalina.logger.FileLogger"
               directory="logs"  prefix="consumer_log." suffix=".txt" timestamp="true"/>
                 <Alias>consumer.com</Alias>
        </Host>
    </Engine>
  </Service>
</Server>
For as many applications you have to apply
 

Installing mod_proxy_html January 5, 2008

Filed under: Uncategorized — javatechie @ 5:35 am
Tags:

Download mod_proxy_html.c from here (please remember to register if you use this software).
Compile it like so

apxs -a -c -I/usr/include/libxml2 -i mod_proxy_html.c

Create a file /etc/httpd/conf.d/mod_proxy_html.conf

LoadFile /usr/lib/libxml2.soLoadModule proxy_html_module  /usr/lib/httpd/modules/mod_proxy_html.soProxyHTMLExtended On