ThisCalculator

Everyday

Map Tomcat Web App to Domain Name: Step-by-Step

Updated · Standard formula, shown below

This guide walks you through connecting a Tomcat web application to a custom domain name, so visitors can reach it via a clean URL like www.example.com instead of http://server:8080/app. You will need a registered domain, a server with Tomcat installed, and access to its configuration files and DNS settings.

What You’ll Need

  • A registered domain name (e.g., example.com) with access to its DNS management panel.
  • A server (physical, virtual, or cloud) with Tomcat installed and running.
  • Root or sudo access to the server to edit Tomcat and system files.
  • A web application deployed in Tomcat (e.g., a WAR file in webapps/).
  • Port 80 (HTTP) and optionally port 443 (HTTPS) available on the server firewall.

Step-by-Step

  1. Point your domain to your server’s IP address. In your DNS provider’s control panel, create an A record for @ (or the subdomain like www) with the public IP address of your Tomcat server. If you use a subdomain like app.example.com, create an A record for app with that IP. Wait for DNS propagation (can take minutes to hours).
  2. Decide on the URL structure. You can map the domain to the root of your app (e.g., example.com serves myapp), or to a specific path (e.g., example.com/myapp). This guide covers mapping to the root, which is the most common.
  3. Edit Tomcat’s server.xml. Locate the Connector for HTTP (usually port 8080) and add the proxyName and proxyPort attributes if you plan to use a reverse proxy (recommended). For direct Tomcat on port 80, you can change the port to 80 directly.
  4. Option A: Direct Tomcat on port 80 (no reverse proxy). In server.xml, change the HTTP Connector’s port from 8080 to 80. Then, set the appBase of the Host to your webapps folder and ensure the Host has name="example.com" (or your domain). Deploy your app as ROOT.war (rename your WAR to ROOT.war) so it serves at the root context. Restart Tomcat.
  5. Option B: Use a reverse proxy (like Apache or Nginx) – recommended. Install Nginx or Apache on the same server. Configure it to listen on port 80 and proxy requests to Tomcat’s port 8080. For example, in Nginx, create a server block with server_name example.com; and location / { proxy_pass http://localhost:8080; }. This allows Tomcat to keep running on 8080 and handles static files, SSL, and load balancing more flexibly.
  6. Configure the Tomcat Host. In server.xml, inside the Engine, add a Host element with name="example.com" and appBase="webapps". If you use a reverse proxy, set the Connector’s proxyName="example.com" and proxyPort="80" (or 443 for HTTPS) so Tomcat generates correct URLs in redirects and links.
  7. Deploy your app as ROOT. To serve your app at the domain root, remove any existing ROOT folder in webapps and place your WAR file renamed to ROOT.war (or unpack the app into a ROOT folder). If you want the app at a subpath, skip this and keep the app’s name; then access it at example.com/appname.
  8. Set up a firewall and port forwarding. Ensure your server’s firewall allows incoming connections on port 80 (and 443 if using HTTPS). If your server is behind a router, forward ports 80/443 to the server’s internal IP.
  9. Test the mapping. Restart Tomcat (and the reverse proxy if used). Open a browser and go to http://example.com – you should see your application.
  10. Add HTTPS (optional but recommended). Use a free certificate from Let’s Encrypt via Certbot, or configure your own SSL certificate in Tomcat’s server.xml (add a Connector with SSLEnabled="true" and the keystore). Update the proxy to forward HTTPS to Tomcat with the appropriate proxyPort="443".

Common Mistakes

  • Forgetting to change the context path: If you don’t deploy as ROOT.war, your app will still be at example.com/appname, not the root.
  • Using the wrong port: Tomcat defaults to 8080; if you don’t change it or proxy correctly, visitors on port 80 won’t reach it.
  • DNS not propagated: Even after setting A records, DNS can take time to update. Test with ping example.com or use an online DNS checker.
  • Not setting proxyName: If using a reverse proxy and you don’t set proxyName and proxyPort, Tomcat may generate redirect URLs with localhost:8080.
  • Firewall blocking: Even if Tomcat listens on 80, the OS firewall may block it. Open the port with ufw allow 80 (Ubuntu) or firewall-cmd --add-port=80/tcp (CentOS).

Tips

  • Use a reverse proxy for production: it handles static files, SSL, and load balancing more efficiently than Tomcat alone.
  • Set the Host name to your domain to avoid Tomcat’s default localhost being used in generated links.
  • If you have multiple apps, consider using different subdomains (e.g., app1.example.com, app2.example.com) and separate Hosts or proxy rules.
  • Always back up server.xml before editing.
  • Use a DNS provider that supports fast propagation, and set a low TTL (e.g., 300 seconds) during setup to speed up testing.

Troubleshooting

  • Site not loading: Check that DNS resolves correctly (dig example.com). Verify the server’s public IP matches the A record. Ensure Tomcat is running and listening on the correct port (netstat -tulpn | grep java).
  • Connection refused: This usually means the port is not open. Check firewall rules and that Tomcat is actually listening on that port. If using a proxy, ensure it’s running and configured correctly.
  • App loads but with wrong URLs: If links point to localhost:8080, you forgot to set proxyName and proxyPort on the Connector. Add them and restart.
  • 404 error: If you see a 404 from Tomcat, the app isn’t deployed as ROOT. Rename the WAR to ROOT.war and restart, or access the app at /appname.
  • DNS works but site still shows default Tomcat page: That means Tomcat is running but your app isn’t in the root context. Check the webapps folder for a ROOT directory or WAR.
  • HTTPS issues: If using a proxy and SSL, ensure the proxy forwards the correct protocol headers. In Nginx, add proxy_set_header X-Forwarded-Proto $scheme; and in Tomcat set scheme="https" and secure="true" on the Connector.

Frequently Asked Questions

  1. Can I map multiple domains to the same Tomcat app? Yes, you can add multiple Host elements in server.xml with different names, or configure multiple server blocks in your reverse proxy to point to the same Tomcat instance.
  2. Do I need to change the port from 8080 to 80? Not necessarily. Using a reverse proxy on port 80 is safer and more flexible. Directly changing to 80 works but requires root privileges and may conflict with other services.
  3. What if my domain has a subpath like example.com/myapp? You can keep the app’s default context path. Just ensure the proxy forwards the path correctly, and no special Tomcat configuration is needed.
  4. How do I get an SSL certificate for my Tomcat app? Use Let’s Encrypt with Certbot, which can automatically configure your reverse proxy. For Tomcat alone, you can import the certificate into a Java keystore and configure the HTTPS Connector.
  5. Why does my app show the IP address in the URL after a redirect? This is because Tomcat doesn’t know the external domain. Set proxyName and proxyPort on the Connector to the external domain and port.
  6. Can I use a subdomain like app.example.com? Yes, just create an A record for that subdomain and use it as the Host name or server_name in your proxy.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.