Skip to main content

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.
techmythoughts 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).
techmythoughts 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...
techmythoughts 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
techmythoughts 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.
Absecurity said…
I really like how you presented your facts and how you made it interesting and easy to understand. Thank you.

The article posted was very informative and useful. You people are doing a great job. Keep going.
Masjid sound system

Popular posts from this blog

Installing i3lock-color on Ubuntu

i3lock is fancy lock screen for Linux. There is i3lock dep available on Ubuntu but its not what I was looking for; I was more interested in i3lock-color . Lets see how we can install the same on ubuntu. PS: I'm using Ubuntu 16.04 Get source code for i3lock-color $ git clone https://github.com/PandorasFox/i3lock-color.git  $ cd i3lock-color Install required packages to build i3lock  $ apt install libev-dev $ apt install libxcb-composite0 libxcb-composite0-dev libxcb-xinerama0 libxcb-randr0  $ apt install libxcb-xinerama0-dev  $ apt install libxcb-xkb-dev  $ apt install libxcb-image0-dev  $ apt install libxcb-util-dev $ apt install libxkbcommon-x11-dev $ apt install libjpeg-turbo8-dev  $ apt install libpam0g-dev Build  $ autoreconf -i && ./configure && make Execute $ ./lock.sh Assign Shortcut In order to assign shortcut, install compizconfig $ sudo apt-get install compizconfig-settings-manager compiz-plugins-extra and then go

INTO OUTFILE query, How to modify & output null values ?

I tried one query (originally asked at JR) on MySQL which export the table data into a file, with a comma delimited field. The query is straight forward and replace the null column value by "\N" character, but when I want to replace that "\N" value by some other character like "0" or simple empty field like " ", its gets little tricky. Here is the base query SELECT Id, name, age FROM student INTO OUTFILE 'c:/result.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'; And it outputs like, 1, "sagar", 23 2, "anant", 24 Now suppose some 'name' fields are null, then it outputs like 1, "sagar", 23 2, \N, 24 Now to obtain my desired results, which replace this null (\N) values by empty string like, "", I tried out two solutions --1. Using CASE statement SELECT Id, CASE WHEN name IS NULL THEN '' ELSE name END AS NewName, age FROM s

GlassFish V3 admin console taking too much time to load.

If you have installed Glassfish V3 and trying to load admin console, but after signing in, is it taking too much time to get to the main page ? Do you have server.log entry like this: admin console: initSessionAttributes() Cannot refresh Catalog : Connection timed out then its time to tweak some files. Here its how: 1. Update the %GLASSFISH_HOME/glassfish/domains/domain1/domain.xml -Dcom.sun.enterprise.tools.admingui.NO_NETWORK=true This will block up the News item, the registration item, etc 2. Remove update tool jar (Backup and remove this JAR) %GLASSFISH_HOME/glassfish/modules/console-updatecenter-plugin.jar Delete this dir: %GLASSFISH_HOME/glassfish/domains/domain1/osgi-cache %GLASSFISH_HOME/glassfish/domains/domain1/generated Now start the server (bin/asadmin start-domain) and you will see the admin console won't be hang up and take you directly to main page.