Possible duplicate of this but answer is not accepted.
I have 2 scenarios
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.
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
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.
This can be one of the solution but not sure whether it will work on same port or different port
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.
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.
Related
i have my (spring) web application 'mywebapp' configured in tomcat as follows:
$CATALINA_HOME/conf/Catalina/localhost/mywebapp.xml
<Context path="/mywebapp" docBase="<absolute path to my web application>"
debug="0" privileged="true" reloadable="true">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Context>
on my local machine, i can access my application from browser at the following url:
http://localhost:8080/mywebapp/
no issues whatsoever up to this point
to support mobile devices we have recently implemented a separate set of views which are organised under path m/. this mapping is done via spring's #RequestMapping annotation. basically, now there are two sets of resources that are served via the following urls:
http://localhost:8080/mywebapp/
and
http://localhost:8080/mywebapp/m/
where the former is for desktop / laptop users and the latter is for mobile users
now, is there a way that i can configure a subdomain to make this work? in other words, how can i configure tomcat so that requests to
http://m.localhost:8080/mywebapp/
are forwarded to
http://localhost:8080/mywebapp/m/
please keep in mind that there is only a single web application. so both the urls should actually reach the same application 'mywebapp'
i thought configuring an additional Host element in tomcat's server.xml would be the way to go. but i still couldn't figure out how it can be done
i am on a windows machine and have already added the following entry in C:\Windows\System32\drivers\etc\hosts file
127.0.0.1 m.localhost
I would like to run my spring application two times, in parallel, on the same tomcat server. One time with a production profile and one time with a dev profile.
I also would like to build one single WAR for the two profiles.
I've successfully integrated profiles in my application with #Profile annotations. I've successfully deployed the two WAR files on my tomcat server.
What I need is a mean to activate a different profile on each of theses two applications, with the constraint that these two applications use a copy of the same WAR file and that the two applications should run in parallel.
So WebApplicationInitializer and web.xml seem not an option.
For the record:
To activate the dev spring profile on the application in application-dev.war
Create a file <CATALINA_BASE>/conf/Catalina/localhost/application-dev.xml
With the following content:
<Context>
<Environment name="spring.profiles.active" value="dev,server" type="java.lang.String" override="false" />
</Context>
This set the spring.profiles.active property to dev,server for the application run by application-dev.war.
Thanks to this answer: https://stackoverflow.com/a/26653238/1807667
P.S.: With autoDeploy=true in server.xml, the configuration files disappear on tomcat restart.
Solution is to add <Context reloadable="true"> in <CATALINA_BASE>/conf/context.xml but beware that according to documentation :
This feature is very useful during application development, but it
requires significant runtime overhead and is not recommended for use
on deployed production applications.
and moreover using <Context reloadable="true"> does not solve fully the issue the configuration files still disappear for some restart.
P.S.2: There is no docBase attribute in the Context element, see this question.
I've set up two tomcat instances running on different ports on the same Windows machine. One is for Alpha, the other is for Development. I've deployed the Alpha and Dev wars (same app name, different settings in META-INF/context.xml) to each server.
The Tomcat directories have the exact same content except for:
Wars deployed are different (I started fresh, removed webapps/* war files and dirs)
conf/server.xml files are different (set different ports)
bin/catalina.bat files are different (changed CATALINA_HOME to the full path of each instance)
The rest is the same.
The thing is that META-INF/context.xml files of each war point to different databases, one for dev and one for alpha. More specifically the connection URL is different, and even more specifically only the database name is different (the string between the slash and the question mark of the URL).
However, after starting both instances for some reason they BOTH point at the Dev database. That is, they both are presumably reading the Dev context.xml, and probably the same webapps/AppName folder
How is that possible?
When I start the Dev tomcat the console displays the values of CATALINA_HOME, CATALINA_BASE, CATALINA_TMPDIR and CLASSPATH, and they all show full paths to the Dev tomcat directory.
When I start the Alpha tomcat the console displays the values of those variables as well, and they all show full paths to the Alpha tomcat directory (so the paths seem to be OK).
I checked the deployed alpha_tomcat_dir/webapps/AppName/META-INF/context.xml file and it DOES show alpha database configuration.
But at runtime it still uses the db information of the Dev database.
Where else should I look?
These are the context.xml files:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/hng"
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
type="javax.sql.DataSource"
maxActive="60"
minIdle="5"
maxIdle="60"
maxWait="10000"
testOnBorrow="true"
testOnReturn="true"
testWhileIdle="true"
validationQuery="SELECT 1"
timeBetweenEvictionRunsMillis="20000"
minEvictableIdleTimeMillis="300000"
removeAbandoned="true"
removeAbandonedTimeout="180"
logAbandoned="true"
username="root"
password="<removed>"
url="jdbc:mysql://127.0.0.1:3306/dbname?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true" />
</Context>
The files only differ in the database name. It's "hngdev" for dev and "hngalpha" for alpha (instead of dbname)
If you are 100% sure that the context content is right,
Then I would say check your settings in the database server.
could be that you have created the user wrong in the database server? Given same rights for alpha user and dev user. Maybe you did copy-paste same grant user queries for both?
Also you could try to change db information in alpha context, to some wrong user info/port. Does it still connect?
The problem was that I had a context.xml file equal to the dev environment's in $CATALINA_BASE/conf/[enginename]/[hostname]/[webappname].xml
Due to my server.xml configuration the context descriptor was once copied there before I duplicated the whole tomcat directory structure to make the alpha tomcat environment. And due to my server.xml configuration it was not updated on new deploys of the app. The datasource information was being read from $CATALINA_BASE/conf/[enginename]/[hostname]/[webappname].xml, which I didn't know or expect.
Here's the detailed explanation of how contexts and deployments work on different server configurations:
http://tomcat.apache.org/tomcat-7.0-doc/config/context.html
How can I configure Tomcat (in standalone mode, that is without Apache [*]) so that I can deploy it on one server and have it serve two different webapps, depending on the domain name requested?
What are the gotchas when doing that? Can you have SSL on both domains? If anyone here actually did it I'd be interested in answer to these questions as well as as much feedback as possible...
I found a blog entry describing such a setup, but it's for Tomcat 5.5:
<Engine defaultHost="domain1.com" name="Catalina">
<Host name="domain1.com" appBase="/home/user1/domain1">
<Alias>www.domain1.com</Alias>
<Context path="" docBase="."/>
</Host>
<Host name="domain2.com" appBase="/home/user1/domain2">
<Alias>www.domain2.com</Alias>
<Context path="" docBase="."/>
</Host>
http://iam-rakesh.blogspot.com/2009/10/hosting-multiple-domains-in-tomcat.html
Also, as of now I've got one webapp, ROOT.war, inside .../tomcat/webapps/
How would that work once I'd have two "roots", one root webapp for domain1.com and one root webapp for domain2.com? Where would the .war needs to be located?
The blog that you linked to basically shows how to do it. The one thing that you need to differently is to set the 'docBase' attribute differently for each host. The docBase is the location of war files for that host. With different docBases, you can have different root apps.
I am working on a Java application for a while. I primarily work on .NET Platform. Although I feel lot of concepts are common between these two platforms but there are few areas where I am finding some issues related to the configuration.
I am working on Authentication and Authorization and thought I would get something similar to Membership APIs of .NET in JAVA. Closest which I got was using j_security_check. I also got to know about JAAS but think it is little too deep for me to dive into.
I have created the user and role tables in the database and now I have to specify the JDBC Realm settings somewhere. I am using Tomcat 7.0. In most places, it's mentioned that I need to specify the realm setting in the server.xml.But wouldn't that apply to all web application deployed on that server since it would become a server level configuration ?.
On a site I even saw a developer mentioning about context.xml but again can't see a standard document that mention about using this XML file for setting JDBC realm
In.NET, We always put Membership settings at the web.config level and not Machine.config.
Totally Confused on this. Looking for some light on this.
Why is Realm setting required in Server.xml and not web.xml
This is not true, you can define it in your webapp as well, but then only in a servletcontainer-specific configuration file, such as /META-INF/context.xml in case of Tomcat. It cannot be definied in /WEB-INF/web.xml because it's specific to the standard Servlet API, not the servletcontainer implementation.
But wouldn't that apply to all web application deployed on that server since it would become a server level configuration ?.
That's correct. This is not recommended if you have no control over the server or if you don't want to publish the realm through other webapps.
On a site I even saw a developer mentioning about context.xml but again can't see a standard document that mention about using this XML file for setting JDBC realm
You can specify it in webapp's /META-INF/context.xml. See also Tomcat's own documentation on the <Context> element:
Defining a context
It is NOT recommended to place <Context> elements directly in the server.xml file. This is because it makes modifying the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat.
Individual Context elements may be explicitly defined:
In an individual file at /META-INF/context.xml inside the application files. Optionally (based on the Host's copyXML attribute) this may be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to application's base file name plus a ".xml" extension.
In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context path and version will be derived from the base name of the file (the file name less the .xml extension). This file will always take precedence over any context.xml file packaged in the web application's META-INF directory.
Inside a Host element in the main conf/server.xml.
(emphasis is not mine, it is already as such in Tomcat's documentation)