Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1427
  • Last Modified:

how to configure tomcat for virtual hosts

I have been running apache/tomcat on linux fine for quite a while. Now I want to add virtual hosts. I can configure the virtual hosts in apache, but I cannot figure out how to get tomcat to recognize the context, etc. I've spent a lot of time researching and experimenting. No solutions. This has to be a fairly common application.
0
jmarkfoley
Asked:
jmarkfoley
  • 6
  • 4
1 Solution
 
cubeeqCommented:
Start at server.xml in conf directory in Tomcat home. Look at line beginning with

<Host name="localhost"  appBase="webapps" - it is default virtual host
0
 
jmarkfoleyAuthor Commented:
The <Host> entries in my current server.xml are shown below. The 1st one, localhost, is the default tomcat host. The next one, courtscan.com, is the new one I have configured. Does it look OK? Is appBase always relative to the .../tomcat/webapps directory or does specifying the complete pathname with a leading "/" give me an absolute path?

btw - thanks for your response! I really need to get this up ASAP!
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
 
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->
 
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
        -->
 
      </Host>
 
      <Host name="courtscan.com" appBase="/www/courtscan" >
        <Logger className="org.apache.catalina.logger.FileLogger" directory="logs/" prefix="courtscan_access_log" suffix=".log" timestamp="true"/>
 
        <Context path="/" docBase="/www/courtscan" reloadable="false">
          <WatchedResource>/WEB-INF/web.xml</WatchedResource>
        </Context>
      </Host>

Open in new window

0
 
mrcoffee365Commented:
Are you using Tomcat 5 or Tomcat 6?  The rules are looser for Tomcat 5, so that will affect how you need to approach this.  Also, are you using a .war file to deploy your courtscan app?

If you're using Tomcat 5, and your desired structure for the virtual host is something like:
http://www.courtscan.com
http://www.mydomain.com

and your webapp files are in your $catalina_home directory -- so on the file system:
$catalina_home/www/courtscan/

Then a setup like this should work:
<Host name="www.courtscan.com" appBase="/www/courtscan"
      unpackWARs="true" autoDeploy="true"
      xmlValidation="false" xmlNamespaceAware="false">
      <Alias>courtscan.com</Alias>
      <Logger className="org.apache.catalina.logger.FileLogger" directory="logs/" prefix="courtscan_access_log" suffix=".log" timestamp="true"/>
      <Context path="" docBase="ROOT" reloadable="false">
        <WatchedResource>/WEB-INF/web.xml</WatchedResource>
      </Context>
</Host>

You can leave out the <Context I think -- web.xml is watched by default.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jmarkfoleyAuthor Commented:
I am using tomcat 6. So, the appBase is relative to $CATALINA_HOME, even if I specify the full path? My /www/courtscan directory is from the root. I'll play around with your suggestions. I'm still not sure how to configure the virtual host entry for apache to compliment this. Currently, httpd.conf is as shown in the code snippet for this virtual host. Without tomcat, entering http://www.courtscan.com in the browser will look in /www/courtscan.  

The httpd directive JKMount appears to specify directories subordinate to the DocumentRoot. So, for a non-virtual host, with DocumentRoot /usr/local/apache/htdocs:

JKMount /courtscan/* worker1

would send everything referencing /usr/local/apache/htdocs/courtscan to tomcat context $CATALINA_HOME/webapps/courtscan. How would I specify this with a virtual host with DocumentRoot /www/courtscan? Would it be:

JKMount /* worker1

<VirtualHost *>
 ServerName www.courtscan.com
 DocumentRoot /www/courtscan
 
<directory /www/courtscan>
allow from all
</directory>
 
 CustomLog /www/courtscan/logs/access_log combined
 ErrorLog /www/courtscan/logs/error_log
</VirtualHost>

Open in new window

0
 
jmarkfoleyAuthor Commented:
btw - no, I am not using a .war file. Normally, I just put the jsp files subordinate to the webapps directory:

$CATALINA_HOME/webapps/myapp1
$CATALINA_HOME/webapps/myapp2, etc.

I haven't seen a downside to this yet, but maybe you can enlighten me
0
 
jmarkfoleyAuthor Commented:
More info. Sorry for all the separate messages. I think I sort-of have it working. I used your configuration for the <host> in server.xml except that I changed appBase to "webapps" since I didn't want the path to be $CATALINA_HOME/www/courtscan. If I have to specify a path relative to $CATALINA_HOME, I can live with $CATALINA_HOME/webapps/courtscan. However, when I typed http://www.courtscan.com in the browers it took me to $CATALINA_HOME/webapps/ROOT. So, I changed your docBase="ROOT" to be docBase="courtscan". That took me to $CATALINA_HOME/webapps/courtscan and ran the index.jsp I had there.

I have httpd.conf:
JKMount /* courtscan
JKMount / courtscan

I haven't tried anything else at this point. Let me know if you think I'm on the right track or just getting lucky with coincidentally hacked directives.

I'd like to NOT make everything subordinate to $CATALINA_HOME/webapps if possible. I suppose I could:
ln -s /www/courtscan $CATALINA_HOME/webapps/courtscan

What if I set server.xml: <Context path="..." to some absolute path. Would that do it?
0
 
mrcoffee365Commented:
It's good that you've posted more info about your configuration.  Now that we know how your server is really set up, it's easier to advise you as to how to configure it.  It's also good to know that Apache is the front end -- helpful to know.

Would you post the server.xml sections that you're now working with?

Now that you have a working version, you might get more from reading the documentation for Tomcat configuration:
Virtual Host:
http://tomcat.apache.org/tomcat-6.0-doc/virtual-hosting-howto.html
Context:
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html

Now that we have more info about your configuration, this server.xml fragment might work better for you:

<Host name="www.courtscan.com" appBase="webapps/courtscan"
      unpackWARs="true" autoDeploy="true"
      xmlValidation="false" xmlNamespaceAware="false">
      <Alias>courtscan.com</Alias>
    <Context path="" docBase="/www/courtscan" reloadable="false"/>
      <Logger className="org.apache.catalina.logger.FileLogger" directory="logs/" prefix="courtscan_access_log" suffix=".log" timestamp="true"/>
</Host>

Look at every parameter to Host and Context (and Alias) and compare them to your file system, and to the documentation for Host and Context for Tomcat 6 (linked above).  It will help you make any future changes.

Also -- my original example was a good one for Tomcat 5.  The recommendation for Tomcat 6 is to structure server.xml and context.xml differently from this.   However, this setup works in 6, so you can stay with it until you're more comfortable with Tomcat configuration.


0
 
jmarkfoleyAuthor Commented:
Thanks for all the help so far. It has been invaluable. Below are the <Host> section of the server.xml and relavent sections of httpd.conf and workers.properties, sans comments. The 1st <Host> section in server.xml is out-of-the-box vanilla installation. I did post all of this in an original posting that no one responsed too, possibly because there was too much info. So, this time I decided to start simple and provide info as needed.

I'll definitely look at those links and your server.xml suggestions and get back to you. I would really like to not have my context subordinate the the webapps directory for a variety of reasons, so I'll look at these resources. So far, I am able to get some work done. I've even been able to connect to my Oracle database and query successfully. So far so good.
server.xml
-----------
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
 
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->
 
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
        -->
 
      </Host>
      <Host name="www.courtscan.com" appBase="webapps"
        unpackWARs="true" autoDeploy="true"
        xmlValidation="false" xmlNamespaceAware="false">
        <Alias>courtscan.com</Alias>
        <Logger className="org.apache.catalina.logger.FileLogger" directory="logs/" prefix="courtscan_access_log" suffix=".log" timestamp="true"/>
        <Context path="" docBase="courtscan" reloadable="false">
          <WatchedResource>/WEB-INF/web.xml</WatchedResource>
        </Context>
      </Host>
 
httpd.conf
-------------
LoadModule    jk_module  lib/mod_jk.so
JkWorkersFile conf/workers.properties
JkShmFile     /var/log/httpd/mod_jk.shm
JkLogFile     logs/mod_jk.log
JkLogLevel    info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkMount  /foley/* worker1
JkMount  /foley worker1
JkMount  /* courtscan
JkMount  / courtscan
 
workers.properties
-------------------
worker.list=worker1,courtscan
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
 
worker.courtscan.type=ajp13
worker.courtscan.host=localhost
worker.courtscan.port=8009

Open in new window

0
 
mrcoffee365Commented:
The server.xml change I posted showed how to configure it to have your webapp in a location other than under the Tomcat directory.

Please look at it carefully, and, as I said, compare it to the documentation to see what it is doing.
0
 
jmarkfoleyAuthor Commented:
I think I am not going to mess with relocating my context outside of $CATALINA_HOME/webapps for the time being. Otherwise, everything seems to be working and development is under-way. So, I'll play around with relocating things later, when the development crunch lightens up. Thanks.
0
 
mrcoffee365Commented:
Great -- congrats on getting everything working, and good luck.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now