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.