TOMCAT AND MULTIPLE VIRTUAL HOSTS

We had Tomcat working on a single web server. Then we added some virtual hosts and the tomcat didn't work on the virtual web servers. "Not a problem" we thought, we can get this working without any problems.

Four days later and its finally working. What I expected to be simple turned out to be a nightmare of hacking things. The documentation was hazy at best, so this is the brief recap on what we did. Part of the problem I'm sure was because multiple people had set up the server, so things were in different places with multiple instances and configuration files installed.

First step was to get the latest and greatest install of Tomcat.

We used the binary version of tomcat (Tomcat-4.1.24). You uncompress it, then copy it to the desired destination.

I like things on the local machine being in /usr/local/ So I put it in /usr/local/tomcat-4.1.24 and created a symbolic link /usr/local/tomcat pointing to that directory.

Of course, the old install had been in /var/tomcat, so putting the new one in /usr/local broke things, so there was an enjoyable half a day fixing things and getting it back to the same state it was in before hand.

I won't put in here how to configure a basic install of tomcat, that is relatively straight forward and there is decent documentation on the web about that.

Lets also assume that your web server is setup with virtual hosts. Somewhere in your Apache configuration you should have something that looks like this:


	<VirtualHost 192.168.1.1>
	    ServerAdmin webmaster@somewhere.com
	    DocumentRoot /var/www/virtual/public_html
	    ServerName virtual.domainname.com
	    ErrorLog /var/www/virtual/weblogs/error_log
	    CustomLog /var/www/virtual/weblogs/access_log common
	    <Directory /var/www/virtual/public_html/cgi-bin/>
	        Options ExecCGI
	        SetHandler cgi-script
	    </Directory>
	</VirtualHost>

First thing you want to do it put in the handler entries for the Tomcat stuff so the Apache server knows not to try and interpret the .jsp pages and instead send it to the Tomcat system.


	    JkMount /servlet/* connect_id
	    JkMount /*.jsp connect_id
So you end up with something like this:

	<VirtualHost 192.168.1.1>
	    ServerAdmin webmaster@somewhere.com
	    DocumentRoot /var/www/virtual/public_html
	    ServerName virtual.domainname.com
	    ErrorLog /var/www/virtual/weblogs/error_log
	    CustomLog /var/www/virtual/weblogs/access_log common
	    <Directory /var/www/virtual/public_html/cgi-bin/>
	        Options ExecCGI
	        SetHandler cgi-script
	    </Directory>
	    JkMount /servlet/* connect_id
	    JkMount /*.jsp connect_id
	</VirtualHost>



First file you should edit is the server.xml file. You should find it in the config directory of your tomcat install. You need to create a connector for tomcat to run on. The default port is 8109, so don't use that, I just moved things up to port 8112


    <Connector className="org.apache.ajp.tomcat4.Ajp13Connector"
               port="8112" minProcessors="3" maxProcessors="10"
               acceptCount="10" debug="0"/>

This will run a virtual machine on port 8112. The other parameters to be aware of are the min and max numbers of processors to run. We don't envsinge a lot of work or connections, so we throttled the min processors back to 3 and a max of 10. We may have to change this later, but with 21 virtual hosts, it does save a fair amount of CPU load.

Further down in server you need to add the entry for the virtual host. Ensure that you add this below the descriptor for the default (localhost?) host.


        <!-- Instance for each virtual hosts -->
        <Host name="virtual.domainname.com" debug="0" unpackWARs="true">
        <Logger className="org.apache.catalina.logger.FileLogger"
                 directory="logs"  prefix="virtual_log." suffix=".txt" timestamp="true"/>
        <Context path="" docBase="/var/www/virtual/public_html" debug="0" reloadable="true"/>
        <Valve className="org.apache.catalina.valves.AccessLogValve"
                 directory="logs"  prefix="virtual_log." suffix=".txt"
                 pattern="common"/>
        </Host>

Hostname is the name of the virtual host. There is some logging info. Something to take note of is the Context path. Notice the empty quotes. Don't put in "/" it doesn't work. Also set the docBase to the same as DocumentRoot in the virtual hosts entry.



Other file that needs editing is workers.properties First thing. Add the connection name to the worker.list

		worker.list=ajp12, ajp13, connect_id
Next add that connection to the worker

		worker.connect_id.port=8112
		worker.connect_id.host=virtual.domainname.com
		worker.connect_id.type=ajp13



Within the document root directory, ensure there is a WEB-INF directory. I discovered that it doesn't work without one. Or you could probably redirect that using a directory alias within the virtual hosts directive.


If it doens't work, start running things in debug mode and looking at logs, hopefully its just a simple thing.

TROUBLESHOOTING

Problems that I had:


Back to index page