Django项目部署:uwsgi+daphne+nginx+vue部署

一、项目情况

项目根目录:/mnt/www/alert

虚拟环境目录:/mnt/www/venv/alert

激活虚拟环境:source  /mnt/www/venv/alert/bin/activate

二、具体配置

1、uwsgi启动配置

根目录下:新增 uwsgi.ini

注意:使用9801端口与nginx通信

[uwsgi]
#nginx与项目在同一服务器时,使用socket方式,可以提高性能
socket=0.0.0.0:9801
# nginx与项目不在一个服务器时,uwsgi直接作为web服务器使用
#http=127.0.0.1:8010
# 配置工程目录
chdir=/mnt/www/alert
# 配置项目的wsgi目录,相对于项目根目录,(绝对路径/mnt/www/alert/alert/wsgi.py)
wsgi-file=alert/wsgi.py
#虚拟环境目录(下级目录有bin)
virtualenv =/mnt/www/venv/alert
#配置进程,线程信息
listen=1024
processes=2
threads=4
enable-threads=True
master=True
pidfile=uwsgi.pid
daemonize=uwsgi.log
#django项目修改完文件后自动重启
py-autoreload=1

启动命令:

#激活虚拟环境
source /mnt/gs/venv/yd/bin/activate  

#启动项目
uwsgi --ini uwsgi.ini

方式1:看是否有相关进程

ps -aux | grep uwsgi 

方式2:看端口的tcp是否处于监听状态

netstat  -tuln | grep 9801

2、daphne启动配置

项目根目录下 :新增 start_daphne.sh

注意:使用9802端口与nginx通信

#!/bin/bash

# 设置项目根目录
PROJECT_ROOT="/mnt/www/alert"

# 设置 Python 虚拟环境路径(ls 可以看到bin目录)
VENV_PATH="/mnt/www/venv/alert"


# 设置 定时器脚本的 启动命令
DAPHNE_COMMAND="$VENV_PATH/bin/daphne -b 127.0.0.1 -p 9802 gzgs_alert.asgi:application"

# 函数: 启动 Daphne 服务
start_daphne() {
    cd "$PROJECT_ROOT"
    #source "$VENV_PATH/bin/activate"
    echo "Starting Daphne server..."
    nohup $DAPHNE_COMMAND >/dev/null 2>&1 &  
    echo "Daphne server started."
}

# 函数: 停止 Daphne 服务
stop_daphne() {
    echo "Stopping Daphne server..."
    pkill -f "$DAPHNE_COMMAND"
    echo "Daphne server stopped."
}

# 函数: 重启 Daphne 服务
restart_daphne() {
    stop_daphne
    start_daphne
}

# 根据命令行参数执行相应的函数
case "$1" in
    start)
        start_daphne
        ;;
    stop)
        stop_daphne
        ;;
    restart)
        restart_daphne
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

配置可执行:chmod  +x  start_daphne.sh

启动: sh start_daphne.sh start

停止:sh start_daphne.sh stop 

重启:sh start_daphne.sh restart

3、配置nginx

注意:当前系统对外开放的端口是80端口

server {
        listen       80;
        server_name  www.xxx.com;

        #vue打包后的代码
        location / {
            root   /mnt/www/html/dist;
            index  index.html index.htm;
        }

        #后端api接口: uwsgi使用socket=0.0.0.0:9801
        location /api {
                include /etc/nginx/uwsgi_params;
                uwsgi_pass localhost:9801;
        }

        #后端websocket接口
        location /ws {
                #所有的websocket的路由都设置socket开头
                proxy_pass http://localhost:9802/socket;
                #nginx配置支持websocket,下面三条
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

                #websocket三个超时时间,有默认值
                proxy_read_timeout 600s;
                proxy_connect_timeout 60s;
                proxy_send_timeout 60s;

        }
        #静态文件代理
        location ~ ^/(api|)static {
                alias /mnt/www/alert/static;
        }
        #客户资源代理
        location ~ ^/(api|)media {
                alias /mnt/www/alert/media;
        }

}

启动:

sudo systemctl  start  nginx.service

停止:

sudo systemctl  stop  nginx.service

重启:

sudo systemctl  restart  nginx.service

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值