Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Host Multiple Domain Names on an Apache Tomcat Server

This guide shows you how to serve more than one website from a single Tomcat server. It needs root access, so it fits a VPS or dedicated server.

Domains and DNS2 min read8 steps

What is Tomcat?

Apache Tomcat is a program that runs Java web applications. A Java web application is a website built with the Java language. Tomcat listens for visitors and sends them the right page.

What is a virtual host?

A virtual host lets one server act like many. Imagine one building with several shops inside. Each shop has its own sign and its own goods. In Tomcat, each domain gets its own virtual host, called a Host.

Warning: A mistake in the config file can stop Tomcat from starting. Back up the file before you edit it.

Before you begin

  • Point each domain to your server's IP address using its DNS settings.
  • Know where Tomcat is installed. We call that folder CATALINA_HOME. A common path is /opt/tomcat, but yours may differ.

Steps

  1. Log in to your server with SSH, using root.
  2. Back up the config file.
cp /opt/tomcat/conf/server.xml /opt/tomcat/conf/server.xml.bak

This makes a safe copy of the file.

  1. Make a folder for each site.
mkdir -p /opt/tomcat/sites/example1.com
mkdir -p /opt/tomcat/sites/example2.com

These folders will hold the files for each domain.

  1. Open server.xml in a text editor such as nano.
  2. Find the <Engine> section.
  3. Add one <Host> block for each domain, inside the Engine.
<Host name="example1.com" appBase="/opt/tomcat/sites/example1.com"
      unpackWARs="true" autoDeploy="true">
</Host>
<Host name="example2.com" appBase="/opt/tomcat/sites/example2.com"
      unpackWARs="true" autoDeploy="true">
</Host>

Each block ties a domain name to its own folder.

  1. Put each site's app inside its folder. To show as the main site, name the app folder ROOT.
  2. Save the file and restart Tomcat.
systemctl restart tomcat

This restarts the service. Your service may have a different name.

Check it

Open each domain in a browser. Each should show its own site. If one does not, check the DNS and the name spelling.

Quick recap

  • Tomcat runs Java websites.
  • A Host block in server.xml adds a domain.
  • Give each domain its own folder.
  • Back up first, then restart Tomcat.
  • Make sure each domain's DNS points to your server.