11

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.

KuKu
  • 646
  • 2
  • 15
  • 38
  • If I were you, I would use a reverse proxy, and run 2 instances of Tomcat (or at least 2 servlet contexts based on the same application in the same Tomcat). It's safer and less error-prone. – ernest_k Jan 23 '19 at 07:02
  • Keeping one piece of code on multiple directories doesn't make sense. The reason behind that is, if we update the code, I will have to update at multiple places at the time of deployment. – KuKu Jan 23 '19 at 07:17
  • 1
    I'd wonder why that's a problem. It's not duplicated code. It's a copy of the artifact/binaries. It's common practice to run multiple instances (full stack) of an application, for many reasons including failover. That's why deployment tasks get automated. When that's a problem, which I strongly doubt, I think multiple tomcat instances can be launched and pointed to the same doc root (though I can't remember how to set that up). – ernest_k Jan 23 '19 at 07:23
  • @ernest_k, I am getting you. I think i need to put it in different way. Although your point is absolutely right. I was doing all this for reduce efforts from development side as well as deployment side. Imagine that i made a minor changes to database class from core architect files, i have to replicate that change to all sites. This can be done either manually or using git or something else which i can not think of now. yet, what i was planning that, one commit, one push, one deploy. so thats why I went to this route. – KuKu Jan 23 '19 at 09:34
  • Are you running an Apache web server in front of Tomcat? – gsl Jan 25 '19 at 13:54

1 Answers1

10

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.

Selaron
  • 6,105
  • 4
  • 31
  • 39