FastCGI、Lighttpd 和 Flask

19

我正在我的树莓派上设置一个简单的Web服务器,但我似乎无法正确地设置lighttpd、fastcgi和flask。

到现在为止,我已经尝试了几次编辑/etc/lighttpd/lighttpd.conf文件,最近一次的更改如下:

fastcgi.server = ("/test" =>
    "test" => (
        "socket" => "/tmp/test-fcgi.sock",
        "bin-path" => "/var/www/py/test.fcgi",
        "check-local" => "disable"
    )
)

这导致在/etc/init.d/lighttpd start上出现了一个错误。第一行看起来不对,所以我在fat arrow后面加了一组括号:

那个命令在/etc/init.d/lighttpd start时报错。第一行看着有问题,所以我在那个Fat Arrow后面添加了一对括号:

fastcgi.server = ("/test" => (
...
))

这并没有报错,但是当我尝试连接时,在Chrome中出现了ERR_CONNECTION_REFUSED。然后我尝试移除"/test" =>,但问题依然存在。我还尝试了这个问题中展示的配置,结果仍然相同。

/var/www/py/test.fgci中:

#!/usr/bin/python
from flup.server.fcgi import WSGIServer
from test import app

WSGIServer(app, bindAddress="/tmp/test-fcgi.sock").run()

/var/www/py/test.py 中:

from flask import Flask
app = Flask(__name__)

@app.route("/test")
def hello():
    return "<h1 style='color:red'>&#9773; hello, comrade &#9773;</h1>"

当我使用/etc/init.d/lighttpd start启动时,当前的lighttpd.conf失败了。

2个回答

0

关于 Python 部分,我无法提供帮助,因为它超出了我的技能范围。然而,当将 PHP 作为 fcgi 服务器运行时,我会使用以下类似的 lighttpd.conf 文件。

fastcgi.server += ( ".php" =>
    ((
        "host" => "127.0.0.1",
        "port" => "9000",
        "broken-scriptfilename" => "enable"
    ))
)

所以我认为以下内容是你需要的Python代码。

fastcgi.server += ( "/test" =>
    ((
        "socket" => "/tmp/test-fcgi.sock",
        "bin-path" => "/var/www/py/test.fcgi",
        "check-local" => "disable"
    ))
)

这是在假设一个独立的进程正在端口9000上处理这些请求。这与nginx中的proxy-pass相同的方法。php-fpm已经有该服务运行,但对于Python,您需要安装一个专用服务器,如gunicornuwsgi,并将其配置为在指定端口上运行您的应用程序。 - Prahlad Yeri

0
考虑到您收到了错误消息ERR_CONNECTION_REFUSED,看起来问题可能与错误的身份验证或受限权限有关,而不是语法问题。
可能性:
1. 检查lighttpd、fastcgi的运行端口状态。(检查IPv6)
    netstat --listen

或者lighttpd日志。
2. 你有没有意识到你写的是'/test' => 'test',只需确保格式如下。
fastcgi.server = ("/hello.fcgi" =>
    ((
        "socket" => "/tmp/hello-fcgi.sock",
        "bin-path" => "/var/www/demoapp/hello.fcgi",
        "check-local" => "disable",
        "max-procs" => 1 
    ))
)

你需要在/var/www/py/test.fcgi中添加这行代码'if name == 'main'',因为这个应用程序是从你的应用程序导入的。
#!/usr/bin/python
from flup.server.fcgi import WSGIServer
from yourapplication import app

if __name__ == '__main__':
    WSGIServer(app, bindAddress="/tmp/test-fcgi.sock").run()


这是参考资料。
请事先确保您应用程序文件中的任何app.run()调用都在if name == 'main':块内或移动到单独的文件中。只需确保不要调用它,因为这将始终启动一个本地WSGI服务器,而我们在部署该应用程序到FastCGI时不需要。

https://flask.palletsprojects.com/en/2.0.x/deploying/fastcgi/

一旦你检查了所有可能性,我们来看看会发生什么。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接