安装: pip install gunicorn 启动应用: gunicorn -w 3 -k gthread -e DJANGO_SETTINGS_MODULE=settings.prod Server.wsgi -b 0.0.0.0:8005 debug方式: gunicorn -w 3 -k gthread -e DJANGO_SETTINGS_MODULE=settings.prod Server.wsgi -b 0.0.0
安装:
pip install gunicorn
启动应用:
gunicorn -w 3 -k gthread -e DJANGO_SETTINGS_MODULE=settings.prod Server.wsgi -b 0.0.0.0:8005debug方式:
gunicorn -w 3 -k gthread -e DJANGO_SETTINGS_MODULE=settings.prod Server.wsgi -b 0.0.0.0:8005 --preload
参数说明:
-w : 启动线程数量
-k: 指定线程类型
-e: 指定环境变量
-b: 绑定的地址和端口
注意点:
如果你的配置文件目录发生了变化, 需要修改wsgi.py文件然后启动的时候只需要执行wsgi
然后使用supervisor进行进程管理:
[program:server]directory=/data/www/server/
command=/data/www/server/env/bin/gunicorn -w 3 -k gthread -e DJANGO_SETTINGS_MODULE=settings.prod Server.wsgi -b :8005
process_name=server-%(process_num)d
stdout_logfile=/var/log/supervisord/server_out.log ; stdout log path, NONE for none; default AUTO
stdout_logfile_maxbytes=100MB ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=5 ; # of stdout logfile backups (0 means none, default 10)
stderr_logfile=/var/log/supervisord/server_err.log ; stderr log path, NONE for none; default AUTO
stderr_logfile_maxbytes=100MB ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile_backups=5 ; # of stderr logfile backups (0 means none, default 10)
nginx配置:
server {listen 80;
server_name xxx.xxx.com;
access_log /data/logs/nginx/access_xxx.com_80.log access2 buffer=10k flush=5s;
error_log /data/logs/nginx/error_xxx.com_80.log warn;
location / {
proxy_pass http://127.0.0.1:8005;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /astatic {
alias /data/www/server/astatic;
}
}
常见问题:
1.最近发现使用gunicorn方式部署的服务,间歇性出现服务不可用的问题, 怀疑是进程被堵塞住了
解决:
之前使用gthread , 现在打算使用gevent的方式来运行,异步模式1.安装gevent库
pip install gevent
2.启动的时候指定方式-k gevent