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.
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.
- 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.
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:
Virtual hosts allow us to configure multiple domains on a single server. This tutorial helped you to configure virtual hosts on Tomcat server.
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/
how many subdomain we can add
You can add the unlimited subdomain
ubuntu server 16, tomcat 7, guacamole
how to change http://ip_address:8080/guacamole TO http://serverguacamole.com
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.
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..?
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.
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…
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” />