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.
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
- Log in to your server with SSH, using root.
- 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.
- 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.
- Open
server.xmlin a text editor such as nano. - Find the
<Engine>section. - 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.
- Put each site's app inside its folder. To show as the main site, name the app folder
ROOT. - 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
Hostblock inserver.xmladds a domain. - Give each domain its own folder.
- Back up first, then restart Tomcat.
- Make sure each domain's DNS points to your server.