【问题标题】:500 Server Error With Running Flask App With CGI Script使用 CGI 脚本运行 Flask 应用程序时出现 500 服务器错误
【发布时间】:2021-04-13 16:40:28
【问题描述】:

我的 Python Flask/CGI 程序不断收到500 Internal Server Error

我在共享主机上运行它,所以我遵循了这个教程: https://medium.com/@dorukgezici/how-to-setup-python-flask-app-on-shared-hosting-without-root-access-e40f95ccc819

这是我的主要 python 脚本:(~/website/mainApp.py)

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
        return "123 :)"

if __name__ == "__main__":
        app.run()

这是我的 CGI 脚本 (~/website/main.cgi)

#!/usr/bin/python

import sys
sys.path.insert(0, "~/.local/lib/python3.7/site-packages")

from wsgiref.handlers import CGIHandler
from mainApp import app
CGIHandler().run(app)

这是我的 .htaccess 文件 (~/website/.htaccess):

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /main.cgi/$1 [L]

这基本上是它的文件树:

这是我得到的错误:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

有谁知道哪里可能出错了?

谢谢!

编辑:它现在有一个奇怪的.pyc 文件。但是我没有添加它。?

【问题讨论】:

  • 顺便说一句,您可以放心地忽略 .pyc 文件,这只是 Python 生成的字节码。

标签: python apache flask cgi ionos


【解决方案1】:

我更改了 main.cgi 文件顶部的 shebang,它起作用了。

之前: #!/usr/bin/python

之后: #!/usr/bin/python3.7

【讨论】:

  • 太棒了!很高兴它对你有效。奇怪的是,您在日志中没有看到任何错误消息?相反,您被重定向到默认的 IONOS 页面;他们有奇怪的设置。
【解决方案2】:

.cgi.py 文件的内容看起来不错。

主要问题是您的.cgi 文件的权限不正确。 website 目录可能也是如此——它的权限在您发布的文件视图中不可见。

您需要对 CGI 文件以及指向它的任何目录的执行(和读取!)权限。

理论上,从website 目录中运行以下内容就足够了:

chmod a+rx . main.cgi

注意. 也将命令应用于当前 (website) 目录。这为所有者、组和其他人添加了读取权限和执行权限。

如果您不想为该组申请任何权限,那么这就足够了:

chmod uo+rx . main.cgi

至于.htaccess 文件,它也是有效的并且可以工作——假设您在您的服务器上启用了mod_rewrite。查看this 帖子以获取有关启用该功能的说明,以防您尚未启用。

【讨论】:

  • 我更新了python文件、cgi文件和它们所在目录的权限。我还在.htaccess文件中添加了RewriteBase /。但是,每当我访问该 URL 时,它都会将我带到 IONOS 服务器的默认页面,该页面上有 sedo 停车位。出于某种原因,它没有运行 Flask。我更新了帖子。
  • 我不太熟悉 IONOS 设置。但是你需要确保DocumentRoot 设置正确——指向~/website/
  • DocumentRoot,你的意思是RewriteBase吗? @costaparas
  • 它们是两个不同的东西 - DocumentRoot 在(全局)配置中设置,它应该指向服务器的根目录。 (但我想你不能用 IONOS 改变它)。也许在你的.htaccess 文件中尝试RewriteBase /website
  • 我做到了,但我仍然遇到原来的问题,它把我带到了 IONOS 的 sedo 停车页面。顺便说一句,在 IONOS 中,您可以更改当前指向 /website 的网站“目标”。 @costaparas
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2016-11-23
相关资源
最近更新 更多