You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GitHub Actions自动部署Django项目遇服务重启认证问题求助

解决GitHub Actions部署Django时重启服务的交互式认证报错

报错原因:普通用户执行service/systemctl重启gunicorn、nginx需要root权限,而GitHub Actions发起的SSH会话是非交互式的,无法输入sudo密码,导致认证失败。

解决方案一:配置sudo免密并修改重启命令

  1. 在服务器上配置免密sudo权限
    使用visudo编辑sudoers文件(该命令会自动检查语法,避免直接编辑出错):

    sudo visudo
    

    在文件末尾添加以下内容(替换deploy_user为你的实际部署用户名):

    deploy_user ALL=(ALL) NOPASSWD: /usr/sbin/service gunicorn restart, /usr/sbin/service nginx reload
    

    注:如果不确定service命令路径,可执行which service查看,通常为/usr/sbin/service。

  2. 修改deploy.sh中的重启命令
    给service命令添加sudo,同时推荐用reload代替restart处理nginx(更平滑,无需中断服务):

    # Restart gunicorn and reload nginx
    sudo service gunicorn restart
    sudo service nginx reload
    

解决方案二:改用systemctl命令(推荐,适配systemd发行版)

大部分现代Linux发行版采用systemd管理服务,systemctl是更标准的服务管理命令:

  1. 配置sudo免密权限
    同样用visudo添加规则:

    deploy_user ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart gunicorn, /usr/bin/systemctl reload nginx
    

    可通过which systemctl确认命令路径,通常为/usr/bin/systemctl。

  2. 更新deploy.sh的服务操作命令

    # Restart gunicorn and reload nginx
    sudo systemctl restart gunicorn
    sudo systemctl reload nginx
    

额外提醒

  • 确保deploy.sh中的python命令指向项目的虚拟环境(比如./venv/bin/python),避免使用系统默认python导致依赖缺失。
  • 执行git pull前,可添加git stash或确保工作目录干净,避免代码冲突。

内容的提问来源于stack exchange,提问作者Milano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 10:25:20