Django project deployment: use uwsgi+nginx+vue to deploy projects, django online deployment

Directory Structure:

study_drf

​ --study_drf

​ --static

​ --manage.py

​ --uwsgi.ini #uwsgi configuration file

1. uwsgi configuration

1. uwsgi download

pip install uwsgi

2. uwsgi configuration file: under the project root directory

[uwsgi]
#与nginx搭配时使用
socket=127.0.0.1:8000

#项目根目录
chdir=/home/liuhaizhang/project/study_drf

#启动主进程,管理其他进程
master=true

#项目同名目录下的wsgi.py文件
wsgi-file=study_drf/wsgi.py

#进程数和每个进程的线程数
process=4
threads=2

#进程id存放位置【只写文件名时是在项目根目录下】
pidfile=uwsgi.pid

# 运行的日志【只写文件名时是在项目根目录下】
daemonize=uwsgi.log

# 每个进程最大的请求数
max_requests = 1000

#使用虚拟环境时,需要指定虚拟环境
pythonpath = /home/liuhaizhang/.virtualenvs/study_drf

# 启用线程
enable-threads = true

3. uwsgi command

进入到项目的根目录下[记住要先切换到虚拟环境中]
#启动
uwsgi --ini uwsgi.ini

#停止
uwsgi --stop uwsgi.pid

Two, nginx configuration

1. Configuration file

server {
       listen 80;    #监听端口,浏览器访问的就是这个端口
       server_name localhost;  #你的服务器IP
       client_max_body_size 75M;
       charset     utf-8;
       
       #vue前端
       location / {
        	#打包的
            root /home/liuhaizhang/project/dist;
            index index.html;
       }
       
       #django后端接口
       location /api {
     	    proxy_set_header Host $http_host;
            proxy_set_header  X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            #重写,当访问80/admin时会转发后端,根据>自身后端路由,进行重定向转发
            rewrite ^/api/(.*)$ /$1 break;  
            # 这个是根据nginx部署位置的
            include /etc/nginx/uwsgi_params;
            #这个是uwsgi的启动IP和端口
            uwsgi_pass 127.0.0.1:8000;
       }
       
       #django静态资源
       location /static {
           #配置媒体资源,要求同静态资源一致
           alias /home/liuhaizhang/project/study_drf/static;
        }

  }

        

Guess you like

Origin blog.csdn.net/weixin_46371752/article/details/130478226