Supervisor托管Flask项目,supervisor托管flask


1 环境需求

  • Ubuntu
  • pip
  • virtualenv
  • Flask
  • Gunicorn

2 环境部署

2.1Ubuntu环境部署

pip部署
安装

sudo apt-get install python-pip
sudo apt-get install pyhton3-pip

查看版本

pip -V
pip3 -V

2.2 viurtualenv部署

安装

sudo apt-get install virtualenv

新建指定py版本虚拟环境

virtual -p /usr/bin/python2 py2env
virtual -p /usr/bin/python3 py3env

激活虚拟环境

cd 新建虚拟环境的路径
source py2env/bin/activate
source py3env/bin/activate

停用虚拟环境

deactivate

修改虚拟环境用户

# 进入虚拟环境文件夹
cd py2env
cd py3env
# 修改该文件夹的用户为当前用户
# 这样就可以直接pip安装文件
# 如果用sudo是全局安装,不是在虚拟环境安装
sudo chown xindq -R *
sudo chown xindq -R *

2.3 Flask部署

安装

(py2env)pip install flask
(py2env)pip install flask

框架

from flask import Flask
app = Flask(__name__)

@app.route('/api', methods=[GET,POST])
def api():
	return 'Api test!'
if __name__ == '__main__':
	app.run(host='0.0.0.0', port=8080, debug=True)

运行

(py2env)python app.py
(py3env)python app.py

访问

http://127.0.0.1:8080/api

2.4 gunicorn部署

安装

(py2env)pip install gunicorn
(py3env)pip install gunicorn

配置

gunicorn -w 4 -b [inetIP]:[port] [fileName]:[serverName]
w:gunicorn开启的进程数;
b:绑定服务地址及端口;
inetIP:内网IP;
port:绑定运行端口;
fileName:需要监控的py文件(包含运行的服务);
serverName:服务的名称;

查看内网地址

ifconfig

例子

#输入,即可运行app服务
gunicorn -w 4 -b 127.0.0.1:8080 chatbot:app

源码chatbot.py

from flask imprort Flask
app = Falsk(__name__)
@app.route('/connect', methods=[GET])
	return 'Service is runing...'
if __name__ == '__main__':
	app.run(host='0.0.0.0', port=8080, debug=True)

2.5 supervisor部署

2.5.1 安装

#python2环境
sudo apt-get install supervisor

2.5.2 配置supervisord.conf

  • /etc/supervisor/supervisord.conf
; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)
[supervisord]
nodaemon=true
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run//supervisor.sock ; use a unix:// URL  for a unix socket
; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.
[include]
;单个程序
files = /etc/supervisor/conf.d/chatbot.conf
;多个程序,同时写入conf.d文件夹中
;如有chatbot和face_recognition两个程序
;程序设计如下
;chatbot.conf和face_recognition.conf
files = /etc/supervisor/conf.d/*.conf
;注意,python中的文件后缀,可以更改
;经验:A用py2,B用py3
;使用的conf不兼容
;有后缀隔离自己配置:如*.ini, *.txt
;supervisor控制台
[inet_http_server]
port=*:8080
username=xindaqi
password=123456

2.5.3 配置程序文件chatbot.conf

  • /etc/supervisor/conf.d/chatbot.conf
[program:app]
#执行gunicorn命令
command=/sourcePath/py3envinstall/bin/gunicorn -w 4 -b 127.0.0.1:8080 chatbot:app
directory=/sourcePath
#必须添加用户
user=xindq
#自动启动
autostart=true
#自动重启
autorestart=true
#日志
stdout_logfile=/sourcePath/logs/gunicorn_supervisor.log

2.5.4 supervisor使用

#进入/etc/supervisor目录
#启动服务
sudo supervisord -c supervisord.conf
#查看supervisor状态
sudo supervisordctl -c supervisord.conf status
#重新载入
sudo supervisorctl -c supervisord.conf reload
#开启服务
supervisorctl -c supervisord.conf start 
#关闭服务
supervisorctl -c supervisord.conf stop

2.5.6 supervisor使用

#重新载入
sudo supervisorctl reload
#启动
sudo service supervisor start
#重启
sudo service supervisor restart
#查看supervisor当前状态
sudo supervisorctl
#启动app
start app
#停用app
stop app

3 测试

#启动supervisor
sudo supervisord -c supervisord.conf
#查看状态
sudo supervisorctl
#启动app
start app
#启动Postman测试
# 查看supervisor状态
0.0.0.0:8080
  • supervisor登录

图3.1 supervisor登录
  • supervisor监控界面

图3.2 supervisor监控界面

4 安装supervisor问题总结

  • 问题1
$ sudo supervisorctl reload
$ sudo supervisord -c supervisord.conf
error: <class 'socket.error'>, [Errno 111] Connection refused: file: /usr/lib/python2.7/socket.py line: 228
$ sudo supervisorctl
unix:///var/run/supervisor.sock no such file
$ sudo supervisorctl
unix:///var/run/supervisor.sock refused connection

解决方案

#修改xhchatbot.conf文件
[program:app]#app为服务的名称
user= xindq#服务器用户名称
  • 问题2
app     FATAL   Exited too quickly (process log may have details)
app: ERROR (spawn error)

【原因】
未在虚拟环境下启动app。
【解决方案】
在虚拟环境下启动app。

source py3env/bin/activate
sudo supervisorctl
start app

[参考文献]
[1]https://blog.csdn.net/cecurio/article/details/78058606
[2]https://blog.csdn.net/w1014074794/article/details/51881050

相关内容

    暂无相关文章