Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to configure tomcat for virtual hosts

Posted on 2009-04-02
11
Medium Priority
?
1,426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

 
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 2000 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

Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

Question has a verified solution.

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

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
There are numerous questions about how to setup an IBM HTTP Server to be administered from WebSphere Application Server administrative console. I do hope this article will wrap things up and become a reference for this task. You need three things…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

704 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