Home > Article > Operation and Maintenance > How to create VirtualHost (virtual host) in Tomcat
This article introduces how to create a VirtualHost (virtual host) in Tomcat. Let’s take a look at the specific content.
Why choose a virtual host?
Virtual hosting allows us to host multiple domains (websites) on one server. It is the concept of resource sharing between multiple hosting accounts. The best use of web hosting is a shared hosting server where multiple users can host multiple websites on a single server.
Installation Details
We have created a Linux server with IP 192.168.1.100 for the Tomcat hosting service. Tomcat 8 is installed and configured to run on port 80. After that we deployed two java web applications on tomcat using Tomcat Admin panel. Now both the applications are running on the following URL
http://192.168.1.100/myapp1 http://192.168.1.100/myapp2
Now we want to run these two web applications on the main domain name such as example.com and mydomain.org. This allows end users to access the web application using the primary domain name.
Create a virtual host in Tomcat
To create a virtual host in Tomcat, first, find the Tomcat installation directory, and then edit config/server in the favorites editor .xml or conf/server.xmlfile. Then create a virtual host for the application. The following virtual hosts include:
The first application domain name is example.com and the /opt/tomcat/webapps/myapp1 document root.
The second application uses the domain name mydomain.org and the /opt/tomcat/webapps/myapp2 document root.
<Host name="example.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Alias>www.example.com</Alias> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="example_access_log" suffix=".txt" pattern="%h %l %u %t %r %s %b" /> <Context path="" docBase="/opt/tomcat/webapps/myapp1" debug="0" reloadable="true"/> </Host> <Host name="mydomain.org" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Alias>www.mydomain.org</Alias> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="mydomain_access_log" suffix=".txt" pattern="%h %l %u %t %r %s %b" /> <Context path="" docBase="/opt/tomcat/webapps/myapp2" debug="0" reloadable="true"/> </Host>
Restart the Tomcat service
After adding the virtual host in Tomcat, we need to restart the Tomcat service. Restart it using tomcat init, or if you do not have to initialize the service for tomcat, execute the following command from the tomcat installation directory.
After adding the virtual host in Tomcat, we need to restart the Tomcat service. Restart it using Tomcat init service, or if you do not have to execute init service for Tomcat, you can execute the following command from the tomcat installation directory.
# ./bin/shutdown.sh # ./bin/startup.sh
[Related recommendations: Linux video tutorial]
The above is the detailed content of How to create VirtualHost (virtual host) in Tomcat. For more information, please follow other related articles on the PHP Chinese website!