使用django 自带的runserver好处,便于调试,自动reload热加载。
使用uwsgi好处:支持多并发。
生产上,通常是不能用runserver 这种方式的,推荐使用uwsgi或者gunicorn方式。
下面演示 uwsgi方式。
1、配置static_root目录
在django的项目的settings.py中,加入下面一行
代码语言:javascript复制STATIC_ROOT = 'static/'
2、编写uwsgi配置文件
代码语言:javascript复制个人推荐使用http方式,便于内部接口的http方式调用
# cat xxdb_http.ini
[uwsgi]
http = 0.0.0.0:8118
wsgi-file = mxdb/wsgi.py
master = true
processes = 2
threads = 2
daemonize = /tmp/xxdb_uwsgi.log
stats=/apps/fast/xxdb_prod/xxdb_uwsgi.status
pidfile=/apps/fast/xxdb_prod/xxdb_uwsgi.pid
for = static media
static-map=/static=%(chdir)/%(_)
如果要使用socket方式,配置如下,这种不太方便直接通过http方式调用Django访问:
# cat mxdb_socket.ini
[uwsgi]
socket = 0.0.0.0:8118
wsgi-file = xxdb/wsgi.py
master = true
processes = 2
threads = 2
daemonize = /tmp/xxdb_uwsgi.log
stats=/apps/fast/xxdb_prod/xxdb_uwsgi.status
pidfile=/apps/fast/xxdb_prod/xxdb_uwsgi.pid
for = static media
static-map=/static=%(chdir)/%(_)
3、采集下Django的静态文件
代码语言:javascript复制echo 'yes' | /usr/local/python3.9.7/bin/xxdb_prod/bin/python manage.py collectstatic
会自动把静态文件放到 static 目录下
[root@mxali0071 static]# l
total 8.0K
drwxr-xr-x 6 root root 4.0K 2023-02-20 15:31 admin
drwxr-xr-x 7 root root 4.0K 2023-02-20 15:31 rest_framework
4、使用uwsgi方式启停
代码语言:javascript复制启动
/usr/local/python3.9.7/bin/xxdb_prod/bin/uwsgi --ini xxdb_http.ini --static-map /static=./static
5、nginx配置
代码语言:javascript复制配置文件
server {
listen 80;
server_name mysql-mxdb.demo.com;
rewrite ^(.*)$ https://$server_name$1 redirect;
}
server{
listen 443 ssl;
server_name mysql-mxdb.demo.com;
include /usr/local/nginx/conf/vhosts/white_list;
ssl_certificate /usr/local/nginx/cert/server.pem;
ssl_certificate_key /usr/local/nginx/cert/server.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP: MEDIUM;
ssl_prefer_server_ciphers on;
set $tid $pid-$remote_addr-$request_length-$msec;
location / {
proxy_pass http://127.0.0.1:8118;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
# 重载Nginx
nginx -t
nginx -s reload
6、测试
代码语言:javascript复制1、访问后端接口进行测试
2、通过域名访问,看静态资源的加载是否有异常
7、uwsgi的停止
代码语言:javascript复制停止
/usr/local/python3.9.7/bin/xxdb_prod/bin/uwsgi --stop /apps/fast/xxdb_prod/xxdb_uwsgi.pid