Issue
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 "%r" %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 "%r" %s %b" />
</Host>
</Engine>
</Service>
What I have tried so far and what were the results
Method 1: place war file in deployment folder
When I put
myapp.war
file intowebapps
folder, it is being deployed without any issues onlocalhost:8080/myapp
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.
- When I rename the file to
ROOT.war
and place it intowebapps
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
- When I rename the file to
ROOT.war
and place it intowebapps_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>
I place
myapp.xml
in/var/lib/tomcat9/conf/Catalina/localhost/osp.xml
- app is deployed successfuly onlocalhost:8080/myapp
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]
- I rename
myapp.xml
toROOT.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
- I rename
myapp.xml
toROOT.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?
Solution
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
Answered By - Kamil