Map Tomcat Web App to Domain Name: Step-by-Step
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
- Point your domain to your server’s IP address. In your DNS provider’s control panel, create an
A recordfor@(or the subdomain likewww) with the public IP address of your Tomcat server. If you use a subdomain likeapp.example.com, create an A record forappwith that IP. Wait for DNS propagation (can take minutes to hours). - Decide on the URL structure. You can map the domain to the root of your app (e.g.,
example.comservesmyapp), or to a specific path (e.g.,example.com/myapp). This guide covers mapping to the root, which is the most common. - Edit Tomcat’s
server.xml. Locate theConnectorfor HTTP (usually port 8080) and add theproxyNameandproxyPortattributes if you plan to use a reverse proxy (recommended). For direct Tomcat on port 80, you can change the port to 80 directly. - Option A: Direct Tomcat on port 80 (no reverse proxy). In
server.xml, change the HTTP Connector’s port from8080to80. Then, set theappBaseof the Host to your webapps folder and ensure theHosthasname="example.com"(or your domain). Deploy your app asROOT.war(rename your WAR toROOT.war) so it serves at the root context. Restart Tomcat. - 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;andlocation / { proxy_pass http://localhost:8080; }. This allows Tomcat to keep running on 8080 and handles static files, SSL, and load balancing more flexibly. - Configure the Tomcat Host. In
server.xml, inside theEngine, add aHostelement withname="example.com"andappBase="webapps". If you use a reverse proxy, set the Connector’sproxyName="example.com"andproxyPort="80"(or 443 for HTTPS) so Tomcat generates correct URLs in redirects and links. - Deploy your app as ROOT. To serve your app at the domain root, remove any existing
ROOTfolder inwebappsand place your WAR file renamed toROOT.war(or unpack the app into aROOTfolder). If you want the app at a subpath, skip this and keep the app’s name; then access it atexample.com/appname. - 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.
- 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. - 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 aConnectorwithSSLEnabled="true"and the keystore). Update the proxy to forward HTTPS to Tomcat with the appropriateproxyPort="443".
Common Mistakes
- Forgetting to change the context path: If you don’t deploy as
ROOT.war, your app will still be atexample.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.comor use an online DNS checker. - Not setting
proxyName: If using a reverse proxy and you don’t setproxyNameandproxyPort, Tomcat may generate redirect URLs withlocalhost:8080. - Firewall blocking: Even if Tomcat listens on 80, the OS firewall may block it. Open the port with
ufw allow 80(Ubuntu) orfirewall-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
Hostname to your domain to avoid Tomcat’s defaultlocalhostbeing 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.xmlbefore 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 setproxyNameandproxyPorton 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.warand 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
webappsfolder for aROOTdirectory 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 setscheme="https"andsecure="true"on the Connector.
Frequently Asked Questions
- Can I map multiple domains to the same Tomcat app? Yes, you can add multiple
Hostelements inserver.xmlwith different names, or configure multiple server blocks in your reverse proxy to point to the same Tomcat instance. - 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.
- 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. - 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.
- 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
proxyNameandproxyPorton the Connector to the external domain and port. - Can I use a subdomain like
app.example.com? Yes, just create an A record for that subdomain and use it as theHostname 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.