One Tomcat instance for two domains and two webapps - java

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.

Related

Hosting multiple domains on single webapp folder in tomcat

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.

can i configure a subdomain to point to a specific path within a web application?

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

Netbeans issues with tomcat context.xml

I have the following JNDI realm ldap "thing" that used to reside inside of META-INF/context.xml
// Obviously i changed some of the code here to hide certain key information
<Realm className="org.apache.catalina.realm.JNDIRealm"
connectionName="uid=admin,ou=system"
connectionPassword="<PASSWORD>"
debug="99"
roleBase="<ROLEBASE>"
roleName="cn" roleNested="true"
roleSearch="(uniqueMember={0})"
roleSubtree="true"
userBase="USERBASE"
userSearch="(mail={0})"
connectionURL="ldap://MACHINE:10389"
/>
Now that I'm nearing a production phase of my work I wanted to move this file into the $CATALINA_BASE/conf/context.xml file instead so that I can do a machine based configuration. I did this on two linux boxes no problem, however, when I do it on my development machine and try to run (in netbeans) i get this prompt
Is there something key I'm missing here? Even if i type in the correct password netbeans gets into a strange loop and won't take the password. As soon as i remove the Realm def everything works fine.
Am I doing something terribly wrong here?
I just ran into this issue as well and I was able to fix this by pasting the UserDatabaseRealm from server.xml:
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
into the tomcat manager webapp context.xml
webapps\manager\META-INF\context.xml
and also the manager.xml that is found in the folder at:
%APPDATA%\NetBeans\<netbeans version>\apache-tomcat-<version>_base\conf\Catlina\localhost
I believe you only really need to paste it into the %APPDATA%\NetBeans\<netbeans version>\apache-tomcat-<version>_base\conf\Catlina\localhost\manager.xml folder but it doesn't hurt to put it in both to be safe.
For reference, this is what my manager.xml file looks like now and everything is working as expected again:
<Context docBase="${catalina.home}/webapps/manager" antiResourceLocking="false" privileged="true">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Context>
Hopefully this works for you as well! It was a pretty annoying issue that I scratched my head about for around an hour.
EDIT: Note: Of course, the location of your conf\Catalina\localhost\manager.xml file is probably different and may not be in your AppData\Roaming folder. To get the actual location you can grab it from the server properties in Tomcat. Go to Tools > Servers and look for the path listed in Catalina Base:

How to deploy multiple web application in tomcat which will run on different ports?

How to deploy multiple java web application in tomcat which will run on different ports ?
- How to do settings so that different web application will run on different ports
- What all needs to be done for achieving this?
You will need to setup another service in your server.xml file (tomcat_home/conf). If you havent changed your server file, you should already have one named Catalina (I am using Tomcat 5.5, you may have something slightly different depending on version)
<Service name="Dev2">
<Connector port="8090" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
<Connector port="8092"
enableLookups="false" redirectPort="9443" protocol="AJP/1.3" />
<Engine name="Dev2" defaultHost="MyDev">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Host name="MyDev" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
</Host>
</Engine>
</Service>
Notice that the names have changed from Catalina to Dev2, and localhost to MyDev. Change these to whatever you seem fit for your application. The ports and connectors have also changed.
Once the new Service is setup, you then need to deploy applications to the proper Service/Port. You accomplish this by using XML files under (See Virtual Hosting )
Tomcat_Home/conf/Catalina/localhost/
and
Tomcat_Home/conf/Dev2/MyDev/
for the respective ports which you are setting up
At this point all you have to do is add a few more files to point the Service to your application.
As an Example, under Tomcat_Home/conf/Dev2/MyDev/ I have a file called Another.xml This file contains the following
<Context path="/" docBase="C:/to_delete" debug="10" crossContext="false">
</Context>
Now I can access the new application using the web address http://127.0.0.1:8090/Another
If I try and access this using my default port of 8080, I get an error as the application was not deployed for that given port.
Few things to note about this setup. If you use VirtualVM to look at the application, you will notice that they share the same process ID. Therefore you have to be extra careful of your resources. They will be using the same Heap space, and all the threads will be showing in the same list. If you have logging in your applications (i.e Log4j) ensure you have an option to show which thread was doing the work, as it may be tough to tell otherwise which port/application this would be coming from.
As Bozho has already pointed out, It may be easier to simply have two instances of Tomcat running instead of one server listening on multiple ports.
You'd better have multiple tomcat installations. It would be easier.
I guess you can register multiple <Connector>s in server.xml, and then filter out the contexts, but that's tedious and sounds wrong.
Sorry about making this an answer. I don't see any commenting ability for me on this question. Mabye the question is too old or my reputation is not high enough.
However, I have been researching the same question myself. You will have to know a lot more about how Tomcat, http servers, and the Java system environment to use the same instance. I have read where it is also VERY slow also.
The best bet is separate instances. There are two fairly easy ways to do that:
A/ For Ubuntu, you can use SVN to get this script:
http://ubuntuforums.org/showthread.php?t=1211517
http://code.google.com/p/tomcat-linux/
B/ Your own, per user instances.
http://brian.pontarelli.com/2007/09/17/multiple-tomcat-instances-on-ubuntu/
The last one was written for tomcat 5.5, but is probably adaptable to Tomcat 6
However, the best directions for multiple JVM instances for the latest Tomcat on Linux is here:
http://www.puschitz.com/InstallingTomcat.html
You can use mod-proxy in apache to redirect the custom port to the standard one.
mod proxy

Prevent tomcat from starting application on deploy

Is there any way to tell Tomcat not to automatically start application which I want to deploy? I'd like to this manually.
In CATALINA_HOME/conf/server.xml:
<Host appBase="webapps" autoDeploy="false" name="localhost" unpackWARs="true"
xmlNamespaceAware="false" xmlValidation="false">
Note the autoDeploy="false"
I understand your question as not deploying your app on starting Tomcat - if so,
At the <Host> in server.xml, deployOnStartup attribute set to false should do it.
But that will affect all webapps on that server. The default is true
For future reference, since at least Tomcat 7.0 every container has an undocumented property startChildren (cf. source code), which decides whether children should be automatically started.
Setting:
<Host startChildren="false" />
will prevent auto-deployed applications to start automatically.
Remark: this only applies to auto-deployed applications (i.e. those added to the StandardHost after it already started). The applications configured in server.xml will start automatically, when the StandardHost starts.

Categories

Resources