lighttpd+fastcgi+flask

2019-11-18  本文已影响0人  _UniCorn

需求:在lighttpd服务器上跑python的flask框架

前言:这个需求折磨了我三天,安装环境上,由于对这种搭配的文档极少,所以遇见了非常多的坑。接下来就将我的搭建过程,以及遇见的问题给大家做一个参考。


lighttpd安装

lighttpd配置

到这一步证明lighttpt配置好了,接下来可以配置lighttpd+flask了


首先创建一个项目目录我的就叫foobar了之后创建.fcgi文件,名称可以是myapp.fcgi

具体内容:

from flup.server.fcgi import WSGIServer
from yourapplication import app

if __name__ == '__main__':
    WSGIServer(app).run()

接下来配置lighttpd tip(我直接写lighttpd的多端口了php+python)

$SERVER["socket"] == "0.0.0.0:5080"{
        ssl.engine = "enable"
        ssl.pemfile = "/usr/local/lighttpd/certs/server.pem"
        ssl.ca-file = "/usr/local/lighttpd/certs/server.pem"
        server.document-root = "/usr/local/lighttpd/htdocs"
        $HTTP["url"] !~ "^/static" {
            fastcgi.server = ("/" => 
                ((
                    "socket" => "/tmp/foobar-fcgi.sock",
                    "bin-path" => "/web/usr/local/lighttpd/htdocs/foobar/test.fcgi",                                
                    "check-local" => "disable",
                    "max-procs" => 1
                ))
        )
}
        alias.url=("/"=>"/")
        # here Solve the problem of URL redundant parameters
如果不加下面的内容,访问就是https:localhost:port/test.fcgi 
        url.rewrite-once=(
             "^/static/(.*)$" => "/static/$1",
             "^/(.*)$"=>"/test.fcgi/$1",     
        )

}
PHP的代码(对于PHP不做过多强调,更改PHP配置文件网上多得是)
$SERVER["socket"] == "0.0.0.0:8080" {
        #server.document-root = "/usr/local/lighttpd/htdocs"
        ssl.engine = "enable"
        ssl.pemfile = "/usr/local/lighttpd/certs/server.pem"
        ssl.ca-file = "/usr/local/lighttpd/certs/server.pem"
        fastcgi.server = (
        ".php" => ((
         "host" => "127.0.0.1",
         "port" => "9000"
            ))),
}   

建议开启fastcgi的调试模式,方便在error_log里面找到问题


最后附上我测试的flask的代码

#!/usr/local/python3/bin/python3
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "123"

小问题:当你访问falsk的时候需要加上.fcgi

解决方式:翻了一遍lighttpd的官方文档,终于找到解决办法了。只要加上一句

接下来就可以根据端口不同去访问flask和PHP了,描述的不是很完美,有没看懂的可以留言哈

上一篇下一篇

猜你喜欢

热点阅读