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.

Advertisement

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:

  1. Edit the server.xml file in your favorite text editor. This file exists under the conf directory of tomcat installation.
  2. Go to the end of the file and you will get the default virtual host configuration as shown below:
    Creating Virtual Host in Tomcat
    Default virtual host settings in Tomcat
  3. 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.

  4. The second virtual host will be created for a subdomain demo.example.com with /opt/tomcat/webapps/myapp2 document root.
  5. The configuration file will look like this:
    Virtual hosts configuration in Tomcat
  6. 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:

./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.

Share.

19 Comments

  1. Hi Rahul ,

    Can you please post about apache web server configuration and integration with tomcat instance for hosting a website.

    Thanks in Advance,

  2. Nithin Krishna on

    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

  3. 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.

  4. 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.

  5. Ananda Bhattacharjee on

    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” />

Exit mobile version