django + gunicorn + supervisor

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

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

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. `

好了,下面说一下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 , 刚才应该也看到了

2. 好了,现在进入这个目录
如果supervisor已经在管理着其他进程, 这里面应该已经有一些.conf文件了, 可以参照这些文件, 比如添加一个test.conf, 里面的内容:

[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 `文件里有写

猜你喜欢

转载自www.cnblogs.com/zhang-can/p/9231131.html
今日推荐