skip to Main Content

I want to have a web-server running on port 80. Everywhere I read, Apache Tomcat runs on 8080. Who wants to have that in their URL? So my first question is where do I cahnge the port number? Do I change it in a single location?

I am used to apache2 virtual hosting. I would like the same setup on tomcat. A folder belongs to each website. I have figured out how to create virtual hosting with servers.xml and have multiple folders for each site but I can not figure out how to deploy a war file to the root directory of each virtual host.

When the war file is deployed, there is a second.war file and there is a blank dir called second. When I go to see the site I have to go to http://example.com:8080/second to see it. I don’t want to have to type in second in the URL. How can I resolve this? I am not a java programmer and the guy who is compiling the Java is a beginner JSP developer.

Here is my Host in servers.xml:

  <Host name="example.com"  appBase="webapps" unpackWARs="true" autoDeploy="true">
      <Alias>www.example2.com</Alias>

      <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
       prefix="example_com_app_access_log" suffix=".txt"
       pattern="%h %l %u %t %r %s %b" />

     <Context path="" docBase="/opt/tomcat/webapps/example_com"
       debug="0" reloadable="true"/>
  </Host>

2

Answers


  1. Chosen as BEST ANSWER

    Scary Wombat had the answer in the comments. Setup a reverse proxy with Apache and it works great!


  2. tl;dr

    For Tomcat listening on port 8080 to receive requests coming in from web browsers on port 80, use a packet-filter tool on your host operating system to alter the incoming network packets’ port number. This process is known as port-forwarding.

    Port 80 is restricted 🚫

    I want to have a web-server running on port 80. Everywhere I read, Apache Tomcat runs on 8080. Who wants to have that in their URL? So my first question is where do I cahnge the port number?

    In Unix-like operating systems (macOS, BSD, Linux, Solaris, etc.), port numbers below 1024 have restricted access for security purposes. Java is built to be secure by default, and so does not have any way (that I know of) to access those ports. So all Java servers default to some number between 1024 and the limit of about 64,000. Tomcat defaults to 8080, arbitrarily chosen for its cute repetition of 80, the usual port for Web apps.

    Every web browser defaults to that port 80, and hides that fact from the user. So a URL like http://www.StackOverflow.com/ is equivalent to a URL with an explicit port number like http://www.StackOverflow.com:80/.

    As you suggested, we usually do not want to bother our users with an explicit port like 8080 to Tomcat. How to resolve this Catch-22 where the web browser wants to default to port 80 but Java-based servers such as Tomcat cannot listen on port 80? The common solution is known as port-forwarding.

    Port-forwarding ⏩

    Most every Unix-like OS comes with a firewall tool known as a packet-filter. Every packet of data being transported across a network and into your server machine can be inspected, filtered, and even modified by such a tool.

    The modification that we want is for any incoming packet coming in on port 80 to be redirected to port 8080 on which Tomcat is listening. In this approach, we alter the network traffic, without Tomcat knowing, and without doing anything to Tomcat at all. Neither the web browser nor the web server will know anything is going on, both remain blissfully ignorant. The browser sends out requests addressed to port 80, and the web server (Tomcat, Jetty, etc.) receives those now-modified requests coming in on port 8080.

    The packet-filter tools I have seen all work the same way: You specify a list of rules. Each rule specifies a condition that if met should result in an action being taken. Each packet is compared to the rules one-by-one. The list of rules is ordered, each rule having an assigned number. The first rule that matches the packet wins, and its action is taken, while the remaining rules in the list are ignored. So, we need to set up rule that says “if this packet is coming in on port 80, change the port to 8080, and let it continue on its way”.

    Simple idea. Unfortunately, every packet-filter tool has its own syntax for these rules. You will need to search for which packet-filter is current for your OS, as some older tools have fallen away, supplanted by newer tools. And then you will need to learn the syntax of that tool’s rules. With luck you will easily find an example snippet to copy, as port-forwarding port 80 for web servers is extremely common.

    Be aware that most packet-filters maintain their rules only for the current work session. When the machine hosting the operating systems reboot, the packet-filter returns to its default settings, without your added rule. You can either manually add your port-forwarding rule after every reboot or find a way for your particular packet-filter tool to learn the rule so it is applied automatically after reboot.

    Caveat: My discussion above assumes that you are using Apache Tomcat as your web server as well as being the JSP/Servlet engine. You mentioned Apache HTTP Server. If you are using Tomcat behind Apache HTTP Server, so Tomcat is acting only as a JSP/Servlet engine but not a web server, that is a different situation entirely.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search