Published on May 18 2018 in Java Tomcat

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 path under it to the Tomcat (this is done with mod_jk or mod_proxy_ajp using our Java Control Panel), second step is to add virtual host in server.xml. See the below example.

Let's assume you have 1 primary domain maindomain.com and are adding 1 addon domain addondomain1.com. Your ~/appservers/apache-tomcat directory already includes webapps directory that is default appBase for localhost and is serving maindomain.com (from webapps/ROOT). Please create webapps_domain1 directory.

Please put JSP files into webapps_domain1/domain1 directory. Alternatively you can put domain1.war into webapps_domain1 directory and Tomcat will deploy the WAR.

  1. maindomain.com is the main domain (the main domain can point to a different directory such as ROOT, anyway this is only example).
  2. addondomain1.com is the domain that we want to add to Tomcat.
  3. You need create addondomain1.com as addon domains in cPanel.
  4. Please make sure you use correct nameservers for addondomain1.com.
  5. Create mappings - default mappings are enough. Use 'cPanel - Java Control Panel - Mappings' for this.
  6. Configure $CATALINA_HOME/conf/server.xml file.

Please edit $CATALINA_HOME/conf/server.xml as follows:

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
    <!-- default content for default Host follows here -->
</Host>

<Host name="domain1.com" autoDeploy="true" appBase="webapps_domain1" 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>
    

Restart Tomcat using JavaCP or shell (SSH) and your are done.

Keeping applications for multiple domains under default (and common) webapps

This approach is also possible but uses slightly more memory as each Host detects all applications (WARs) inside common webapps and deploys them. All apps are then aaccessible via every domain (Host) which may also be unwanted behavior. Server.xml configuration for this approach would only differ in appBase for the new Hosts (it will be webapps):

<Host name="domain1.com" autoDeploy="true" appBase="webapps" unpackWARs="true">