How to Create VirtualHost in Tomcat 7/8
Why VirtualHost ?
Virtual Hosting provides us to host multiple domains (websites) on single server. It is an concept of resource sharing between multiple hosting account. The best use of Virtual hosting is shared hosting servers, where multiple users can host multiple websites on single server.
Setup Details:
We have created a Linux server with ip 192.168.1.100 for Tomcat hosting services only. So install Tomcat 8 and configured to run on port 80. After than we have deployed two java web applications on tomcat using Tomcat Admin panel. Now both applications are running on following urls
http://192.168.1.100/myapp http://192.168.1.100/myapp2
Now we want to run both web applications on main domains ( example.com & mydomain.org ). So that end users can access web application by using main domain name.
Create Virtual Hosts:
To create virtual hosts in Tomcat, first navigate to Tomcat installation directory and edit config/server.xml file in favorite editor. Add following host entries in configuration file.
<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/tomcat8/webapps/myapp " 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/tomcat8/webapps/myapp2 " debug="0" reloadable="true"/> </Host >
Restart Tomcat Service
After adding virtualhost in Tomcat, we need to restart Tomcat service. Use tomcat init service to restart it or if you don’t have init service for tomcat, execute following commands from tomcat installation directory.
# ./bin/shutdown.sh # ./bin/startup.sh
Tidak ada komentar:
Posting Komentar