Skip to content

Instantly share code, notes, and snippets.

@esfand
Last active November 26, 2019 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save esfand/7748566 to your computer and use it in GitHub Desktop.
Save esfand/7748566 to your computer and use it in GitHub Desktop.
Tomcat with Multiple Virtual Host

I configured my single tomcat to host 2 services at different connector port like this :-

<!-- WebApp 1-->
<Service name="webapps1">
    <Connector port="7001" 
               maxThreads="150" 
               minSpareThreads="25" 
               maxSpareThreads="75" 
               enableLookups="false" 
               redirectPort="8443" 
               acceptCount="100" 
               connectionTimeout="20000" 
               disableUploadTimeout="true" />
    <Engine name="receiver1" defaultHost="localhost">
        <Host name="localhost"  
              appBase="webapps1" 
              unpackWARs="true" 
              autoDeploy="true"> 
        </Host>
    </Engine>
</Service>
<!-- End of WebApp 1-->

<!-- WebApp 2-->
<Service name="webapps2">
    <Connector port="7002" 
               maxThreads="150" 
               minSpareThreads="25" 
               maxSpareThreads="75" 
               enableLookups="false" 
               redirectPort="8443" 
               acceptCount="100" 
               connectionTimeout="20000" 
               disableUploadTimeout="true" />
    <Engine name="receiver2" defaultHost="localhost">
        <Host name="localhost"  
              appBase="webapps2" 
              unpackWARs="true" 
              autoDeploy="true">
        </Host>
    </Engine>
</Service>
<!-- End of WebApp 2-->

How to host multiple websites in single Tomcat instance

[By Duy Khanh in Linux, Web Server] (http://tips4admin.com/blog/2013/10/how-to-host-multiple-websites-in-single-tomcat-instance/)

In this quick post, I want to show you how to host multiple websites in single Tomcat instance.

Go to tomcat server configuration file conf/server.xml we will see the section like this.

...
<Host name="localhost"  appBase="webapps"
    unpackWARs="true" autoDeploy="true">
 
<!-- 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
     Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
       prefix="localhost_access_log." suffix=".txt"
       pattern="%h %l %u %t &quot;%r&quot; %s %b" />
 
</Host>
....

I will remove some comments in the code above to make it more succinct.

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
 
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
    prefix="localhost_access_log." suffix=".txt"
    pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>

That is the default host configuration when you setup tomcat where name=”localhost” is the new client’s domain, and appBase=”webapps” is his web application root directory name (where you deploy your war file); it is relative to the tomcat home dir.

If you want to add more host, here is an example

<Host name="tips4admin.com" appBase="myblog" unpackWARs="true" autoDeploy="true">
 
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
    prefix="localhost_access_log." suffix=".txt"
    pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>

In the new configuration above, I add a new host for domain name tips4admin.com and it will have java home directory is myblog.

For other domain name which not listed in your server.xml configuration file, Tomcat will jump to default localhost host. You can see this in the section

<Engine name="Catalina" defaultHost="localhost">

Once you have finish edited your server.xml file, you must restart tomcat server to take effect.

How can I configure Tomcat with multiple virtual hosts?

Posted on September 20, 2011 by admin

With Tomcat running on JVM Host dedicated JVM you have full control over configuration files. You may host multiple domains and map them to particular web applications. First step is to map a domain or a directory under it to the Tomcat (this is done with mod_jk or mod_proxy_ajp using our JVMCP control panel), second step is to add virtual host in server.xml.

See the below example.

You have 2 domains:

  • primary domain domain1.com, and
  • addon domain domain2.com.

Your ~/appservers/apache-tomcat/webapps directory:

$ls -al
docs
domain1
domain2
examples
manager
host-manager
ROOT

Please put JSP files into domain1 and domain2 directories. Alternatively you can put domain1.war and domain2.war in webapps directory and Tomcat will deploy the wars.

  • Domain1.com is the main domain (the main domain can point to different directory such as ROOT, anyway this is only example).
  • Domain2.com is the domain that we want to add to Tomcat.

You need create domain2.com as addon domain in cPanel.

Please make sure you use correct nameservers for domain2.com.

Create mappings – default mappings are enough. Use custom JVM control panel JVMCP for this. Configure $CATALINA_HOME/conf/server.xml file.

Please edit $CATALINA_HOME/conf/server.xml

 <Host name="domain1.com" autoDeploy="true" appBase="webapps" unpackWARs="true">
 <Alias>www.domain1.com</Alias>
 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"   
                prefix="localhost_access_log." suffix=".txt" 
                pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
 <Context path="" docBase="domain1" debug="0" reloadable="true"/> 
 </Host>

 <Host name="domain2.com" autoDeploy="true" appBase="webapps" unpackWARs="true">
 <Alias>www.domain2.com</Alias>
 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"   
                prefix="localhost_access_log." suffix=".txt" 
                pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
 <Context path="" docBase="domain2" debug="0" reloadable="true"/> 
 </Host>

Restart Tomcat using JVMCP or shell and your are done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment