Virtual Hosting provides us to host multiple domains (websites) on a single server. It is a concept of resource sharing among multiple hosting accounts. The best use of Virtual hosting is shared hosting servers, where multiple users can host multiple websites on a single server.
This blog post will help you to create virtual hosts in the Tomcat web server.
Setup Details
Here are the basic setup details used in this article:
- A Linux host with IP address 192.168.1.100
- Running the Tomcat server on the host on port 80
- Deployed two sample applications, the first application is configured at http://192.168.1.100/myapp1
- The second application is configured at http://192.168.1.100/myapp2
The task is to configure virtual hosts for both applications. The first virtual host uses the domain example.com and the second virtual host will use a subdomain demo.example.com.
Create Virtual Hosts in Tomcat
Tomcat stores the virtual host configurations under the server.xml file. The default ‘localhost’ virtual host is configured in Tomcat servers. Now we have to add two more virtual hosts as below:
- Edit the server.xml file in your favorite text editor. This file exists under the conf directory of tomcat installation.
- Go to the end of the file and you will get the default virtual host configuration as shown below:
- Create the first virtual host for the domain example.com with the document root /opt/tomcat/webapps/myapp1. You can also add an Alias directive to set up www.example.com.12345678910<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>
- The second virtual host will be created for a subdomain demo.example.com with /opt/tomcat/webapps/myapp2 document root.
- The configuration file will look like this:
- Save changes and close the file.
1 2 3 4 5 6 7 8 9 | <Host name="demo.example.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="demo_example_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 Tomcat Service
Once you made changes to the Tomcat configuration file, it’s required to restart the service to apply changes. If the Tomcat is running as a systemd service, use the following command to restart.
sudo systemctl restart tomcat
If you are running tomcat with the shell scripts. use the following commands:
./bin/shutdown.sh
./bin/startup.sh
Conclusion
Virtual hosts allow us to configure multiple domains on a single server. This tutorial helped you to configure virtual hosts on Tomcat server.
19 Comments
it is possible to use ip base url after virtual hosting like host-example.com ip-192.168.12.22 so, can we use url http://192.168.12.22 instead http://example.com
hi can i access my war file without any url ..?? Like:-> localhost:8080/
hi
how many subdomain we can add
You can add the unlimited subdomain
please help
ubuntu server 16, tomcat 7, guacamole
how to change http://ip_address:8080/guacamole TO http://serverguacamole.com
thank
Hi Rahul ,
Can you please post about apache web server configuration and integration with tomcat instance for hosting a website.
Thanks in Advance,
Thanks Srikanth to remind me, even I had worked with mod_jk and want to write for it.
Hi
How can i create sub domain in the tomcat with the above example
i need a subdomain like “test.example.com”
so how will be the configuration like..?
Thanks Nithin
Hi Nithin, Use configuration like below.
can any1 share server.xml.. what need to be done on connctor?
I have followed the same but I am getting the Tomcat Home page, while browsing my sub-domain.
Hello,
Pattern should be as fallow:
pattern=”%h %l %u %t "%r" %s %b” />
without dots 😉
pattern=”%h %l %u %t .&.quot.;%r.&.quot.; %s %b” />
I have updated tutorial.
After writing this code, my tomcat won’t start…
Hi John,
The double quote was causing the issue on Tomcat startup. I have fixed in above tutorial.
This is not working!
The moment I start the tomcat server it goes off!
I am not an expert but I think there should be more configurations.
I think that for different Host’s, you should not have the appBase’s overlapping. Otherwise a client hitting one server name might be able to access the content of the other server. This is despite the fact that the docBase’s are different. Not an expert but that is my experience. And the official documentation says that they should not overlap.
pattern=”%h %l %u %t “%r” %s %b” />
Please remove double quotes from pattern “%r”
& It should look like
pattern=”%h %l %u %t %r %s %b” />