Solved

how to configure tomcat for virtual hosts

Posted on 2009-04-02
11
1,418 Views
Last Modified: 2013-12-02
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
Comment
Question by:jmarkfoley
  • 6
  • 4
11 Comments
 
LVL 3

Expert Comment

by:cubeeq
ID: 24058510
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
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24059470
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
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 24061594
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
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
LVL 1

Author Comment

by:jmarkfoley
ID: 24065854
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
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24065859
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
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24065912
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
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 500 total points
ID: 24068081
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
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24069155
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
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 24072403
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
 
LVL 1

Author Closing Comment

by:jmarkfoley
ID: 31566120
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
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 24107154
Great -- congrats on getting everything working, and good luck.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question