django Nginx部署
sudo apt-get install nginx #安装
/etc/init.d/nginx start #启动 /etc/init.d/nginx stop #关闭 /etc/init.d/nginx restart #重启
2、安装uwsgi
python3 -m pip install uwsgi
3、django项跟目录创建uwsgi.ini
# uwsgi.ini [uwsgi] #django 端口号 socket = :8000 #django 目录 chdir = /home/user/www/django项目 #Django 项目下 wsgi.py 文件路径 module = myweb.wsgi master = true #进程数 processes = 4 vacuum = true
# django项目根目录
# 执行启动项目
uwsgi --ini uwsgi.ini
4、nginx 配置 新建网站 sudo vi /etc/nginx/conf.d/django项目名.conf
# 创建新的网站
sudo vi /etc/nginx/conf.d/任意文件名.conf
# 写入 server { listen 8099; # nginx 端口号 server_name wwww.baidu.com #绑定域名 charset UTF-8; access_log /var/log/nginx/myweb_access.log; error_log /var/log/nginx/myweb_error.log; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; # django运行端口 uwsgi_read_timeout 2; } location /static { # 静态文件配置 alias /home/user/wwww/django项目/static/; } location /media { # 静态文件配置 alias /home/user/wwww/django项目/media/; } }
5、django settings.py 设置
# 上传文件配置 MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# 静态文件目录配置 STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# ursl配置 url(r'^media/(?P<path>.*)', serve, {'document_root': MEDIA_ROOT}), url(r'^static/(?P<path>.*)$', serve, {'document_root': STATIC_ROOT}), from django.conf import settings if settings.DEBUG: from django.conf.urls.static import static urlpatterns += static( settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)