How to Set Up Multiple Domains on Tomcat in CentOS: Step‑by‑Step Guide
Learn how to configure Tomcat virtual hosts on a CentOS server to host multiple websites with distinct domain names, covering directory preparation, server.xml modifications, firewall and DNS settings, testing, and restart procedures, complete with code snippets and practical tips.
Tomcat Virtual Host Configuration: Multi‑Domain Deployment on CentOS
When working with Tomcat, you may need to run multiple websites on a single server, each accessed via a different domain name. This guide walks through the complete process of setting up virtual hosts on Tomcat in a CentOS environment.
1. Prepare Directories and Test Files
Create separate directories for each virtual host and place a simple test page.
Create Directories
mkdir -p /var/www/site1
mkdir -p /var/www/site2Add Test Files
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 in $CATALINA_HOME/conf and locate the <Engine> element under <Service name="Catalina">. Add two <Host> blocks, one for each domain.
Modified Configuration Example
<Engine name="Catalina" defaultHost="localhost">
<!-- Default localhost configuration -->
<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 Details
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 document base is specified directly with docBase. docBase: the root directory for the virtual host, respectively /var/www/site1 and /var/www/site2. path: set to "", meaning the virtual host serves at the root path /.
The Engine node is a core component that manages multiple Host entries.
3. Access Test
4. Restart Tomcat
$CATALINA_HOME/bin/shutdown.sh
$CATALINA_HOME/bin/startup.sh5. Test Virtual Hosts
In a browser, 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.
Notes
Firewall: If Tomcat runs on a non‑default port (e.g., 8080), ensure the port is allowed. Example:
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reloadDNS: In production, configure the domain names ( www.site1.com, www.site2.com) to resolve to the server’s public IP.
Production recommendation: Use Apache or Nginx as a reverse proxy, forward domain requests to Tomcat, and enable HTTPS for security.
Summary
Confirm that the docBase directories exist and have correct permissions.
Open the necessary firewall ports; otherwise external access will fail.
Ensure DNS records point to the server’s public IP for each domain.
For security, place Tomcat behind a reverse proxy (Apache/Nginx) and enable HTTPS.
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.
