1

我的 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]

这基本上是它的文件树: 文件树 2.0 的图像

这是我得到的错误:

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文件。不过我没加。?

4

2 回答 2

1

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

前: #!/usr/bin/python

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

于 2021-01-08T13:33:19.780 回答
0

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

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

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

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

chmod a+rx . main.cgi

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

如果您不希望为该组应用任何权限,那么这就足够了:

chmod uo+rx . main.cgi

至于.htaccess文件,它也是有效的并且可以工作——假设你已经mod_rewrite在你的服务器上启用了。如果您还没有启用该功能,请查看此帖子以获取有关启用该功能的说明。

于 2021-01-08T07:07:37.550 回答