uwsgi是什么
- 学习站点: https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WSGIquickstart.html#http9090
> uWSGI 是一个Web 服务器,它实现了WSGI 协议(web服务网关接口)、uwsgi(线路协议)、http 等协议。注意uwsgi 是一种通信协议,而uWSGI 是实现uwsgi 协议和WSGI 协议的Web 服务器。uWSGI 具有超快的性能、低内存占用和多app 管理等优点,并且搭配着Nginx 就是一个生产环境了,能够将用户访问请求与应用app 隔离开,实现真正的部署。相比来讲,支持的并发量更高,方便管理多进程,发挥多核的优势,提升性能。
nginx 和uWISG 服务器之间如何配合工作的?
– (1)首先浏览器发起http 请求到nginx 服务器
– (2)Nginx 根据接收到请求包,进行url 分析,判断访问的资源类型,如果是静态资源,直接读取静态资源返回给浏览器
– (3)如果请求的是动态资源就转交给uwsgi服务器,uwsgi 服务器根据自身的uwsgi 和WSGI 协议,找到对应的Django 框架。
– (4)Django 框架下的应用进行逻辑处理后,将返回值发送到uwsgi 服务器,然后uwsgi 服务器再返回给nginx。
– (5)最后nginx将返回值返回给浏览器进行渲染显示给用户。
部署uwsgi+django+nginx+supervisor
- django程序目录 /data/Bind-Web/devops
- nginx部署目录 /home/nginx
uwsig配置
# cat /data/Bind-Web/devops/uwsgi.ini
[uwsgi]
socket = 127.0.0.1:9090
chdir = /data/Bind-Web/devops
module = devops.wsgi
master = true
processes = 10
chmod-socket = 664
vacuum = true
pidfile = /var/run/uwsgi.pid
#日志记录位置, 因为用supervisor管理, 这个注释注释掉
#logto = /data/Bind-Web/devops/uwsgi.log
#后台运行, 日志记录位置, 因为用supervisor管理, 这个注释注释掉
#daemonize = /data/Bind-Web/devops/uwsgi.log
nginx配置
# cat /home/nginx/conf/vhost/sinan.xesv5.com.conf
server {
listen 80;
server_name sinan.xesv5.com 10.20.97.127;
charset utf-8;
location /static {
alias /data/Bind-Web/devops/static;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
}
}
supervisor配置
# cat /etc/supervisor/bind-web.ini
[program:bind-web-django]
command=/bin/python manage.py runserver 0.0.0.0:8000
directory=/data/Bind-Web/devops
autostar=true
autorestart=true
redirect_stderr=true
stdout_logfile =/var/log/supervisor/bind_web_access.log
stderr_logfile=/var/log/supervisor/bind_web_error.log
loglevel=info
startretries=3000
stopwaitsecs=300
startsecs=10
[program:bind-web-uwsgi]
directory =/data/Bind-Web/devops
command= /bin/uwsgi -i /data/Bind-Web/devops/uwsgi.ini
autostart = true
startsecs = 5
autorestart = true
startretries = 3
user = root
redirect_stderr = true
stdout_logfile_maxbytes = 100MB
stdout_logfile_backups = 20
stdout_logfile = /data/Bind-Web/devops/uwsgi-stdout.log
stderr_logfile = /data/Bind-Web/devops/uwsgi-stderr.log
stopasgroup=true
killasgroup=true
解决admin后台样式消失的问题
在setting.py
文件中添加STATIC_ROOT = '/home/nginx/html/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = '/home/nginx/html/static'
搜集静态文件到/home/nginx/html/static
# python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:
/home/nginx/html/static
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Copying '/data/Bind-Web/devops/static/js/jquery.vmap.js'
Copying '/data/Bind-Web/devops/static/js/jquery.flot.spline.min.js'
Copying '/data/Bind-Web/devops/static/js/moment.min.js'
Copying '/data/Bind-Web/devops/static/js/daterangepicker.js'
Copying '/data/Bind-Web/devops/static/js/jquery.flot.orderBars.js'
Copying '/data/Bind-Web/devops/static/js/gauge.min.js'
重启uwsgi, reload nginx. 完成.