title: How to deploy multiple web apps on different ports in Tomcat 9?
body: <p>I am trying to deploy a spring app on different port on Tomcat 9. What I want to achieve is to:<br>
  1. Leave default Tomcat config for other apps to deploy them in <code>webapps</code> folder on port 8080<br>
  2. Deploy only one spring app on port 8081 on root path  </p>
  
  <p>So for example:<br>
  <code>localhost:8080/app1</code><br>
  <code>localhost:8080/app2</code><br>
  [...]<br>
  <code>localhost:8081/</code>  </p>
  
  <p>I have created new folder for my second service: <code>/var/lib/tomcat9/webapps_8081/</code><br>
  For the first service I left it in default location: <code>/var/lib/tomcat9/webapps/</code></p>
  
  <p>Here is my <code>server.xml</code> file:</p>
  
  <pre><code>  &lt;Service name="Catalina"&gt;
  
      &lt;Connector port="8080" protocol="HTTP/1.1"
                 connectionTimeout="20000"
                 redirectPort="8443" /&gt;
  
      &lt;Engine name="Catalina" defaultHost="localhost"&gt;
        &lt;Realm className="org.apache.catalina.realm.LockOutRealm"&gt;
          &lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                 resourceName="UserDatabase"/&gt;
        &lt;/Realm&gt;
  
        &lt;Host name="localhost"  appBase="webapps"
              unpackWARs="true" autoDeploy="true"&gt;
          &lt;Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                 prefix="localhost_access_log" suffix=".txt"
                 pattern="%h %l %u %t &amp;quot;%r&amp;quot; %s %b" /&gt;
        &lt;/Host&gt;
      &lt;/Engine&gt;
    &lt;/Service&gt;
  
    &lt;Service name="Catalina_8081"&gt;
      &lt;Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8444" /&gt;
      &lt;Engine name="Catalina_8081" defaultHost="localhost"&gt;
  
        &lt;Realm className="org.apache.catalina.realm.LockOutRealm"&gt;
          &lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/&gt;
        &lt;/Realm&gt;
  
        &lt;Host name="localhost"  appBase="webapps_8081" unpackWARs="true" autoDeploy="true"&gt;
          &lt;Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                 prefix="localhost_access_log." suffix=".txt"
                 pattern="%h %l %u %t &amp;quot;%r&amp;quot; %s %b" /&gt;
        &lt;/Host&gt;
      &lt;/Engine&gt;
    &lt;/Service&gt;
  </code></pre>
  
  <p><strong>What I have tried so far and what were the results</strong>  </p>
  
  <p><strong>Method 1: place war file in deployment folder</strong></p>
  
  <ol>
  <li><p>When I put <code>myapp.war</code> file into <code>webapps</code> folder, it is being deployed without any issues on <code>localhost:8080/myapp</code>  </p></li>
  <li><p>When I put myapp.war file into <code>webapps_8081</code> folder, I get following errors:  </p></li>
  </ol>
  
  <pre><code>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]
  </code></pre>
  
  <p>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.</p>
  
  <ol start="3">
  <li>When I rename the file to <code>ROOT.war</code> and place it into <code>webapps</code> folder, it is being deployed without any issues:  </li>
  </ol>
  
  <pre><code>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
  </code></pre>
  
  <p>But when opening <code>localhost:8080/</code> it's giving me 404 error</p>
  
  <ol start="4">
  <li>When I rename the file to <code>ROOT.war</code> and place it into <code>webapps_8081</code> folder, I get the same errors as in point no.2.</li>
  </ol>
  
  <p>It looks like some problem with permissions to the folder which I created manually, but the rights are the same for both: <code>webapps</code> and <code>webapps_8081</code> (I have changed a group and owner to <code>tomcat</code> user for <code>webapps_8081</code>):</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <p><strong>Method 2: use external content file</strong></p>
  
  <p>As second method of deployment, I have tried to use Context file. I was following <a href="https://stackoverflow.com/a/41284328/8864483">Christopher's answer in this topic</a></p>
  
  <p>I placed my war file outside of webapps folders: <code>/var/lib/tomcat9/myapp.war</code>  </p>
  
  <p><code>myapp.xml</code> file:<br>
  <code>&lt;Context docBase="/var/lib/tomcat9/myapp.war"&gt;&lt;/Context&gt;</code></p>
  
  <ol>
  <li><p>I place <code>myapp.xml</code> in <code>/var/lib/tomcat9/conf/Catalina/localhost/osp.xml</code> - app is deployed successfuly on <code>localhost:8080/myapp</code></p></li>
  <li><p>I place <code>myapp.xml</code> in <code>/var/lib/tomcat9/conf/Catalina_8081/localhost/osp.xml</code>  - I get following errors:</p></li>
  </ol>
  
  <pre><code>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]
  </code></pre>
  
  <ol start="3">
  <li>I rename <code>myapp.xml</code> to <code>ROOT.xml</code> and place it in <code>/var/lib/tomcat9/conf/Catalina/localhost/ROOT.xml</code> - as per logs, app is deplyed successfully</li>
  </ol>
  
  <pre><code>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
  </code></pre>
  
  <p>But when opening <code>localhost:8080/</code> it's giving me 404 error</p>
  
  <ol start="4">
  <li>I rename <code>myapp.xml</code> to <code>ROOT.xml</code> and place it in <code>/var/lib/tomcat9/conf/Catalina_8081/localhost/ROOT.xml</code> - I get following errors: </li>
  </ol>
  
  <pre><code>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]
  </code></pre>
  
  <p>So as you can see I have 2 main issues here.</p>
  
  <p><strong>Question 1</strong>
  Why Tomcat cannot create new directories within <code>webapps_8081</code> ?<br>
  <strong>Question 2</strong>
  Why do I get 404 error when an app is deployed as ROOT.war while it works well on regular mapping?</p>