【问题标题】:500 Internal Server Error Flask Api on LighttpdLighttpd 上的 500 内部服务器错误 Flask Api
【发布时间】:2020-07-26 21:56:40
【问题描述】:

几天来我一直在努力在 Lighttpd(在 Rasp Pi 下)下构建 Flask Api,但遇到了“500 Internal Server Error”的问题。

在“/var/www/api/api.py”下,

#!/usr/bin/env python
import requests
from flask import Flask
from flask import request
from flask import jsonify

app = Flask(__name__)

@app.route("/", methods=["GET"])
def home():

    #return "<h3>TestLine</h3>"
    return jsonify("TestLine")

@app.route("/GetDevice", methods=["GET"])
def getdevice():

    Subject = request.args.get("Subject")

    return jsonify("TestLine 123")

app.run(host = "0.0.0.0", port = 80)

在“/var/www/api/api.fcgi”下,

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

if __name__ == "__main__":
    WSGIServer(app, debug = True).run()

在“/etc/lighttpd/lighttpd.conf”下,

debug.log-request-handling = "enable"

server.modules   += ( "mod_fastcgi" )

server.modules   += ( "mod_rewrite" )

server.modules   += ( "mod_alias" )

server.modules   += ( "mod_accesslog" )

server.document-root        = "/var/www"

#server.port = 80

server.modules += ( "mod_cgi" )

mimetype.assign = (
 ".html" => "text/html", 
 ".txt" => "text/plain",
 ".jpg" => "image/jpeg",
 ".png" => "image/png",
 ".js" => "text/javascript",
 ".css" => "text/css"
)

static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", ".inc", ".pl" )

cgi.assign                 = ( ".pl"  => "/usr/bin/perl",
                       ".cgi" => "/usr/bin/perl",
                       ".rb"  => "/usr/bin/ruby",
                       ".erb" => "/usr/bin/eruby",
                       ".py"  => "/usr/bin/python",
                       ".php" => "/usr/bin/php-cgi" )

index-file.names   += ( "index.pl",   "default.pl",
                       "index.rb",   "default.rb",
                       "index.erb",  "default.erb",
                       "index.py",   "default.py",
                       "index.php",  "default.php" )

server.errorlog    = "/var/log/lighttpd/error.log"
accesslog.filename = "/var/log/lighttpd/access.log"

fastcgi.debug = 1
fastcgi.server = ("/api.fcgi" =>
  ((
     "socket" => "/tmp/api-fcgi.sock",
     "bin-path" => "/var/www/api/api.fcgi",
     "check-local" => "disable",
     "max-procs" => 1
  ))
)

alias.url = ( 
   "/api/" => "/var/www/api"
)

url.rewrite-once = (
  "^(/api($|/.*))$" => "$1",
  "^(/.*)$" => "/api.fcgi$1"
)

在终端下运行“/var/www/api/api.fcgi”没有错误。 我希望有人能真诚地帮助我指出错误并感谢感谢。

【问题讨论】:

    标签: flask raspberry-pi fastcgi lighttpd


    【解决方案1】:

    lighttpd 在配置中的 80 端口上运行。当 lighttpd 尝试启动后端时,后端将失败,因为您的 python 代码也在尝试侦听端口 80

    app.run(host = "0.0.0.0", port = 80)
    

    既然你已经配置了 lighttpd 来启动后端,你应该配置它来监听标准输入。 lighttpd 创建您已配置的套接字(“/tmp/api-fcgi.sock”)并使用该套接字在您的应用程序的标准输入上启动后端。

    【讨论】: