Tuesday, March 5, 2013

Virtual Hosting on Apache Tomcat


 Solution Description


      This is a guide will help you configuring the virtual hosting on Apache Tomcat as explained in the below scenarios.

1.    You have registered domain names like www.site1.com, www.site1.org, www.site1.co.uk for your website which is hosted in Apache Tomcat. Now you need to configure in such a way that above URLs should show the same website from Apache Tomcat.

2.    You have registered domain names like www.site1.com, www.site1.org and www.site2.com, www.site2.org for your two websites hosted on a single Apache Tomcat Server. Now you need to configure the URLs to hit the corresponding websites.

Overview of the solution

This is a guide on setting up Tomcat to do virtual hosting and make it behave like a simple web server 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.

To configure Tomcat for a virtual host, we need a <Host> directive in the server.xml file. We start with the website, called 'localhost' which keeps its files in <Apache Tomcat Installation Folder>/webapps.

Assume the folders for site1 is <Apache Tomcat Installation Folder>/webapps/SITE1 and site 2 is <Apache Tomcat Installation Folder>/webapps/SITE2.

Contents of server.xml

<Server port="8005" shutdown="SHUTDOWN">
<Service name="Catalina">
<!-- Define a non-SSL Coyote HTTP/1.1 Connector on port 8080 -->
<Connector port="80" protocol="HTTP/1.1"  connectionTimeout="20000"  redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost"  appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
</Host>
<!-- VIRTUAL HOST INJECTION POINT -->
 
</Engine>
</Service>
</Server>


Add the following file ROOT.xml into the path <Apache Tomcat Installation Folder>/webapps/SITE1(Similarly for SITE2)

Contents of the ROOT.xml

<?xml version='1.0' encoding='utf-8'?>
<Context displayName="SITE1" docBase="" path=""
workDir="work/Catalina/site1/_">
</Context>
 
              Scenario 1.

             Adding a virtual host to this config.

From here, to add a virtual host site1.com with an alias of www.site1.com or www.site1.org or site1.org or www.site1.co.uk or site1.co.uk , the following steps are required.

Shut down tomcat.

The following command can be used to shutdown the tomcat.

./catalina.sh stop

(catalina.sh can be located under <Apache Tomcat Installation Folder>/bin)

Add a Host entry to the server.xml file at the VIRTUAL HOST INJECTION POINT

<Host name="site1.com"  appBase="webapps/SITE1"  unpackWARs="true" autoDeploy="true"          xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="<Apache Tomcat Installation Folder>/webapps/SITE1" debug="0" reloadable="false"/>
<Context path="/site1" docBase="<Apache Tomcat Installation Folder>/webapps/SITE1" debug="0" reloadable="false"/>
<Alias>www.site1.com</Alias>
<Alias>www.site1.org</Alias>
<Alias>site1.org</Alias>
<Alias>www.site1.co.uk</Alias>
<Alias>site1.co.uk</Alias>
 
</Host>
This tells the Tomcat server that if a request comes in for site1.com or www.site1.com or www.site1.org or site1.org or www.site1.co.uk or site1.co.uk  it should redirect the user to the content located under the webapps directory. The Alias tag is where you define all of the alternative names for this website. If the Host is configured with unpackWARs=true and you put a war file in the appBase directory, the war file will be unpacked automatically into a directory with the same name as the war file.   If the Host is configured with autoDeploy=true, the Context path must match the directory name or war file name without the ".war" extension. This means that once the war is unpacked it will immediately become live on the server.

NB:- 
 
The directory structures under the appBase for each host should not overlap each other.
The docBase for a context should never be the same as the appBase for a host.
The directory mentioned in the appbase should be available under webapps.
The appBase directory is the default location under which webapps for a given <Host> are deployed.  A docBase is the location (directory or .war file) of an individual webapp.  The docBase attribute of a <Context> element is nnly used when the webapp is deployed outside of the <Host> appBase directory; otherwise it is derived automatically just from the webapp's existence under appBase.

Start tomcat.

The following command can be used to start the tomcat.

./catalina.sh start

(catalina.sh can be located under <Apache Tomcat Installation Folder>/bin)

Testing the Solution

Step 1. Open an internet browser and type site1.com or www.site1.com or www.site1.org or site1.org or www.site1.co.uk or site1.co.uk in the address bar. User should be able to view the website hosted in tomcat on all the URLs mentioned above.


Scenario 2.

             Adding multiple virtual hosts

From here, to add virtual hosts for site1.com and site2.com with their aliases, the following steps are required.

Shut down tomcat.

The following command can be used to shutdown the tomcat.

./catalina.sh stop

(catalina.sh can be located under <Apache Tomcat Installation Folder>/bin)

Add a Host entry to the server.xml file at the VIRTUAL HOST INJECTION POINT

<Host name="site1.com"  appBase="webapps/SITE1"  unpackWARs="true" autoDeploy="true"          xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="<Apache Tomcat Installation Folder>/webapps/SITE1" debug="0" reloadable="false"/>
<Context path="/site1" docBase="<Apache Tomcat Installation Folder>/webapps/SITE1" debug="0" reloadable="false"/>
<Alias>www.site1.com</Alias>
<Alias>www.site1.org</Alias>
<Alias>site1.org</Alias>
</Host>
<Host name="site2.com"  appBase="webapps/SITE2"  unpackWARs="true" autoDeploy="true"          xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="<Apache Tomcat Installation Folder>/webapps/SITE2" debug="0" reloadable="false"/>
<Context path="/site2" docBase="<Apache Tomcat Installation Folder>/webapps/SITE2" debug="0" reloadable="false"/>
<Alias>www.site2.com</Alias>
<Alias>www.site2.org</Alias>
<Alias>site2.org</Alias>
 
</Host>
 
               Start tomcat.

The following command can be used to start the tomcat.

./catalina.sh start

(catalina.sh can be located under <Apache Tomcat Installation Folder>/bin)

Testing the Solution

Step 1. Open an internet browser and type site1.com or www.site1.com or www.site1.org or site1.org in the address bar. User should be able to view the website site1 hosted in tomcat on all the URLs mentioned above.

Step 2. Open an internet browser and type site2.com or www.site2.com or www.site2.org or site2.org in the address bar. User should be able to view the website site2 hosted in tomcat on all the URLs mentioned above.

No comments: