nginx部署django
一、首先要确保服务器已经安装了nginx
我的nginx是源码包编译安装的,这里就不详细介绍了
二、配置nginx
复制一个server
修改如下:
server{
listen 80;
server_name django.flyfly520.club;
charset UTF-8;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000; # django运行端口
uwsgi_read_timeout 2;
}
location /static{
# 静态文件配置
alias /usr/local/nginx/html/django_fly/static/;
}
location /upload{
# 静态文件配置
alias /usr/local/nginx/html/django_fly/upload/;
}
}
三、安装uwsgi
python3 -m pip install uwsgi
四、配置uwsgi
[uwsgi]
socket = :8000
chdir = /usr/local/nginx/html/django_fly
wsgi-file = django_fly/wsgi.py
master = true
processes = 4
vacuum = true
五、启动与停止并配置开机自启
uwsgi -d --ini uwsgi.ini
报错:
-bash: uwsgi: command not found
解决:
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
重新执行:uwsgi -d --ini uwsgi.ini
pkill -f -9 uwsgi
vim /etc/rc.local在末尾加入
uwsgi -d /usr/local/nginx/html/django_fly/--ini /usr/local/nginx/html/django_fly/uwsgi.ini
六、配置django settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'), ##修改地方
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload/').replace('\\', '/') # media即为图片上传的根路径
MEDIA_URL = '/upload/'
七、配置url.py
re_path('^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static')