Possible duplicate of this but answer is not accepted.
I have 2 scenarios
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.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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With