Hosting multiple domains on single webapp folder in tomcat

KuKu :

Possible duplicate of this but answer is not accepted.

I have 2 scenarios

  1. We are building a CRM and we will be having multiple clients using same product. Lets take a example, subdomain1.maindomain1.com and anysubmain.anothermaindomain.com should be pointed to same webapp folder. And depending on domain, we will select database dynamically but codebase will remain same. Point to note here : Whole codebase remains same.
  2. We are building series of website for a client where part of the codebase will remain same for all but depending on subdomain we will load the default servlet file. Lets take example, manage.domain.com crm.domain.com equote.domain.com should be pointing to same webapp folder. And depending on domain we will load default servlet file. Point to note here : Part of codebase will remain same for all domains. Ex. core architect files.

What solutions other have suggested

  1. Deploy copy of same war file 2 time, Softlink, Create 2 contexts that point to the same file, Use alias. Last one can be good option but no idea how we can use this for different subdomains / domains.
  2. This can be one of the solution but not sure whether it will work on same port or different port
  3. There are many articles over internet which shows how we can deploy multiple webapps on multiple domain on single tomcat server but not the way i need.

Note: I can create 2 AWS EC2 instances for above 2 scenarios. It means that I am not expecting one solution to above 2 problems.

Selaron :

In Apache Tomcat you can configure multiple virtual hosts that each deploy the same .war file (or document base) wile having different context configuration parameters like JDBC connection, ressources, esternal JAR files and others.

To stick with your scenario (1), in server.xml configure both domains' host elements:

<Engine name="Catalina" defaultHost="subdomain1.maindomain1.com">
    <Host name="subdomain1.maindomain1.com"    appBase="subdomain1.maindomain1.com"/>
    <Host name="anysubmain.anothermaindomain.com" appBase="anysubmain.anothermaindomain.com"/>
</Engine>

And create resource and configuration folders for both:

mkdir $CATALINA_HOME/subdomain1.maindomain1.com
mkdir $CATALINA_HOME/anysubmain.anothermaindomain.com
mkdir $CATALINA_HOME/conf/Catalina/subdomain1.maindomain1.com
mkdir $CATALINA_HOME/conf/Catalina/anysubmain.anothermaindomain.com

Then for each host create a ROOT.xml each pointing to the same code base (e.g. .war file) but different data bases configuration. In general this providing a diffent context configuration for each domain.

$CATALINA_HOME/conf/Catalina/subdomain1.maindomain1.com/ROOT.xml

<Context docBase="/path/to/your/webapp.war" path="">
     <Resource name="jdbc/Database" auth="Container" type="javax.sql.DataSource"
               username="subdomain1_maindomain1_com" password="anysecurepassword" driverClassName="com.your.jdbc.Driver"
               url="jdbc:xyz://localhost:321/subdomain1_maindomain1_com_dbname"/>
   ...
</Context>

$CATALINA_HOME/conf/Catalina/anysubmain.anothermaindomain.com/ROOT.xml

<Context docBase="/path/to/your/webapp.war" path="">
     <Resource name="jdbc/Database" auth="Container" type="javax.sql.DataSource"
               username="anysubmain_anothermaindomain_com" password="anysecurepassword" driverClassName="com.your.jdbc.Driver"
               url="jdbc:xyz://localhost:321/anysubmain_anothermaindomain_com_dbname"/>
   ...
</Context>

Additionally, in order to implement scenario 2, for each domain you can configure different external resource folders.

E.G. for anysubmain_anothermaindomain_com_dbname in $CATALINA_HOME/conf/Catalina/anysubmain.anothermaindomain.com/ROOT.xml

<Context>
...
  <Resources>
    <PreResources base="/path/to/anysubmain_anothermaindomain_com_dbname/jarfiles/"
      className="org.apache.catalina.webresources.DirResourceSet" readOnly="true"
      internalPath="/" webAppMount="/WEB-INF/lib" />
  </Resources>
...
</Context>

This way all domain's web application base on the same docBase but can have different (variants of) jar files or other resource dependencies added.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=36672&siteId=1