How to Use Virtual Hosts in Tomcat
- Details
- Written by Nam Ha Minh
- Last Updated on 06 August 2019   |   Print Email
In this Java Tomcat tutorial, we'll guide you how to configure Apache Tomcat server to deploy and run multiple Java web applications under different hostnames, on a single server machine. For example, a project management application can be accessed via this URL:
http://projectx.com/
A payroll application is deployed to the following address:
http://payrollf.com/
And a bug tracking application can be accessed via this URL:
http://bugtrackingz.com/
And so on...Though the applications are deployed and accessed via diffrent hostnames, they are actually running on a single instance of Tomcat. You can set up this kind of deployment by configuring virtual hosts in Tomcat.
Follow these steps:
1. Update Server Configuration File
Open the server.xml file under the conf folder in Tomcat installation directory. Scroll this file to the end, and you see a <Host> section that looks like this:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host>
You see, this is the configuration for the default host - with hostname is locahost and the base directory is webapps:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
Now, to add another virtual host to this server, copy all the XML code in the <Host> section and change the properties name and appBase. For example:
<Host name="projectx.com" appBase="projectapp" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="project_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host>
2. Configure Host Name Resolution
Note that when you specify the hostname "projectx.com", you must also configure the operating system to resolve the hostname to IP address of the server. On Windows, you can do this by editing the hosts file under c:\Windows\System32\drivers\etc directory. Add the following line at the end of the file:
192.168.1.5 projectx.com
Of course you need to use the correct IP address of the server. Then save the hosts file.
3. Create Base Directory for the Virtual Host
The property appBase points to a directory that contains the web application's files. So create a new directory named projectapp under Tomcat installation directory.
4. Deploy the Java Web Application
Copy the web application's WAR file to projectapp directory, and rename it to ROOT.war.
Now, start the server. You will see Tomcat automatically unpacks the ROOT.war file to host the application. You now can access the application via this URL:
http://projectx.com
Note that in the <Host> element for the host "projectx.com", the <Vavle> element specifies the access log file:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="project_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
Navigate to the logs folder under Tomcat installation directory, you will the the project_access_log.txt has been reated.
That's how to add a virtual host in Tomcat. You can repeat the same steps for other applications.
The XML code for virtual hosts configuration in server.xml would look like this:
<Host name="projectx.com" appBase="projectapp" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="project_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="payrollf.com" appBase="payrollapp" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="payroll_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="bugtrackingz.com" appBase="bugtrackingapp" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="bugtracking_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host>
The host name resolution in hosts file would look like this:
192.168.1.2 projectx.com 192.168.1.2 payrollf.com 192.168.1.2 bugtrackingz.com
And the base directories for those web applications would look like this:
References:
Other Tomcat Tutorials:
- How to deploy a Java web application on Tomcat
- How to Embed Tomcat Server into Java Web Applications
- How to set web application context path as server root in Tomcat
- How to configure JNDI DataSource for Database Connection Pooling in Tomcat
- How to configure session timeout in Tomcat
- How to change Tomcat port number
- How to add Tomcat server in Eclipse
- How to change server location and deploy path for Tomcat in Eclipse
Comments
Thanks for sharing. I've seen other approaches that involve creating different physical directories associated with the same tomcat. The nice approach here is the /bin/setenv.sh are specific to each application instance. Is there a way, or does Tomcat have a precedence for reading setenv.sh from different locations?
Like localhost:8181/ords/f?p=payroll as payroll.com on my intranet tomcat
Please guide me
If I only deploy projectx.com, how to restart tomcat by vhost?
Thank you.