需求:在lighttpd服务器上跑python的flask框架
前言:这个需求折磨了我三天,安装环境上,由于对这种搭配的文档极少,所以遇见了非常多的坑。接下来就将我的搭建过程,以及遇见的问题给大家做一个参考。
lighttpd安装
- 安装bzip2
tar xvf bzip2-1.0.6.tar.gz
cd bzip2-1.0.6
make -f Makefile-libbz2_so
- //-f 标志是使bzip2 根据另一个Makefile来编译,就是Makefile-libbz2_so文件,创建一个动态的libbz.so库文件,然后把bzip2工具连接到这个库上
make && make install
- 安装lighttpd
tar zxvf lighttpd-1.4.39.tar.gz
cd lighttpd-1.4.39
./configure --prefix=/usr/local/lighttpd --with-openssl
make
make install
lighttpd配置
-
复制/usr/lib/lighttpd-1.4.50/doc/config文件夹到/usr/local/lighttpd/config目录
vi /usr/local/lighttpd/config/lighttpd.conf
-
将104和105行注释掉
#server.username = "lighttpd"
#server.groupname = "lighttpd"
-
将以下配置修改:(在第16行)
#var.log_root = "/var/log/lighttpd"
var.log_root ="/usr/local/lighttpd/log"
#var.server_root = "/srv/www"
var.server_root = "/usr/local/lighttpd"
#var.state_dir = "/var/run"
var.state_dir = "/usr/local/lighttpd/run"
var.home_dir = "/var/lib/lighttpd"
#var.conf_dir = "/etc/lighttpd"
var.conf_dir = "/usr/local/lighttpd/config"
-
之后在/us/local/lighttpd 下边创建log文件夹和run文件夹
-
将配置文件中的server.use-ipv6 设为 "disable"
-
重启Lighttpd
/usr/local/lighttpd/sbin/lighttpd -f /usr/local/lighttpd/config/lighttpd.conf
-
在/usr/local/lighttpd 下边创建htdocs文件夹
-
在其中编写index.html.内容随意如'hello ,world'
- 测试: image.png
到这一步证明lighttpt配置好了,接下来可以配置lighttpd+flask了
首先创建一个项目目录我的就叫foobar了之后创建.fcgi文件,名称可以是myapp.fcgi
具体内容:
from flup.server.fcgi import WSGIServer
from yourapplication import app
if __name__ == '__main__':
WSGIServer(app).run()
- 与python通信的fcgi文件就已创建完成了
- 确保这个文件有执行权限,这样服务器才能执行它:
# chmod +x /所在目录/myapp.fcgi
接下来配置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里面找到问题
#fastcgi.debug = 1
最后附上我测试的flask的代码
#!/usr/local/python3/bin/python3
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "123"
小问题:当你访问falsk的时候需要加上.fcgi
解决方式:翻了一遍lighttpd的官方文档,终于找到解决办法了。只要加上一句
url.rewrite-once=( "^/static/(.*)$" => "/static/$1", "^/(.*)$"=>"/test.fcgi/$1", )
网友评论