使用 CGI 脚本运行 Flask 应用程序时出现 500 服务器错误

问题描述 投票:0回答:3

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

这基本上是它的文件树: Image of file tree 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
文件。不过我没有添加。?

python apache flask cgi ionos
3个回答
1
投票

您的

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

主要问题是您的

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

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

理论上,当从

website
目录中运行时,以下内容应该足够了:

chmod a+rx . main.cgi

注意

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

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

chmod uo+rx . main.cgi

至于

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


1
投票

我更改了

main.cgi
文件顶部的 shebang,并且成功了。

之前:

#!/usr/bin/python

之后:

#!/usr/bin/python3.7


0
投票

我试过这个..

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

之前:#!/usr/bin/python 之后:#!/usr/bin/python3.9

#!/usr/bin/python3.9
from wsgiref.handlers import CGIHandler
from app import app

CGIHandler().run(app)

.htaccess 文件

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

应用程序.py ... ...

if __name__ == "__main__":
  app.run()
© www.soinside.com 2019 - 2024. All rights reserved.