⚠️ 注意:不保证在Python3环境下一定实验成功
1. 安装gunicorn:pip install gunicorn
gunicorn 可以做为wsgi容器,用来部署 python。
Flask的启动程序:run.py 就等同于一个库文件了。
run.py:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'hello world'
if __name__ == '__main__':
app.run()
gunicron -w4 -b0.0.0.0:8889 run:app
此时,我们需要用 8889 的端口进行访问,默认的5000并没有启用。其中 gunicorn 的部署中,-w 表示开启多少个 worker,-b 表示 gunicorn 开发的访问地址。
这个时候,你去访问就能用了。
那我们还可以搞点其他的,比如加上supervisor,这是用来管理进程的。
2.安装supervisior
pip install supervisor
echo_supervisord_conf > supervisor.conf
nano supervisor.conf
修改的时候,在最后填上:
[program:run]
command=/usr/local/bin/gunicorn -w4 -b0.0.0.0:8889 run:app
directory=/home/popma/xiuyixiu
startsecs=0
stopwaitsecs=0
autostart=false
stdout_logfile=/home/popma/xiuyixiu/log/gunicorn.log
stderr_logfile=/home/popma/xiuyixiu/log/gunicorn.err
然后启动:supervisord -c supervisor.conf
就可以了。
supervisorctl -c supervisor.conf start [all]|[appname] 启动指定/所有 supervisor管理的程序进程
supervisorctl -c supervisor.conf stop [all]|[appname] 关闭指定/所有 supervisor管理的程序进程
还有不明白的,请查看我参考的这篇文章:http://www.jianshu.com/p/be9dd421fb8d,感谢原作者的付出。