Step-by-Step Guide to Configure Tomcat Virtual Hosts on CentOS for Multiple Domains
This tutorial walks you through preparing separate site directories, editing Tomcat's server.xml to define Host entries, restarting the service, and testing each domain, while also covering firewall, DNS, and production‑grade security considerations for deploying multiple websites on a single CentOS server.
Introduction
The article explains how to host multiple websites on a single CentOS server using Tomcat virtual hosts, providing a complete, hands‑on walkthrough from directory preparation to testing.
1. Prepare Virtual Host Directories
Create a distinct directory for each site and place a simple test page.
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 $CATALINA_HOME/conf/server.xml and locate the <Engine> element (usually under <Service name="Catalina">). Add a <Host> block for each site.
<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>Configuration Explanation
name: domain name of the virtual host (e.g., www.site1.com). appBase: set to an empty string because docBase points directly to the site directory. docBase: absolute path to the site’s document root ( /var/www/site1 or /var/www/site2). path: set to "" so the host serves at the root URL ( /).
3. Restart Tomcat
Apply the new configuration by stopping and starting Tomcat:
$CATALINA_HOME/bin/shutdown.sh
$CATALINA_HOME/bin/startup.sh4. Test the Virtual Hosts
Visit the domains in a browser:
http://www.site1.com:8080 → should display Welcome to Site 1 http://www.site2.com:8080 → should display
Welcome to Site 25. Additional Considerations
Firewall
If Tomcat runs on a non‑standard port, open it in the firewall:
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reloadDNS
In production, ensure the domain names ( www.site1.com, www.site2.com) resolve to the server’s public IP.
Production Recommendations
Use Apache or Nginx as a reverse proxy to forward requests to Tomcat and terminate HTTPS for security.
Conclusion
Configuring Tomcat virtual hosts is straightforward once you understand the <Engine> and <Host> elements. Pay attention to correct docBase paths, firewall rules, DNS settings, and consider a reverse proxy for a hardened production 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.
