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

systemctl启动gunicorn3失败,提示缺少ExecStart=和ExecStop=配置

解决gunicorn3 systemd启动报错:Service lacks both ExecStart= and ExecStop= setting

这个错误的根源很明确:你的systemd服务配置文件里没有正确设置ExecStart=(启动gunicorn3的命令)和ExecStop=(停止进程的命令),systemd找不到执行启动/停止的逻辑,所以直接拒绝运行服务。下面是具体的修复步骤:

1. 找到你的gunicorn服务配置文件

首先确认配置文件的位置,通常是这两个路径之一:

  • /etc/systemd/system/gunicorn.service(自定义服务文件优先)
  • /lib/systemd/system/gunicorn.service(系统默认配置)

如果之前没创建过,直接在/etc/systemd/system/下新建这个文件即可。

2. 编写正确的服务配置

把下面的模板复制进去,根据你的实际情况修改占位符:

[Unit]
Description=gunicorn3 Daemon
After=network.target  # 确保网络启动后再启动gunicorn

[Service]
User=your_username  # 替换成你的用户名,比如www-data或者你自己的登录用户
Group=your_usergroup  # 对应用户组,一般和用户名相同
WorkingDirectory=/path/to/your/project  # 你的项目根目录,比如/var/www/my_django_project
# ExecStart是核心:gunicorn3的路径 + 启动参数 + WSGI应用入口
ExecStart=/usr/bin/gunicorn3 --workers 3 --bind unix:/path/to/your/project/myproject.sock myproject.wsgi:application
# 简单的停止命令,也可以用pid文件实现更优雅的停止,这里先给通用方案
ExecStop=/usr/bin/pkill gunicorn3

[Install]
WantedBy=multi-user.target  # 设置为多用户环境下开机自启

关键参数说明:

  • 用which gunicorn3命令可以确认gunicorn3的实际路径,替换掉/usr/bin/gunicorn3
  • --workers根据你的服务器CPU核心数调整,一般是核心数*2+1
  • --bind可以是unix套接字(性能更好)或者TCP端口(比如0.0.0.0:8000)
  • myproject.wsgi:application是你的WSGI应用入口,Django项目一般是项目名.wsgi:application,Flask项目需要调整为对应的入口

3. 重新加载并测试服务

配置好文件后,执行以下命令:

  1. 重新加载systemd配置:
    sudo systemctl daemon-reload
    
  2. 启动gunicorn服务:
    sudo systemctl start gunicorn
    
  3. 查看服务状态,确认是否正常运行:
    sudo systemctl status gunicorn
    

如果还是有问题,查看详细日志排查:

journalctl -u gunicorn -f

4. 常见坑点检查

  • 确保WorkingDirectory和ExecStart里的所有路径都真实存在,没有拼写错误
  • 配置的User必须对项目目录有读写权限,否则gunicorn无法读取代码或创建套接字
  • 不要遗漏=号:比如写成ExecStart /usr/bin/...而不是ExecStart=/usr/bin/...,这是新手常犯的错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:14:17