Running Tomcat9 with dedicated tomcat user: Startup fails with JAVA_HOME or 500 in localhost:8080

huangapple go评论126阅读模式
英文:

Running Tomcat9 with dedicated tomcat user: Startup fails with JAVA_HOME or 500 in localhost:8080

问题

我刚刚在我的计算机上安装了Apache Tomcat 9,主要按照这些说明进行操作,但我在用户管理方面遇到了困难。

我的环境:Ubuntu 20.04,Java 11(在链接组java中只有一个可选项(提供/usr/bin/java):/usr/lib/jvm/java-11-oracle/bin/java)。

少数异常情况:我既没有JAVA_HOME,也没有将Java主目录添加到PATH环境变量中。

我还创建了一个符号链接,指向/opt/tomcat/latest/,用于指向所有Tomcat文件的正确文件夹。

现在我想要让Tomcat作为用户'tomcat'运行。

这是我的tomcat.service文件的内容:

[Unit]
Description=Tomcat 9 servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/java-11-oracle/bin/java"
# Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"

Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/tomcat.pid"
# Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

现在,当我注释掉JAVA_HOME环境变量的那一行时,Tomcat服务的启动失败(通过journalctl -xe查看):

Okt 27 17:43:52 my-user startup.sh[7714]: Tomcat started.
Okt 27 17:43:52 my-user shutdown.sh[7737]: PID file found but either no matching process was found or the current user does not have permission to stop the process. Stop aborted.
Okt 27 17:43:52 my-user sudo[7690]: pam_unix(sudo:session): session closed for user root

因此有一些以root身份运行的东西,我不太明白。

但是,当我注释掉这一行时,Tomcat可以正常运行,但在localhost:8080上我收到了这个500错误:

Message org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.index_jsp

或者这个404错误:

Message JSP file [/index.jsp] not found

这与这里这里类似。

我还尝试过sudo chown -R tomcat /opt/tomcat/latest/work/Catalina/localhost,但没有帮助。

如果能提供帮助,我将不胜感激。

英文:

I have just installed Apache Tomcat9 on my machine, following mostly these instructions, but I am really stuck with user management here.

My enviroment: Ubuntu 20.04, Java 11 (There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-11-oracle/bin/java).

Few exceptions: I neither have JAVA_HOME, nor the java home directory in my PATH environment variable.

I also have created a symlink at /opt/tomcat/latest/ for pointing to the right folder for all tomcat files.

Now I want to let tomcat run as a service as user 'tomcat'.

This is how my tomcat.service file looks like:

[Unit]
Description=Tomcat 9 servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/java-11-oracle/bin/java"
# Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"

Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/tomcat.pid"
# Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

Now, when I comment out the line for the JAVA_HOME environment variable, the startup of the tomcat service fails (journalctl -xe):

Okt 27 17:43:52 my-user startup.sh[7714]: Tomcat started.
Okt 27 17:43:52 my-user shutdown.sh[7737]: PID file found but either no matching process was found or the current user does not have permission to stop the process. Stop aborted.
Okt 27 17:43:52 my-user sudo[7690]: pam_unix(sudo:session): session closed for user root

So there is something running as root, what I don't get.

But when I comment out this line, tomcat runs happily, but on localhost:8080 I am getting this 500:

Message org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.index_jsp

or this 404

Message JSP file [/index.jsp] not found

It is similar like here and here.

I also did sudo chown -R tomcat /opt/tomcat/latest/work/Catalina/localhost, but this did not help.

Any help is appreciated.

答案1

得分: 1

现在它可以运行了,我将在这里列出我的更改,但我不确定哪个是关键的,哪个是不必要的。

  1. tomcat.service 中将 JAVA_HOME 更改为:Environment="JAVA_HOME=/usr/lib/jvm/java-11-oracle"
  2. tomcat.service 中将 CATALINA_PID 更改为:Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
  3. 给其他用户对 work、temp、logs、webapps 文件夹的写访问权限。来源见这里这里
  4. 使用这个指南:https://www.interserver.net/tips/kb/install-apache-tomcat-on-ubuntu-18-04/
英文:

Now it works and I'm going to list my changes here, but I'm not sure, which one was crucial and which was unnecessary.

  1. JAVA_HOME in tomcat.service changed to: Environment="JAVA_HOME=/usr/lib/jvm/java-11-oracle"
  2. CATALINA_PID in tomcat.service changed to: Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
  3. give write access to other users to folders work, temp, logs, webapps. Sources here and here.
  4. using this guide: https://www.interserver.net/tips/kb/install-apache-tomcat-on-ubuntu-18-04/

huangapple
  • 本文由 发表于 2020年10月28日 01:10:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64559524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定