ubuntu18.04 tomcat9 通过 systemd 启动时无法启动应用程序

ubuntu18.04 tomcat9 通过 systemd 启动时无法启动应用程序

首先:我不是 Tomcat 专家,所以请耐心听我说。

目前,当我重新启动上述服务器时,我遇到了一个问题。自动启动时,tomcat 应用程序不可用(404 未找到)。

一旦我通过 systemd 停止 Tomcat 并通过“/usr/share/tomcat9/bin/catalina.sh”手动启动它,配置的应用程序(来自 server.xml)即可可用且可访问。

 ./catalina.sh start
Using CATALINA_BASE:   /var/lib/tomcat9
Using CATALINA_HOME:   /usr/share/tomcat9
Using CATALINA_TMPDIR: /var/lib/tomcat9/temp
Using JRE_HOME:        /usr/lib/jvm/java-12-oracle
Using CLASSPATH:       /usr/share/tomcat9/bin/bootstrap.jar:/usr/share/tomcat9/bin/tomcat-juli.jar

/etc/tomcat9/server.xml 中的一些摘录

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


      <Host name="host-live"  appBase="webapps/host-live"
            unpackWARs="true" autoDeploy="true">
        <Alias>some.domain.com</Alias>
        <Context path="" docBase="/var/lib/tomcat9/webapps/host-live" />

        <Valve className="org.apache.catalina.valves.RemoteIpValve"  remoteIpHeader="x-forwarded-for"  />

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="some.domain.com_access" suffix=".txt"
               pattern="%t %{x-forwarded-for}i %a %m %U %r %b %s %D %I %{X-AUSERNAME}o %{User-Agent}i" resolveHosts="false" />

      </Host>

我的目标是让它在启动时自动启动并可访问:)

因此我尝试修改 systemd 单元文件。我将其更改为以下内容:

# Current Configuration
Environment="CATALINA_HOME=/usr/share/tomcat9"
Environment="CATALINA_BASE=/var/lib/tomcat9"
Environment="CATALINA_TMPDIR=/tmp"
Environment="JAVA_OPTS=-Djava.awt.headless=true"
EnvironmentFile=-/etc/default/tomcat9

# My changes:

# old values:
#ExecStart=/bin/sh /usr/libexec/tomcat9/tomcat-start.sh

# replaced with
ExecStart=/usr/share/tomcat9/bin/startup.sh
ExecStop=/usr/share/tomcat9/bin//shutdown.sh

编辑:

我修复了 tomcat 日志文件的一些权限问题。但仍然出现未找到应用程序的 404 错误。

似乎 Tomcat 在由 systemd 启动时成功部署了应用程序,但仍然不可用。请求时仍出现 404 错误 http://subdomain.domain.tld/myapp/MyAppServer

有趣的是:该应用程序在克隆服务器上立即可用(本地主机文件已编辑)。我不明白。

Mar 24 13:20:00 server rsyslogd: file '/var/log/tomcat9/catalina.out': open error: Permission denied [v8.32.0 try http://www.rsyslog.com/e/2433 ]
Mar 24 13:20:03 server tomcat9[848]: Match [Server/Service/Engine/Host/Valve] failed to set property [resolveHosts] to [false]
Mar 24 13:20:03 server tomcat9[848]: Match [Server/Service/Engine/Host/Valve] failed to set property [resolveHosts] to [false]
Mar 24 13:20:03 server tomcat9[848]: Server version name:   Apache Tomcat/9.0.16 (Ubuntu)
Mar 24 13:20:03 server tomcat9[848]: Server built:          Sep 11 2019 19:47:51 UTC
Mar 24 13:20:03 server tomcat9[848]: Server version number: 9.0.16.0
Mar 24 13:20:03 server tomcat9[848]: OS Name:               Linux
Mar 24 13:20:03 server tomcat9[848]: OS Version:            4.15.0-1063-aws
Mar 24 13:20:03 server tomcat9[848]: Architecture:          amd64
Mar 24 13:20:03 server tomcat9[848]: Java Home:             /usr/lib/jvm/java-12-oracle
Mar 24 13:20:03 server tomcat9[848]: JVM Version:           12.0.1+12
Mar 24 13:20:03 server tomcat9[848]: JVM Vendor:            Oracle Corporation
Mar 24 13:20:03 server tomcat9[848]: CATALINA_BASE:         /var/lib/tomcat9
Mar 24 13:20:03 server tomcat9[848]: CATALINA_HOME:         /usr/share/tomcat9
Mar 24 13:20:03 server tomcat9[848]: Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
Mar 24 13:20:03 server tomcat9[848]: Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
Mar 24 13:20:03 server tomcat9[848]: Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Djava.util.logging.config.file=/var/lib/tomcat9/conf/logging.properties
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Djava.awt.headless=true
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -XX:+UseG1GC
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Xms1024M
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Xmx6144M
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Dignore.endorsed.dirs=
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Dcatalina.base=/var/lib/tomcat9
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Dcatalina.home=/usr/share/tomcat9
Mar 24 13:20:03 server tomcat9[848]: Command line argument: -Djava.io.tmpdir=/tmp
Mar 24 13:20:03 server tomcat9[848]: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib]
Mar 24 13:20:03 server tomcat9[848]: Initializing ProtocolHandler ["http-nio-8080"]
Mar 24 13:20:03 server tomcat9[848]: Server initialization in [1,490] milliseconds
Mar 24 13:20:03 server tomcat9[848]: Starting service [Catalina]
Mar 24 13:20:03 server tomcat9[848]: Starting Servlet engine: [Apache Tomcat/9.0.16 (Ubuntu)]
Mar 24 13:20:05 server tomcat9[848]: 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 TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Mar 24 13:20:37 server tomcat9[848]: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [31,362] milliseconds.
Mar 24 13:20:37 server tomcat9[848]: Deploying web application directory [/var/lib/tomcat9/webapps/my-app-test/my-app]
Mar 24 13:20:38 server tomcat9[848]: 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 TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Mar 24 13:20:38 server tomcat9[848]: Deployment of web application directory [/var/lib/tomcat9/webapps/my-app-test/my-app] has finished in [1,029] ms
Mar 24 13:20:39 server tomcat9[848]: 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 TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Mar 24 13:20:39 server tomcat9[848]: Deploying web application directory [/var/lib/tomcat9/webapps/my-app-live/my-app]
Mar 24 13:20:40 server tomcat9[848]: 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 TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Mar 24 13:20:40 server tomcat9[848]: Deployment of web application directory [/var/lib/tomcat9/webapps/my-app-live/my-app] has finished in [1,106] ms
Mar 24 13:20:40 server tomcat9[848]: Starting ProtocolHandler ["http-nio-8080"]
Mar 24 13:20:40 server tomcat9[848]: Server startup in [36,550] milliseconds
Mar 24 13:22:04 server tomcat9[848]: Pausing ProtocolHandler ["http-nio-8080"]
Mar 24 13:22:04 server tomcat9[848]: Stopping service [Catalina]
Mar 24 13:22:04 server tomcat9[848]: Stopping ProtocolHandler ["http-nio-8080"]
Mar 24 13:22:04 server tomcat9[848]: Destroying ProtocolHandler ["http-nio-8080"]

此致,

马克

答案1

由于我的应用程序在从镜像的新服务器启动时似乎按预期工作,我按照建议检查了 tomcat 监听的端口 (tcp 8080)。我可以看到 tomcat 在端口 8080 上抛出错误 404,并且首先能够使用测试服务器重现该问题。

然后,我检查了 webapps 目录中已部署应用程序的权限,发现所有者组为 root:root

因此我使用以下命令修复了权限:

chown -R tomcat:tomcat /var/lib/tomcat9/webapps/
find  /var/lib/tomcat9/webapps/ -type d -exec chmod 2775 {} +
find /var/lib/tomcat9/webapps/ -type f -exec chmod 0664 {} +
chmod -R g+w /var/lib/tomcat9/webapps/
chmod -R g+s /var/lib/tomcat9/webapps/
usermod -a -G tomcat MYDEPLOYMENTUSERS

这使得 web 应用程序在由 systemd 启动时能够按预期工作。

非常感谢您的建议。保持健康。

相关内容