How to access a Java web application by a domain name using TomCat 8?

Posted May 17, 2016 107.8k views

It isn’t a question.
I already asked for this and nobody answered, so I figured it out by myself. I wish to leave my steps here in case somebody else might find it useful.

I used a droplet with CentOS 7 and TomCat 8 to deploy my webapp.

First of all I followed some tutorials here on DO.

If you don’t know how to work with the firewallD you could just stop it, but I advice you to get aquatinted with it as you’ll need it later to redirect traffic. You can read about it here.
Personally I’ve configured the firewall in the very end after I was sure I did everything right with TomCat, domain etc. But it’s up to you.
For now just open port 80 and 8080 and enable some essential services like ssh and http.
You can do that by

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=8080/tcp

After that reload the firewall.

sudo firewall-cmd --reload

Also make sure your firewall runs on system startup by typing

sudo systemctl enable firewalld

Now you can deploy your WAR via the TomCat web interface.
The URL of your site will be http://droplet-ip:8080/yourapp
How you need to buy a domain name anywhere you like and map it to your droplet.
If you are new to domains like me read this:
It will make you understand a lot of things you’ll need later.
After you bought your domain name follow the next tutorial.
After a while (in my case 5 hours) you’ll be able to access your webapp with

Now you want to get rid of the /yourapp part and port Nr. 8080.

To do the first thing you need to find TomCat “server.xml” file.
You can run

find / -name server.xml

It will search for “server.xml” in your system. In my case it was located under /opt/tomcat/conf/ directory.
So let us go there.

cd /opt/tomcat/conf/

And open the file under root privileges.

sudo vi server.xml

If you followed all the tutorials here, then you should already know how “vi” editor works. So just paste this inside server.xml file.

      <Host name="" appBase="webapps" unpackWARs="true" autoDeploy="true">
      <Context path="" docBase="yourapp" debug="0" privileged="true" />
      <Valve className="org.apache.catalina.valves.AccessLogValve"
             directory="logs"   prefix="localhost_access_log." suffix=".txt"
             pattern="%h %l %u %t &quot;%r&quot; %s %b" resolveHosts="false" />

This entry should be inside the <Engine> tag. I have put it in the end just before the closing </Engine> tag.
Make sure you pay attention to:
name=“” - should be your domain name
<Alias></Alias> - the same but with www in front
docBase=“yourapp” - should be the name you used in URL to access your webapp after the port Nr.
Now TomCat will know what webapp to give if the request will contain the host name or
So you’ll be able to access your app with

Now to get rid of the port Nr.
By default all http request go to port 80, but you can’t bind TomCat to it without running it with root privileges, which is bad. So don’t do it.
You could run Apache as a proxy server in front of TomCat and redirect all traffic from port 80 to 8080, but it is complex. I really don’t need a second server just to redirect requests. You should consider this option if you need better performance as you can deliver all static content with Apache and redirect to TomCat the rest. Or use multiple TomCat servers with Apache as a “router” for them. There are pros and cons for these methods, but you must find the information yourself, as here we will simply redirect port 80 to 8080 using CentOS firewallD.
Here you will need the firewall I mentioned before.
If you already configured your firewall earlier, to redirect ports you type

sudo firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080 --permanent

and reboot it.
You are done. Now you can just type and get straight where you intended.
NOTE this, that I have only TomCat that want to use port 80. If you have additional software they will conflict. In this case you need to apply some rules to redirect to TomCat only what you need. You can read about rules in the link I posted above.

Have a nice day, and I hope it was useful for you. :)

P.S. You can close 8080 port, as you don’t need it anymore since you redirect from port 80 now.

  • Awesome!!! Great Help, Thanks.

  • Hello..

    When i run commands to enable firewall i get following error:
    sudo: firewall-cmd: command not found

    I ran following Commands:

    sudo firewall-cmd –permanent –add-service=http
    sudo firewall-cmd –permanent –add-service=ssh
    sudo firewall-cmd –permanent –add-port=80/tcp
    sudo firewall-cmd –permanent –add-port=8080/tcp
    sudo firewall-cmd –reload
    sudo systemctl enable firewalld
    sudo firewall-cmd –add-forward-port=port=80:proto=tcp:toport=8080 –permanent

    can you pls help?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
6 answers

Thank you for sharing your solution!

Thanks for the tutorial. It worked. How do I access the Tomcat user interface after I do this modification? ip_address:8080 does not bring it up anymore.

  • Did you closed the 8080 port like I said in the end of the tuttorial? If yes, then it’s no wonder you can’t access the user interface, the port is closed. Try typing just the ip_address. The firewall should redirect to port 8080 by itself. That’s considering you did everything right till now. But you said yourself that it works, so everything should be fine.

    • I did not close the 8080. Just did the redirection. I tried ipaddress:8080, ipaddress:80 and ip_address and still can’t access the tomcat. By “it works” I meant that I can now type the domain in the browser and it takes me to the website without any port numbers. The index page loads. Everything shows as running when I run sudo systemctl status tomcat...., firewalld, mysql. I want to remove the war file from tomcat and put a new war instead, but can’t get to the tomcat interface....

      • Does it bring anything up? A warning, an error or something? I just tried to access my own server and it worked fine just with the IP.
        I can’t think of the reason of your error, too little information. There are a lot of ways things can go wrong. Can you specify?

        • it just says:

          “This ip_address page can’t be found”

          HTTP error 404

          It might not be related to your post.... I guess. I’m new to this and I might have an error somewhere else that collides with your script or something. I’m thinking about rebuilding and starting over :) It has been quite the journey here.... Thank you for your help.

I got it to work. The initial </Host> tag in the above example is an extra tag and is not needed. I haven’t opened the server.xml file in a compiler, but it probably throws an error as there is no matching opening <Host> tag for that one.

  • Damm. You are soooo right. I made a typo error. There are two closing tags when the first should be the opening one. Good thing you noticed. :)

I have a tomcat app in port 8080. But I have too two web sites in apache2. Can I make a similar configuration for accesing to my tomcat app using a url like ?

How do I configure domain/subdomain for a spring-boot application which has an embedded tomcat server?