Gunicorn(绿色独角兽)是一个Python WSGI的HTTP服务器 (web服务器)。
项目部署方式: nginx + gunicorn + flask
安装gunicorn: $ pip install gunicorn
安装gunicorn成功后,通过命令行的方式可以查看gunicorn的使用信息:
$ gunicorn -h # 查看命令行选项
$ gunicorn 运行文件名称:Flask程序实例名 # 直接运行,默认启动的127.0.0.1::8000
$ gunicorn -w 4 -b 127.0.0.1:5001 -D --access-logfile ./logs/log.txt 运行文件名称:Flask程序实例名 # 指定进程数和端口号。 -w:(worker)表示进程数。 -b:(bind)表示绑定ip地址和端口号。-D:表示守护进程方式启动(后台运行)
安装Nginx:
$ sudo apt-get install nginx
/usr/local/nginx/conf/nginx.conf(Nginx配置文件,配置Nginx):
# 。。。
# 负载均衡
upstream my_flask {
server 127.0.0.1:5000;
server 127.0.0.1:5001;
}
server {
# 监听80端口
listen 80;
# 本机
server_name localhost;
# 会匹配"/"开头的所有url
location / {
# 请求转发到gunicorn服务器
proxy_pass http://my_flask;
# 设置请求头,并将头信息传递给服务器端
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
# 。。。
启动nginx: $ sudo sbin/nginx
停止nginx: $ sudo sbin/nginx -s stop
重启nginx: $ sudo sbin/nginx -s reload
查看: $ ps aux | grep nginx