Wednesday, 15 April 2009

Tomcat configuration: multi hosting

We recently extended our web server farm and we went through Tomcat 6 configuration trying to optimize performance and memory usage, in particular for our Teamwork hosting service. We are quite satisfied of the final result, but it required quite an effort. During our research we found several tools and hacks that we would like to share. In particular we found it hard to configure multi hosting as documentation is lacking (see the references): here you'll find an example of how to configure multi hosting on Tomcat 6.

In order to set a multi host we need to act on server.xml (in a standard Tomcat installation you can find it in CATALINA_HOME/conf) and change some parameters.
The default form of parameters that we want to change is:



In this example there is a single host and it's localhost and its appBase is in the default Tomcat location webapps directory.
For ours settings we add our virtual host:



In this case the default Host is the same as before, localhost, but we added a new host "demo.twproject.com" and we specified the appBase that is different from Tomcat's default location. But notice that this setting is not enough to set a new virtual host: we have to create (if it doesn't exist you must create it ) an xml context file relative to our new host under

'CATALINA_HOME/conf/Catalina/hostName'.

You can notice that directory structure has a correspondence with the engine and host name, if you want to be more general you should write structure for virtual hosts as

'CATALINA_HOME/conf/EngineName/virtualHostName'

In our specific case the application 'demo.twproject.com' has to answer as root of our registered url, so we created a context file named 'ROOT.xml' under

'CATALINA_HOME/conf/Catalina/demo.twproject.com/',

and the context is:

As we have said before our web application is the main application (and default application) on our virtual host, so we have to put into a directory called ROOT. As you can see we have set the application path to empty simply because it is the main application.

In this way we can set more virtual hosts that respond to different urls.


We set max and min Java heap size allocation using 'tomcat6w.exe', as we are on a Windows server (but what we’ve done before is valid in general) and set max and min at the same size, so we can allocate definitely a portion of memory for the JVM. For other settings such as GC optimized for multiprocessor machines, policy for scavengers, optimizations for throughput we have set a system variable named CATALINA_OPTS with our configuration:

CATALINA_OPTS = -Xmn 600m -XX:-UseParallelOldGC -XX:+AggressiveOpts

In other systems like Linux we can set max and min java heap size adding to CATALINA_OPTS parameters -Xmx and -Xms with max and min heap size.

As web-applications manage utility we use Lambda probe, a useful tool for managing multiple web apps and get statistics on them and on the system:




Lambda Probe is easy to install on your Tomcat ( or other application-server ), because it is just a war that you deploy in your app-server.

References

Java
http://java.sun.com/performance/reference/whitepapers/tuning.html#section4.2.5
http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp
http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html

Tomcat 6
http://tomcat.apache.org/tomcat-6.0-doc/deployer-howto.html#A%20word%20on%20Contexts
http://tomcat.apache.org/tomcat-6.0-doc/virtual-hosting-howto.html

Lambda Probe
http://www.lambdaprobe.org/d/index.htm

@author Roberto Baldi and Pietro Polsinelli

11 comments:

Unknown said...

What if you want multiple domain names to point to the same web app?
E.G. you want your users to be able to write both demo.twproject.com and demo.twproject.co.uk as URL's and come to the same web app.

Rhys Black said...

There are a lot of ways to do this, but the least difficult, assuming you have access to your domain name registry entry is to redirect subdomain traffic to the main page you want as an alias on your domain name service.

Pietro Polsinelli said...

@Wesslan: Redirect is a good idea also considering Google indexing, which does not like duplicated contents.

Pietro Polsinelli said...

A note: a general advice is to set the available permgen space to something more tha the default 64mb - it gave us a lot of problems, solved by setting a higher max value.

Unknown said...

Hi, i want your favour regarding tomcat6 on redhat linux 5 , on same machine have apache2.
tomcat6 working fine.
we deploy 3 .war java applicaton ( myste1.war, mysite1.wasr and mysite3.war) application on this, all working by http://localhost:8080/mysite1
http://localhost:8080/mysite2
http://localhost:8080/mysite2
but i need to host as
http://mysite1.com
http://mysite2.com
http://mysite3.com

i did DNS entry and checked by ping.
Please suggest appropriate step for this. if possible pls mail me on prajapatipuran@gmail.com

Anonymous said...

Thanks for introducing this configuration details. Now its time to use application hosting service to increase business productivity.

Sandy said...

Thanks for giving the information about the very new hosting services, Is application hosting also comes under multi hosting?

Anonymous said...

This is first time i am reading about multi hosting and i got huge knowledge from this post, You have described each step very clearly.domain registration india

Website Domain Name

Anonymous said...

Thanks for taking the time to discuss this Your post really helped me to understand this Thanks to sharing Domain registration

Anonymous said...

This is really great. I like to be visiting your blog. Thanks very much for sharing all this great information here.

download as premium

Unknown said...
This comment has been removed by the author.