1

I am trying to deploy a spring app on different port on Tomcat 9. What I want to achieve is to:
1. Leave default Tomcat config for other apps to deploy them in webapps folder on port 8080
2. Deploy only one spring app on port 8081 on root path

So for example:
localhost:8080/app1
localhost:8080/app2
[...]
localhost:8081/

I have created new folder for my second service: /var/lib/tomcat9/webapps_8081/
For the first service I left it in default location: /var/lib/tomcat9/webapps/

Here is my server.xml file:

  <Service name="Catalina">

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="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" />
      </Host>
    </Engine>
  </Service>

  <Service name="Catalina_8081">
    <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8444" />
    <Engine name="Catalina_8081" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps_8081" unpackWARs="true" autoDeploy="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" />
      </Host>
    </Engine>
  </Service>

What I have tried so far and what were the results

Method 1: place war file in deployment folder

  1. When I put myapp.war file into webapps folder, it is being deployed without any issues on localhost:8080/myapp

  2. When I put myapp.war file into webapps_8081 folder, I get following errors:

07-Sep-2019 15:20:29.370 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/var/lib/tomcat9/webapps_8081/myapp.war]
07-Sep-2019 15:20:29.384 SEVERE [main] org.apache.catalina.startup.ContextConfig.beforeStart Exception fixing docBase for context [/myapp]
 java.io.IOException: Unable to create the directory [/var/lib/tomcat9/webapps_8081/myapp]

When I create this folder manually, then logs says that application has been deplyed, but folder is empty and deployment itself taking only 3 miliseconds.

  1. When I rename the file to ROOT.war and place it into webapps folder, it is being deployed without any issues:
07-Sep-2019 15:23:50.845 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/var/lib/tomcat9/webapps/ROOT.war]
07-Sep-2019 15:25:13.486 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TL$
07-Sep-2019 15:25:13.568 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/var/lib/tomcat9/webapps/ROOT.war] has finished in [82,722] ms

But when opening localhost:8080/ it's giving me 404 error

  1. When I rename the file to ROOT.war and place it into webapps_8081 folder, I get the same errors as in point no.2.

It looks like some problem with permissions to the folder which I created manually, but the rights are the same for both: webapps and webapps_8081 (I have changed a group and owner to tomcat user for webapps_8081):

$ sudo ls -l
drwxrwxr-x 3 tomcat tomcat     4096 Sep  7 14:31 webapps
drwxrwxr-x 2 tomcat tomcat     4096 Sep  7 14:04 webapps_8081

Method 2: use external content file

As second method of deployment, I have tried to use Context file. I was following Christopher's answer in this topic

I placed my war file outside of webapps folders: /var/lib/tomcat9/myapp.war

myapp.xml file:
<Context docBase="/var/lib/tomcat9/myapp.war"></Context>

  1. I place myapp.xml in /var/lib/tomcat9/conf/Catalina/localhost/osp.xml - app is deployed successfuly on localhost:8080/myapp

  2. I place myapp.xml in /var/lib/tomcat9/conf/Catalina_8081/localhost/osp.xml - I get following errors:

07-Sep-2019 16:09:33.483 INFO [main] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying deployment descriptor [/etc/tomcat9/Catalina_8081/localhost/myapp.xml]
07-Sep-2019 16:09:33.501 SEVERE [main] org.apache.catalina.startup.ContextConfig.beforeStart Exception fixing docBase for context [/myapp]
 java.io.IOException: Unable to create the directory [/var/lib/tomcat9/webapps_8081/myapp]
  1. I rename myapp.xml to ROOT.xml and place it in /var/lib/tomcat9/conf/Catalina/localhost/ROOT.xml - as per logs, app is deplyed successfully
07-Sep-2019 16:16:36.411 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.16 (Debian)]
07-Sep-2019 16:16:36.438 INFO [main] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying deployment descriptor [/etc/tomcat9/Catalina/localhost/ROOT.xml]
07-Sep-2019 16:18:06.839 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TL$
07-Sep-2019 16:18:06.923 INFO [main] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of deployment descriptor [/etc/tomcat9/Catalina/localhost/ROOT.xml] has finished in [90,485] ms

But when opening localhost:8080/ it's giving me 404 error

  1. I rename myapp.xml to ROOT.xml and place it in /var/lib/tomcat9/conf/Catalina_8081/localhost/ROOT.xml - I get following errors:
07-Sep-2019 16:14:08.417 INFO [main] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying deployment descriptor [/etc/tomcat9/Catalina_8081/localhost/ROOT.xml]
07-Sep-2019 16:14:08.434 SEVERE [main] org.apache.catalina.startup.ContextConfig.beforeStart Exception fixing docBase for context []
 java.io.IOException: Unable to create the directory [/var/lib/tomcat9/webapps_8081/ROOT]

So as you can see I have 2 main issues here.

Question 1 Why Tomcat cannot create new directories within webapps_8081 ?
Question 2 Why do I get 404 error when an app is deployed as ROOT.war while it works well on regular mapping?

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
Kamil
  • 485
  • 1
  • 10
  • 24
  • Possible duplicate of [How to allow Tomcat war app to write in folder](https://stackoverflow.com/questions/56827735/how-to-allow-tomcat-war-app-to-write-in-folder). The directory names look like you're running on Debian or derivative thereof, where you'll run into Debian's default sandboxing of tomcat. – Olaf Kock Sep 08 '19 at 12:07

1 Answers1

0

I was able to resolve the problems.
First I have reinstalled Tomcat completely and removed all the leftovers, by following the steps described by Dung in this topic: how to completely remove tomcat 7 from ubuntu 14.04. That resolved an issue with root mapping. So in the result I don't know what was causing that.

Permissions problem has been resolved by creating override.conf file described in How to allow Tomcat war app to write in folder

Kamil
  • 485
  • 1
  • 10
  • 24