Virtual Hosting refers to hosting multiple websites on the same server.In this tutorial we will talk about configuring virtual hosts on Tomcat

For the shake of this tutorial suppose our domain name is www.java-only.com and we want to deply the root application for this domain-in other words the web application that will be returned to requests for www.java-only.com-.

First of all,in order to make a new virtual host,we must create a directory that will be used as the appbase for the new host.The appbase directory is the directory that will contain the applications that will be deployed on the new host.It is highly recommended the appbase directory to be relative ot $(CATALINA_HOME) directory.In other words you should create a directory like this:

mkdir $(CATALINA_HOME)/appbase_directory

Since we want our application to be root application under the host,we shoud create a directory inside the appbase and name it ROOT where we will place our application's related files-WEB-INF,META-INF,JSPs etc.Afterwards, inside the appbase directory-but outside the ROOT directory- we must create an ROOT.xml file which is going to be the context configuration file for the application.ROOT.xml file should contain the following snippet:

Context path="/" docBase="/path/to/ROOT-directory" debug="0" privileged="true" allowLinking="true"/>

In the above snippet path attribute is the context path for the application which is the portion of the request uri for the application and since we want our application to be the ROOT application, we declare this by setting the path attribute to "/". the docBase attribute is used to declare the path to the directory where the application-realated files or the .war file is. the privileged attribute is used to declare if the container will accept applications such as the Manager application.the allowLinking is used to declare if the web app is allowed to link to web apps outside the docbase directory.Default value is "false".Finally debug atribute declares the debuging logging level.Default value for this attribute is "0".

After you have set up the context cofiguration file you have to create a directory inside the Catalina directory and name it with the exact name of the virtual host.For example "www.myapp.com".Inside the directory you should copy the ROOT.xml file you created earlier.

Finally,you should edit the server.xml file and place the below snippet:


 

the name attribute is the name of the virtual host which in our case is "www.myapp.com".As it was mentioned before appBase is the directory that will contain the web-applications for the new host.In this case the appBase will be appbase_directory we created earlier.If the autoDeploy attribute is set to "true",the container will check if there are new applications in the appBase directory and deploy them automatically.The unpackWAR attribute is used to actually tell the container whether .war files that are put in to appBase directory wii be unpacked to a coresponding directory structure inside the appbase directory or not.

After all the above steps you should restart your tomcat and you virtual host will be ready.