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

使用systemd启动uWSGI失败:重启后服务无法运行

树莓派Raspbian Bullseye下systemd管理uWSGI服务重启后无法自动启动

问题描述

我在树莓派4(运行Raspbian GNU/Linux 11 (bullseye))上部署了简单Flask应用,希望通过systemd管理uWSGI服务。相关配置如下:

uWSGI配置文件(/home/pi/Python/uwsgi_examples/flask.ini)

http = :5000
processes = 4
threads = 2
wsgi-file = /home/pi/Python/uwsgi_examples/hello_flask.py
callable = app

systemd服务文件(my_service.service)

[Unit]
Description=uwsgi Service
After=network.target
After=network-online.target

[Service]
ExecStart=/home/pi/.local/bin/uwsgi --ini /home/pi/Python/uwsgi_examples/flask.ini
Restart=always
KillSignal=SIGQUIT

[Install]
WantedBy=multi.user.target

手动执行sudo systemctl start my_service.service时服务运行正常,但重启系统后,虽已启用服务,uWSGI却未运行。执行sudo systemctl status my_service显示服务处于inactive (dead)状态。


解决建议

  • 修正WantedBy字段拼写错误
    你的服务文件中WantedBy=multi.user.target存在拼写错误,正确的目标是multi-user.target(使用连字符而非点)。这个错误会导致systemd无法将服务关联到正确的启动运行级别,重启后不会自动触发启动。
    修改后执行以下命令更新配置:

    sudo systemctl daemon-reload
    sudo systemctl reenable my_service.service
    
  • 指定服务运行的用户与组
    当前服务默认以root用户运行,但uWSGI路径位于/home/pi/.local/bin,可能存在权限冲突。在[Service]段添加用户组配置:

    User=pi
    Group=pi
    

    完成后重新加载配置并重启服务。

  • 检查文件与目录权限
    确保uWSGI可执行文件、Flask应用文件及配置文件具备正确的权限:

    # 赋予uWSGI可执行权限
    chmod +x /home/pi/.local/bin/uwsgi
    # 确保应用和配置文件可读
    chmod 644 /home/pi/Python/uwsgi_examples/hello_flask.py /home/pi/Python/uwsgi_examples/flask.ini
    
  • 强化网络依赖配置
    虽然已设置After=network-online.target,但添加Wants=network-online.target可强制等待网络完全就绪后再启动服务,避免因网络未就绪导致启动失败:

    [Unit]
    Description=uwsgi Service
    Wants=network-online.target
    After=network.target network-online.target
    
  • 查看启动日志定位具体问题
    使用journalctl查看服务启动的详细日志,获取失败原因:

    sudo journalctl -u my_service.service -b
    

    -b参数限定显示本次系统启动后的日志,从中可以找到如路径错误、权限不足、依赖缺失等具体问题。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 10:48:22