[code lang="html"]
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.
Mistake one - don’t get the RPM. The RPM over-writes various configuration files. Although it does make a backup, the first time it ran it failed halfway through, so I ran it again and the original backup files were overwritten with the first backup from the RPM. Luckily I had made backups of all the files except for one which I didn’t know about.
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:
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
Options ExecCGI
SetHandler cgi-script
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:
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
Options ExecCGI
SetHandler cgi-script
JkMount /servlet/* connect_id
JkMount /*.jsp connect_id
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
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.
directory="logs" prefix="virtual_log." suffix=".txt" timestamp="true"/>
directory="logs" prefix="virtual_log." suffix=".txt"
pattern="common"/>
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.
Restart Tomcat.
Restart Apache.
Test.
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:
Permissions not set for the Tomcat user. If you test things as root, then try to get the user that tomcat runs under to run them, it will most likely fail. You need to do a chown for the tomcat working area.
Permissions not set correctly for the web areas.
Tomcat being braindead when it came to setting the environment variables. This was fixed by starting the tomcat process as a daemon rather than as a su’ing to the tomcat user.
More tips from another site.
How can I configure Tomcat with multiple virtual hosts?
Highly recommended tutorial:
http://www.csse.uwa.edu.au/~ryan/tech/tomcat.html
Example “/etc/tomcat4/server.xml” file:
www.domain1.com
directory="logs" prefix="virtual_log1." suffix=".log" timestamp="true"/>
www.domain2.com
directory="logs" prefix="virtual_log2." suffix=".log" timestamp="true"/>
Example “/etc/httpd/conf/mod_jk.conf” file:
JkWorkersFile conf/workers.properties
# JkLogFile logs/mod_jk.log
# JkLogLevel info
JkMount /*.jsp ajp13
JkMount /hw/* ajp13
# JkMount /examples/* worker1
# JkMount /*/servlet/ worker1
# JkMount /webdav/* worker1
# JkMount /tomcat-docs/* worker1
# JkMount /admin/* ajp13
# JkMount /manager/* worker1
# JkLogStampFormat “[%a %b %d %H:%M:%S %Y] ”
# JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat “%w %V %T”
# JkOptions +ForwardURICompat
ServerName domain1.com
ServerAlias www.domain1.com
DocumentRoot /var/tomcat4/webapps/domain1
JkMount /* ajp13
ServerName domain2.com
ServerAlias www.domain2.com
DocumentRoot /var/tomcat4/webapps/domain2
JkMount /* ajp13
[/code]