Join 1M+ other developers and:
- Get help and share knowledge in Q&A
- Subscribe to topics of interest
- Get courses & tools that help you grow as a developer or small business owner
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.
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.
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 "%r" %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.
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.×