Question

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

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. https://www.digitalocean.com/community/tutorials/how-to-connect-to-your-droplet-with-ssh https://www.digitalocean.com/community/tutorials/initial-server-setup-with-centos-7 https://www.digitalocean.com/community/tutorials/additional-recommended-steps-for-new-centos-7-servers https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-centos-7

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. https://www.linode.com/docs/security/firewalls/introduction-to-firewalld-on-centos 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: https://www.digitalocean.com/community/tutorials/an-introduction-to-dns-terminology-components-and-concepts It will make you understand a lot of things you’ll need later. After you bought your domain name follow the next tutorial. https://www.digitalocean.com/community/tutorials/how-to-set-up-a-host-name-with-digitalocean After a while (in my case 5 hours) you’ll be able to access your webapp with http://www.example.com:8080/yourapp

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>
      <Host name="example.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
      <Alias>www.example.com</Alias>
      <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" />
</Host>

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=“example.com” - should be your domain name <Alias>www.example.com</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 example.com or www.example.com So you’ll be able to access your app with http://www.example.com:8080

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 www.example.com 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.

Subscribe
Share

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?

Awesome!!! Great Help, Thanks.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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

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 mydomain.com/app ?

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.

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.

Thank you for sharing your solution!