Hi in this post we will see how to setup virtual host in Apache Tomcat server. Virtual Host is in-built feature that allows to deploy multiple website(domains) in single instance of tomcat server. The main benefit in this way is its cost effective.
I am going to deploy 3 website with following domain names in single tomcat http://www.ramki.com http://www.krishnan.com http://www.blog.ramki.com
The following diagram is my outline.
Outline structure of Virtual Host Concept in Tomcat
Here my tomcat IP address 192.168.1.15. or any IP address allocated my ISP. but it should be public IP address.
How all domain names are pointing to my Tomcat?
When we purchase the domain name we need to update the our tomcat IP address to it. like
or we can simulate same DNS Setup through hosts file in both Linux and Windows. In Linux tha file is located at /etc/hosts
Now How Setup Virtual Host Concept?
Before going to setup the virtual host. first take look at the server.xml file in conf folder in tomcat directory.
here <Engine> tag specified which engine is responsible for executing servlet. Here Catalina is the Engine. <Host> tag specify the domain name and web apps base location. here default domain name is localhost and web apps base location is webapps folder in tomcat directory. here name attribute to specify the domain name and appbase attribute to specify the location of domain specific web apps folder path.
Now we need to add more <Host> tags to represent to our domains
<Host name="www.ramki.com" appbase="ramki_webapps"/> <Host name="www.krishnan.com" appbase="krishnan_webapps" /> <Host name="www.blog.ramki.com" appbase="blog_webapps" />
Then we need to copy the webapps folder in tomcat and paste it anywhere and rename it to ramki_webapps, krishnan_webapps, blog_webapps and update the path in <Host> tag
Modifies server.xml file
Simulate the DNS
Open the /etc/hosts file through root privilege and add following entry