How to Set Up Multiple Domains on Tomcat in CentOS: Step‑by‑Step Virtual Host Guide
Learn how to configure Tomcat virtual hosts on a CentOS server to host multiple websites with different domain names, covering directory preparation, server.xml modifications, restarting Tomcat, testing access, firewall and DNS considerations, and essential tips for a reliable deployment.
1. Prepare directories and content for virtual hosts
Create a separate directory for each virtual host and place a simple test file inside.
mkdir -p /var/www/site1
mkdir -p /var/www/site2Create test files
echo "Welcome to Site 1" > /var/www/site1/index.html
echo "Welcome to Site 2" > /var/www/site2/index.html2. Modify Tomcat's server.xml
Edit $CATALINA_HOME/conf/server.xml, locate the <Engine> element under <Service name="Catalina">, and add two <Host> blocks for the virtual hosts.
Example configuration
<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: the domain name of the virtual host, e.g., www.site1.com and www.site2.com. appBase: set to an empty string because the docBase is specified directly. docBase: the document root directory for each virtual host ( /var/www/site1 and /var/www/site2). path: set to "" to indicate the root path (/).
3. Test access
After restarting Tomcat, open a browser and visit the following URLs: http://www.site1.com:8080 – should display "Welcome to Site 1". http://www.site2.com:8080 – should display "Welcome to Site 2".
4. Restart Tomcat
$CATALINA_HOME/bin/shutdown.sh
$CATALINA_HOME/bin/startup.sh5. Additional considerations
Firewall
If Tomcat runs on a non‑default port, ensure the port is allowed through the firewall.
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reloadDNS
In a production environment, configure DNS records for www.site1.com and www.site2.com to point to the server's public IP address.
Production tips
Use Apache or Nginx as a reverse proxy to forward domain requests to Tomcat and enable HTTPS for security.
Avoid exposing Tomcat directly to the internet; the reverse proxy adds an extra layer of protection.
Conclusion
Configuring Tomcat virtual hosts is not difficult once you pay attention to directory paths, firewall rules, and DNS settings; careful configuration prevents common pitfalls.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
