【问题标题】:How do I fix 500 Internal server on error apache server container如何在错误 apache 服务器容器上修复 500 内部服务器
【发布时间】:2020-10-02 20:09:50
【问题描述】:

我在 ubuntu 18.04.1 中有一个 docker apache 容器,它应该运行一个 python 脚本。每次我尝试访问 localhost:8080/script.py 时都会出现此错误

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at 
 webmaster@localhost to inform them of the time this error occurred,
 and the actions you performed just before this error.</p>
<p>More information about this error may be available
in the server error log.</p>
<hr>
<address>Apache/2.4.41 (Ubuntu) Server at localhost Port 8080</address>
</body></html>

为了制作容器的图像,我使用了这个 dockerfile

FROM ubuntu:latest
MAINTAINER Hadoop Engineering
RUN apt-get update
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get install -y apache2
RUN mkdir -p /var/lock/apache2
RUN mkdir -p /var/run/apache2
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_RUN_DIR /var/run/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_LOG_DIR /var/log/apache2
ENV LANG C
CMD ["/usr/sbin/apache2","-D","FOREGROUND"]
EXPOSE 80

我已经使用以下目录指令配置了 /etc/apache2/sites-enabled/000-default.conf。

<Directory /var/www/html>
    Options ExecCGI
    AddHandler cgi-script .py
    DirectoryIndex index.py
</Directory>

我也运行了这个命令来启用 cgi

a2dismod mpm_event
a2enmod mpm_prefork cgi

python脚本真的很简单

#!/usr/bin/python3
import cgitb
cgitb.enable()

print('Content-Type:text/html;charset=utf-8')
print('Hello')

阿帕奇日志

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Sat Jun 13 17:57:35.156583 2020] [mpm_event:notice] [pid 1:tid 139891709893696] AH00489: Apache/2.4.41 (Ubuntu) configured -- resuming normal operations
[Sat Jun 13 17:57:35.156694 2020] [core:notice] [pid 1:tid 139891709893696] AH00094: Command line: '/usr/sbin/apache2 -D FOREGROUND'
[Sat Jun 13 18:02:36.277642 2020] [mpm_event:notice] [pid 1:tid 139891709893696] AH00491: caught SIGTERM, shutting down
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Sat Jun 13 18:02:44.289177 2020] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.41 (Ubuntu) configured -- resuming normal operations
[Sat Jun 13 18:02:44.289305 2020] [core:notice] [pid 1] AH00094: Command line: '/usr/sbin/apache2 -D FOREGROUND'
[Sat Jun 13 18:02:53.781433 2020] [cgi:error] [pid 8] [client 172.17.0.1:33026] malformed header from script 'index.py': Bad header: Hello
[Sat Jun 13 18:03:01.686712 2020] [cgi:error] [pid 10] [client 172.17.0.1:33030] malformed header from script 'index.py': Bad header: Hello
[Sat Jun 13 18:03:52.670527 2020] [cgi:error] [pid 7] [client 172.17.0.1:33034] malformed header from script 'index.py': Bad header: Hello
[Sat Jun 13 18:03:56.058867 2020] [cgi:error] [pid 9] [client 172.17.0.1:33038] malformed header from script 'index.py': Bad header: Hello
[Sat Jun 13 18:03:57.608438 2020] [cgi:error] [pid 6] [client 172.17.0.1:33042] malformed header from script 'index.py': Bad header: Hello
[Sat Jun 13 18:03:58.565509 2020] [cgi:error] [pid 8] [client 172.17.0.1:33046] malformed header from script 'index.py': Bad header: Hello
[Sat Jun 13 18:04:07.376159 2020] [mpm_prefork:notice] [pid 1] AH00169: caught SIGTERM, shutting down
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Sat Jun 13 18:04:15.473822 2020] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.41 (Ubuntu) configured -- resuming normal operations
[Sat Jun 13 18:04:15.473934 2020] [core:notice] [pid 1] AH00094: Command line: '/usr/sbin/apache2 -D FOREGROUND'
[Sat Jun 13 18:04:18.494927 2020] [cgi:error] [pid 9] [client 172.17.0.1:33050] malformed header from script 'index.py': Bad header: Hello
[Sat Jun 13 18:13:19.862778 2020] [cgi:error] [pid 8] [client 172.17.0.1:33092] malformed header from script 'index.py': Bad header: Hello

提前致谢。

【问题讨论】:

  • 通常 Apache 会创建日志文件,您应该检查日志中的内容。应该有关于错误的详细信息。没有细节,我们无能为力。
  • 不记得是否需要,但chmod +x /var/www/html/your_script.py 可能?
  • 我将日志添加到问题@furas
  • 没有用@Torxed,但非常感谢。

标签: python apache docker ubuntu


【解决方案1】:

主要问题似乎是您没有正确退出标题块。根据 HTTP 协议,标头与正文由空的 \r\n 分隔。

#!/usr/bin/python3
import cgitb
cgitb.enable()

print('Content-Type:text/html;charset=utf-8\r\n')
print('\r\n')
print('Hello')

这不仅应该使以标题结尾的行正确(因为它们也需要以 \r\n 每个标题项结束)。但也可以通过添加空/单个 \r\n 来结束标题块。

【讨论】:

  • 我以前见过那个打印行,但认为没有必要。谢谢,成功了。
猜你喜欢
  • 2016-10-28
  • 2015-09-26
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 2015-11-22
  • 2018-11-14
相关资源
最近更新 更多