前言
最近项目要上线了,然后有一些环境上的问题需要规整一下,需要使用到uWSGI来启动项目,然后在配置supervisor做到自动拉起uWSGI,这样uWSGI挂了也能自动起来了,当然,如果supervisor也挂了的话,那就只能人工了。
一、使用uWSGI部署项目
首先,先粗略理解一下uWSGI是个啥:
WSGI是一种通信协议。
uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。
uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
以下文中出现uwsgi 指 uWSGI服务器 ,现在知道这是个啥了之后,就开始在正文啦。
在Django中配置uwsgi文件:
项目结构:
uwsgi.ini配置:
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
#socket = :8000
http = :8006
# the base directory (full path) 项目所在目录
chdir = /**/**/yuqian/env1/yuqian
# Django s wsgi file
module = WebApi.wsgi:application
# uid/gid
#uid = www-data
#gid = www-data
# buffer
post-buffering = 8192
post-buffering-bufsize = 65536
# thunder-lock
thunder-lock = true
# reload rss
reload-on-rss = 128
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 16
# ... with appropriate permissions - may be needed
# chmod-socket = 664
#thread numbers startched in each worker process
#threads = 10
#monitor uwsgi status 通过该端口可以监控 uwsgi 的负载情况
stats = 127.0.0.1:9191
# 并发处理进程数
workers = 16
# 并发的socket 连接数。默认为100。优化需要根据系统配置
listen = 2048
# clear environment on exit
vacuum = true
# 后台运行,并输出日志
;daemonize = /var/log/uwsgi.log
问题记录:
listen=2048 会出现以下问题:
Listen queue size is greater than the system max net.core.somaxconn (128)
解决方法:
需要修改 /proc/sys/net/ipv4/tcp_max_syn_backlog 和 /proc/sys/net/core/somaxconn 文件
命令:
echo 8912 > /proc/sys/net/ipv4/tcp_max_syn_backlog
echo 8912 >/proc/sys/net/core/somaxconn
在虚拟环境使用uWSGI启动项目
先在切换到 虚拟环境 中安装uWSGI
命令:
sudo apt-get install libpcre3 libpcre3-dev
pip install uwsgi --no-cache-dir
不执行第一句可能会出现 !!! no internal routing support, rebuild with pcre support !!! ,如果出现了这个问题就 pip uninstall uwsgi ,然后根据上述命令重新安装
安装完成后 启动项目
命令:
uwsgi uwsgi.ini
启动成功 日志信息 :
二、在supervisor中添加uWSGI任务,自动拉起项目
安装supervisor:
先切换到 虚拟环境
安装:
pip install supervisor
生成配置文件:
echo_supervisord_conf > /etc/supervisord.conf
在配置文件 supervisord.conf 中添加如下配置:
[program:uwsgi]
;执行命令的用户
user=root
;需要执行的命令 多条命令格式: /bin/bash -c "命令 && 命令" 连接符啥的自己百度
command=/usr/local/bin/uwsgi --ini /项目路径/www_uwsgi.ini
;是否自动启动
autostart=true
;掉了是否自动启动
autorestart=true
;启动日志输出
stderr_logfile=/项目路径/django.output
stdout_logfile=/项目路径/django.output
stopsignal=INT
图中 command 解释:先切换到虚拟环境,然后再执行uwsgi命令
启动supervisor:
启动:
supervisord -c /etc/supervisor_lijuan.conf
查看supervisor是否启动成功:
ps -ef | grep supervisord
查看uWSGI是否启动成功:
ps -ef | grep uwsgi
uWSGI启动失败,查看启动日志是否报错:
vi django.output
关闭所有uWSGI的进程:
killall -9 uwsgi
查看uWSGI进程是否会被自动拉起,可以就是成功了:
ps -ef | grep uwsgi
ps:如果没有生成output文件,可能是command有问题,修改一下命令格式啥的