如何在错误的Apache服务器容器上修复500内部服务器

时间:2020-06-12 23:43:46

标签: python apache docker ubuntu

我在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')

Apache日志

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

谢谢。

1 个答案:

答案 0 :(得分: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结束标题块。