当前位置: 首页> 健康> 养生 > Django项目部署:uwsgi+daphne+nginx+vue部署

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

时间:2025/8/23 13:49:00来源:https://blog.csdn.net/weixin_46371752/article/details/140008379 浏览次数:0次

一、项目情况

项目根目录:/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_daphnestart_daphne
}# 根据命令行参数执行相应的函数
case "$1" instart)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:9801location /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

关键字:Django项目部署:uwsgi+daphne+nginx+vue部署

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: