整体处理流程
1. 首先确认uwsgi的配置 虚拟环境 修改为socket接口;项目穿层;2. supervisor部分确认启动命令即可3. Nginx 部分书写转发 添加uwsgi配置以及端口;4. Nginx 修改配置 添加静态文件目录及转发;5. 修改settings配置,同时指定静态文件目录,执行迁移6. 由supervisor控制工具重启Django项目,无需调试其他进程或者端口。7. 备注:访问端口(入口)由Nginx控制,socket仅提供与Nginx与uwsgi的交互
示意图

细节处理
1. 先从后端搞起,uwsgi + django1. 本地打包上传至服务器,xftp,并且输出requirements.txt文件;2. 解压缩,检查python环境配置[自编译];3. 在主项目同级目录下新建虚拟环境 python3 -m venv envb;4. 升级pip3, 安装全部依赖,若有错误使用 python manage.py runserver调试错误;5. 检查MySQL数据库配置,表库数据是否一致,与Django项目连接是否正常;6. 启动虚拟环境,检查环境变量,书写UWSGI文件,特别注意文件路径:主项目第一层,第二层以及uwsgi文件所属,指定虚拟环境位置;7. 设置supervisor, 指定工作进程数量 核数 * 2 + 1 , 指定uwsgi启动以及虚拟环境启动的命令;8. 注意每个阶段的横向调试,三个部件,端口占用检查;2. 注意事项1. 重要的包 pymysql mysqlclient;2. UWSGI & supervisor是关联启动项目,因此杀死进程时,需要先杀死spuervisor 然后杀死UWSGI;3. 补充 kill 是基于 pid 杀死进程,如kill 3389; pkill 是基于进程名字;4. 三个路径很重要 虚拟环境的启动路径,uwsgi.ini文件的位置,manage.py文件所在的位置;分别用于ev, start, test;/tmp/Blog/Blog2/uwsgi.ini /tmp/Blog/envb/bin/ source activate /tmp/Blog/Blog2/manage.py/etc/supervisord.conf5. Blog项目占用 20001端口;6. uwsgi启动命令 /tmp/Blog/envb/bin/uwsgi --ini /tmp/Blog/Blog2/uwsgi.ini;该路径由supervisor工具进行托管;5. uwgi文件中 使用 socket =0.0.0.0:8000端口与Nginx联动
测试启动
supervisord -c /etc/supervisord.confUnlinking stale socket /tmp/supervisor.socksupervisorctl -c /etc/supervisord.confBlog RUNNING pid 11465, uptime 0:00:24supervisor> statusBlog RUNNING pid 11465, uptime 0:00:32supervisor> \q*** Unknown syntax: \qsupervisor> exit()
新增 Nginx 的配置
原有配置备份
server {listen 10001;server_name localhost;charset utf-8;location / {proxy_pass http://s25real_server;}}
修改后的配置
server {listen 10001;server_name localhost;charset utf-8;location / {uwsgi_pass 0.0.0.0:20001;include uwsgi_params;}}
重启 Nginx 配置并且重启项目
nginx -tnginx -s reloadsupervisorctl -c /etc/supervisord.conf>restart Blog
阶段性效果

特别说明
UWSGI与Nginx的交互集中通过sokcet0.0.0.0:20001完成,之后由Nginx指定10001端口代理UWSGI的服务;
未使用Nginx 静态资源代理前的访问速度

激活项目虚拟环境并且收集静态文件到指定目录
cd /tmp/Blog/envb/bin/source activatecd /tmp/Blog/Blog2/whereaboutsvim settings.py# 修改如下用于部署的静态文件(绝对路径),可以通过python manage.py collectstatic收集simpleui依赖的配置项STATIC_ROOT = '/bstatic/'执行命令python manage.py collectstatic
拷贝提示以及检查文件是否拷贝成功
Found another file with the destination path 'summernote/lang/summernote-uz-UZ.min.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.Found another file with the destination path 'summernote/lang/summernote-en-US.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.Found another file with the destination path 'summernote/lang/summernote-pt-PT.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.2220 static files copied to '/bstatic'.
在Nginx中配置静态资源
server {listen 10001;server_name localhost;charset utf-8;location / {uwsgi_pass 0.0.0.0:20001;include uwsgi_params;}location /static {alias /bstatic;}
补充设置对媒体文件的穿透
location /media {autoindex on;alias /tmp/Blog/Blog2/media/;}
老师笔记
nginx+uWSGI+django+virtualenv+supervisor发布web服务器 - py鱼 - 博客园 (cnblogs.com)
