VirtualHost in Tomcat (Make Tomcat a standalone web server)

Problem: I want to bind a registered domain name to my web application using Apache Tomcat as a standalone web server(means NO Apache web Server, HTTPD), simple :)
Environment:
  • Windows Server (64) 2003
  • Apache Tomcat 6.0.18
  • IIS 6

Still recently what were we doing to access the web application is,
we are binding domain name "xyz.com" to the IIS which is running on port 80, and then this IIS redirects the page to local instance of tomcat, which is running on port 8080, as http://localhost:8080/WebApp
So when someone hits www.xyz.com the server redirects to the URL as http://xyz.com:8080/WebApp.
Now we need some changes into it
1.Remove port 8080 from URL (pretty easy)


Open CATLINA_HOME/conf/server.xml and search for number 8080 and replace by 80, save it and you're done. But wait, if you restarted the tomcat, it will throw exception, like ‘JVM_bind: port 80 is already in use’ and this is because of the IIS server running on that default port 80, so stop it...


2.Get the direct access to Tomcat, instead of IIS-Redirecting-Tomcat(Tomcat Virtual Host settings)


Virtual hosting is easy in a Tomcat, (though many people suggest me, to use Apache web server, HTTPD, which acts like IIS, and then redirect the request to tomcat, which I didn't understand it, yet :)),
We need some edits in following xml
Caution: Before modifying this files make sure you had backup this files, so that if anything goes wrong you need not worry about the original files.

a. Create a virtual host, which maps xyz.com to the correct application deployed. Note that the <Host> element MUST be configured inside <Engine> element, see Tomcat docs for more details.


[Caution: Do NOT copy-paste the code as it is, instead edit and write into the file]

<Host name="www.xyz.com" appbase="Mywebapps" unpackwars="true"
autodeploy="true" xmlvalidation="false" xmlnamespaceaware="false">
</Host>


Here, <Host> name is the DNS registered site name “xyz.com” and it will bind all the web application from folder “Mywebapps” to it
b. Create <Alias> element inside the <Host>, which takes the other format of site name and bind to the same virtual host.


<Host name="www.xyz.com" appbase="xyzwebapps" unpackwars="true"
autodeploy="true" xmlvalidation="false" xmlnamespaceaware="false">
<Alias>xyz.com</Alias>
</Host>

After this just rename your web application's war file as 'ROOT.war', so that's it context path changes from "YOUR_WEB_APP_NAME" to "/" and application can be accessed as 'www.xyz.com', simply.
So finally, this files/directory you got in 'xyzwebapps'


CATALINA_HOME/xyzwebapps/
./ROOT.war
./ROOT



Restart the server, hit http://www.xyz.com and you're good to go :)


PS: Here is the beautiful link to implement SSL on Tomcat, you just need a private key from your host provider. Its easy and straightforward.
http://blog.datajelly.com/company/blog/34-adding-ssl-to-tomcat.html

Comments

eugene said…
This is a great post - easy to follow and works. Thanks.
SagaR said…
@eugene
Thanks, thanks for the encouraging words. Great to knew that some one finds it useful.. and for my first "technical" post comment :)
Anonymous said…
how to add wild chatacter alias to host. exmaple "abc.xyz.com", in this 'abc' can become any thing. Is there any thing to complete this or shell we pass like '*.xyz.com'(its not working).
SagaR said…
@Anonymous
First thing first - You can use wild card notations in server.xml but whether it work or not, I don't know as of now.
Second thing, do you have 'abc.xyz.com' DNS entry, which is nothing but the subdomain for 'xyz.com' OR an appropriate entry in the C:\WINDOWS\system32\drivers\etc\hosts file for 'abc.xyz.com'. Unless you got subdomain entry, you cant do as you're expecting !!
Anonymous said…
Thanks for the post.
I have a similar problem.

I'm using a VPS and I want to run a Java Web App in Tomcat in .NET app in IIS in the same server. Both have two different domain names.

Currently I can access the apps as follow.
.NET - www.abc.com:8090
Java - www.xyz.com

But i don't want to type port number in .NET app.

Pls suggest...
SagaR said…
Hi,

Firstly, you can configure 2 hosts (domain) for 2 different web app dir in tomcat. See, http://tomcat.apache.org/tomcat-6.0-doc/config/host.html

But, I've no knowledge how to do this in IIS.

Secondly, you need some kind of connector to delegate requests from Tomcat to IIS or vice verse.

Also, look into Apache HTTPD server, it might give you some idea.
Subbu said…
hey i registered three subdomains as follows in my original domain called mysite.com

1)abc.mysite.com
2)pqr.mysite.com
3)xyz.mysite.com

how can i bind all these three urls with my three webApplications(abc, pqr,xyz) in tomcat server.

now i configured first app, but its working with url like abc.mysite.com:8400/abc/index.jsp for first app, where for 2nd app & third app its not working properly but working like

abc.mysite.com:8400/pqr & abc.mysite.com:8400/xyz


whats wrong with all these, am a beginner in all these.

if i change my tomcat port to 80, again prob with JVM already binded :(

please help me in details
SagaR said…
1. In order to configure 3 sub domain for same Tomcat instance, you have to add 3 element for each sub domain, like
[xml]
Host name="www.abc.site.com" appbase="abcapp">
Alias>abc.site.com/Alias>
/Host>
[/xml]

2. In order to access the web app as root app, means if you directly want to access site like, http://abc.site.com/m you have to create new webapp dir , like "abcapp" (see #1 point) and place ur abc.war and rename it to "ROOT.war" and start tomcat.

3. Looks like port 80 is already in use on ur server, find out which process occupied that port and kill it, then u can start tomcat on 80 port.

Popular Posts