Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosting multiple domains on single webapp folder in tomcat

Tags:

java

tomcat

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.

like image 406
KuKu Avatar asked Jan 23 '19 06:01

KuKu


People also ask

How do I host multiple websites on one Tomcat server?

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.

Can you have multiple domains on one server?

Virtual hosting allows you to host many domains on one server. A server may have extensive resources like HDD space, CPU, RAM, and so on. You can use the same server resources for different sites. It lets you host multiple websites on a single web server instance.

What is webapp folder in Tomcat?

The webapps directory is where deployed applications reside in Tomcat. The webapps directory is the default deployment location, but this can be configured with the appBase attribute on the <Host> element.


1 Answers

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.

like image 99
Selaron Avatar answered Sep 18 '22 02:09

Selaron