#for conda source activate my_project_env conda install -r requirements.txt #for virtualenv activate my_project_env pip install -r requirements.txt
gunicorn -w 4 -b 0.0.0.0:8080 run:app
server {
listen 80; # nginx监听的端口server_name _;
location / {proxy_pass http://127.0.0.1:8080; # 表名nginx接收到请求去哪里找gunicorn的服务proxy_redirect off;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}
service nginx start/stop/restart/reload/status #check status: ps -aux | grep nginx
(nginx收到客户端发来的请求,根据nginx中配置的路由,将其转发给WSGI)nginx:"WSGI,找你的来了!"(WSGI服务器根据WSGI协议解析请求,配置好环境变量,调用start_response方法呼叫flask框架)WSGI服务器:"flask,快来接客,客户资料我都给你准备好了!"(flask根据env环境变量,请求参数和路径找到对应处理函数,生成html)flask:"!@#$%^......WSGI,html文档弄好了,拿去吧。"(WSGI拿到html,再组装根据env变量组装成一个http响应,发送给nginx)WSGI服务器:"nginx,刚才谁找我来着?回他个话,!@#$%^....."(nginx再将响应发送给客户端)
###生成一个supervisord服务的配置文件
echo_supervisord_conf > supervisord.conf
###将配置文件统一放在/etc下
cp
supervisord.conf
/etc/supervisord
.conf
###修改配置文件
vi
/etc/supervisord
.conf
##加入如下配置信息,
[include]
files =
/etc/supervisord
.d/*.conf
###为了避免将全部新增配置信息全写在一个配置文件里
###每一个配置信息新增一个配置文件,都会被上面那个include添加进去
mkdir
/etc/supervisord
.d/
### 在此目录下增长本app的confsudo vi /etc/supervisord.d/my_flask.conf#加入, 注意其中的路径和用户名:[program:my_flask_app]directory=/home/leslie/flask-project-directorycommand=/your-gunicorn-whole-path-here/gunicorn app:app -b 127.0.0.1:8080 --workers 8 --max-requests 1000user=leslieautostart=true
autorestart=true
redirect_stderr=True
/etc/init.d/supervisord
):#!/bin/sh
#
# /etc/init.d/supervisord
#
# Supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord
# Source init functions
.
/etc/rc
.d
/init
.d
/functions
prog=
"supervisord"
prefix=
"/usr/leslie"
exec_prefix=
"${prefix}"
prog_bin=
"${exec_prefix}/your-path-of-supervisor/bin/supervisord"
# PIDFILE=
"/var/run/$prog.pid" # 博客推荐的这个设置对我来讲不行,我检查本身的supervisord.conf发现pidfile在tmp
PIDFILE="/tmp/$prog.pid"CONF= "/etc/supervisord.conf" # 让启动的时候知道去哪里找配置文件 文章2没有这个start()
{
echo
-n $
"Starting $prog: "
###注意下面这一行必定得有-c /etc/supervisord.conf
daemon $prog_bin -c $CONF
--pidfile $PIDFILE
[ -f $PIDFILE ] && success $
"$prog startup"
|| failure $
"$prog startup"
echo
}
stop()
{
echo
-n $
"Shutting down $prog: "
[ -f $PIDFILE ] && killproc $prog || success $
"$prog shutdown"
echo
}
case
"$1"
in
start)
start
;;
stop)
stop
;;
status)
status $prog
;;
restart)
stop
start
;;
*)
echo
"Usage: $0 {start|stop|restart|status}"
;;
esac
sudo chmod +x /etc/rc.d/init.d/supervisord sudo chkconfig --add supervisord sudo chkconfig supervisord on sudo service supervisord start
ps -ef | grep supervisord ## 同时也能够有service supervisord restart/stop/stats
ps -ef | grep supervisord # 经过这个来得到supervisord的进程pid 而后杀掉以前的进程后再启动。 kill (-s SIGTERM) pid