Django 部署

CentOS 7 + Nginx + uWSGI 下部署 Django 项目

the web client <-> the web server <-> the socket <-> uwsgi <-> Django

给网站添加 https 证书

参考:
Nginx on CentOS/RHEL 7
Renewing Let’s Encrypt certificates using a systemd timer
Using systemd Timers to Renew Let’s Encrypt Certificates

Nginx 代理静态资源的访问

配置 nginx 请求 url 规则,将静态资源的请求直接转为由 nginx 访问服务器文件系统对应的资源。这样就不需要经过 uwsgi 和 django ,提高性能。

收集静态资源到指定 STATIC_ROOT 目录。

python manage.py collectstatic

nginx 配置

server {
    location /static/ {
        alias /path/to/staticfiles/;
    }
    location /media/ {
        alias /path/to/mediafiles/;
    }
}

uwsgitop

uwsgi 类似 linux 的 top 系统资源占用分析工具,用于后台项目的监测。
但是在 pip install uwsgitop 后,命令行 uwsgitop /tmp/stats.socket 报错:
ModuleError: No module named '_curses'。
也就是 uwsgitop 依赖 curses ,这是一个允许用户编写基于文本的用户界面(TUI)的编程库。许多基于文本的游戏都是使用这个库创建的。
python 交互式命令行 import curses 发现确实报这个错,原来是 import _curses 这行出错了,提示说找不到这个包,这个包是个 curses.so 包,正常编译后的 python 都会存在这个 so 库,移到 /usr/local/bin/python3.7/lib-dynload 下确实没有 curses 相关的,
最终发现原来在编译python源码的时候没有成功将系统下的 curses 的 so 包拷贝到该目录下,于是又重新编译了下,发现编译后的 build 文件下还是不存在这个 so 包,原来需要系统安装 ncurses-devel 包才行。
CentOS 7 下运行:

sudo yum install ncurses-devel

然后再次编译 python 源码后就 可以了,我擦。
参考链接:
[Python]CentOS - ImportError: No module named '_curses'
uwsgitop

参考链接:

How To Serve Django Applications with uWSGI and Nginx on CentOS 7