In this guide I’ll go through setting up some very simple virtual hosts on an Tomcat server. This guide assumes the steps gone through to setup Tomcat 6 on Ubuntu as per this previous post.
So, the first step is to define the host under /etc/tomcat6/server.xml:
Put the above line in the Catalina “Engine” section. The “name” attribute will be used as the hostname to match and the “appBase” will define where Tomcat will look for the applications to run off of this host. If you’d like to define some aliases for this virtual host, you can do so with a nested “Alias” directive as described in the Tomcat documentation.
Next, if we want to define the Context we simply create the directory for it under Catalina:
And then for a simple application we can just copy the ROOT app from the default context:
cp /etc/tomcat6/Catalina/localhost/ROOT.xml /etc/tomcat6/example.org
This will define the Context for our application. Next, we will need to create the application directory to actually hold our applications for this virtual host and copy the relevant application files to this new directory. This is done with:
cp -r /var/lib/tomcat6/webapps/ROOT /var/lib/tomcat6/example.org
Then I modified the “index.html” file under “example.org/ROOT/” to display “example.org” instead of the default “It Works!” so that we would know when the Virtual Host was being accessed. Once this is done, we can go ahead and restart Tomcat in order to apply the changes:
sudo service tomcat6 restart
To test out that this configuration is working, I added a line to my hosts file (/etc/hosts under linux) on my desktop machine to point “example.org” to the IP address of the VM that I had installed Tomcat on. This allowed me to type in http://example.org:8080 and have the request go to the Tomcat server.
So, there you have it, that’s the short story on how to setup up virtual hosts on Apache Tomcat.