Operations 6 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Configure Multiple Domain Virtual Hosts on Tomcat in CentOS – Step‑by‑Step Guide

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.html
Directory structure
Directory structure

2. 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>
server.xml configuration
server.xml configuration

4. Restart Tomcat

Apply the new configuration by shutting down and starting Tomcat again.

$CATALINA_HOME/bin/shutdown.sh
$CATALINA_HOME/bin/startup.sh

5. 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 2
Browser test results
Browser test results

6. 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 --reload

DNS

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ServerConfigurationVirtualHost
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.