django + gunicorn + supervisor

在服务器上跑着一个Django项目,想用supervisor管理起来,遇到一个小问题,记录一下
原本启动Django项目的命令是用的manage.py ,  可是这中方法有个很神奇的坑,就是ctrl + c 终止程序后,端口号还被占用,年少无知的我觉得都是这样,偶尔用gunicorn启动了一次,发现人家就没这毛病,顿时感受好蠢
因此,接下来就是用gunicorn来启动Django项目了, 对于Django项目来讲,有一个自带的wsgi.py文件,咱们用这个文件来启动就行 ,web

在命令行的名令是:(执行命令的路径是在manage.py所在的目录)flask

gunicorn appproject.wsgi:application -b 0.0.0.0:8080  # appproject是项目名


这就是最基本的启动命令, 第一次用可能会对 ` appproject.wsgi:application `  以为奇怪,其实没什么,appproject.wsgi 实际上就是appproject/wsgi , 而后你打开这个wsgi文件,就知道application是怎么回事了
若是你用gunicorn启动的是一个flask项目,那个写法就一目了然了

重点是gunicorn启动项目,就得这么启动,必须有个启动文件
还有坑就是,只能用点的方式去引用,不要用路径的方式,那种会报错 `  ImportError: Import by filename is not supported. `

ubuntu

好了,下面说一下supervisor的用法:
基本用法从网上一搜一大把,我就不复制了,就说一下添加一个进程该写哪些东西,就以上面这个项目为例,好比咱们如今要添加对这个项目的管理
1. 通常supervisor的配置目录都在 /etc/supervisor/ 下
进到这个目录, 打开supervisior.conf文件,注意这个文件中的最下面的include, 这个是supervisor配置的要管理的项目的配置,到这个目录下去添加一个配置
服务器

; 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]
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/*.conf      # 注意这里

其实这个目录通常就是/etc/supervisor/conf.d , 刚才应该也看到了app

2. 好了,如今进入这个目录
若是supervisor已经在管理着其余进程, 这里面应该已经有一些.conf文件了, 能够参照这些文件, 好比添加一个test.conf, 里面的内容:socket

[program:slots_console]    # 项目名
autorestart=True        # 项目挂掉后是否自动重启
redirect_stderr=True      # 自动记错误日志
command=/mnt/slots_spin/lib/slots_admin/venv/bin/gunicorn -b 0.0.0.0:10002  slots_console_backend.wsgi:application    # 这个是重点,启动命令, 格式是 gunicorn [参数] application   顺序不要写反, 并且application不要写一长串路径,不要担忧找不到文件,目录在下面写
user=ubuntu  
autostart=True
directory=/mnt/slots_spin/lib/slots_admin/slots_console/slots_console_backend  # 这个是启动命令的执行目录

 

3. 配置好后, ` sudo supervisorctl `,进入supervisor的管理界面,首先用update命令更新一下配置,而后用status查看一下状态,不出意外已经启动起来了,若是有问题,去看日志,日志文件目录在` supervisior.conf `文件里有写

this

相关文章
相关标签/搜索