How to Configure Multiple Domain Virtual Hosts on Tomcat in CentOS – Step‑by‑Step Guide
This tutorial walks you through setting up separate directories for each site, editing Tomcat's server.xml to add Host entries, configuring docBase and path attributes, restarting the service, testing the domains, and handling firewall and DNS considerations for a production‑ready multi‑domain Tomcat deployment on CentOS.
Introduction
The article explains how to run multiple websites on a single Tomcat server in a CentOS environment by configuring virtual hosts, each accessed via a distinct domain name.
1. Prepare virtual‑host directories
Create independent directories for each site and place a simple test file.
mkdir -p /var/www/site1
mkdir -p /var/www/site2
echo "Welcome to Site 1" > /var/www/site1/index.html
echo "Welcome to Site 2" > /var/www/site2/index.html2. Modify server.xml
Edit the server.xml file located under $CATALINA_HOME/conf and locate the <Engine> element (usually inside the <Service name="Catalina"> block). Add a <Host> element for each virtual host.
3. Configuration example
<Engine name="Catalina" defaultHost="localhost">
<!-- Default localhost -->
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="${catalina.home}/webapps/ROOT" />
</Host>
<!-- Virtual host 1 -->
<Host name="www.site1.com" appBase="" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/var/www/site1" />
</Host>
<!-- Virtual host 2 -->
<Host name="www.site2.com" appBase="" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/var/www/site2" />
</Host>
</Engine>4. Restart Tomcat
Apply the new configuration by shutting down and starting Tomcat again.
$CATALINA_HOME/bin/shutdown.sh
$CATALINA_HOME/bin/startup.sh5. Test the virtual hosts
Open a browser and visit the configured domains (replace with real DNS or hosts‑file entries):
http://www.site1.com:8080 – should display Welcome to Site 1 http://www.site2.com:8080 – should display
Welcome to Site 26. Additional considerations
Firewall
If Tomcat runs on a non‑default port, ensure the firewall allows traffic.
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reloadDNS
In production, configure DNS records so www.site1.com and www.site2.com resolve to the server’s public IP.
Production recommendations
Use Apache or Nginx as a reverse proxy to forward domain requests to Tomcat and to enable HTTPS.
Do not expose Tomcat directly to the internet; the proxy adds a security layer.
Conclusion
Configuring Tomcat virtual hosts is straightforward once you create the site directories, correctly set docBase, adjust name and path attributes, open the necessary firewall ports, and configure DNS. Adding a reverse proxy such as Nginx or Apache further secures the deployment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
