【问题标题】:Can't start gunicorn with systemd django无法使用 systemd django 启动 gunicorn
【发布时间】:2016-09-12 15:08:42
【问题描述】:

我是systemd 的新手。刚刚安装lubuntu16.04
我有以下systemd 文件:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=jcg
Group=jcg
WorkingDirectory=/home/jcg/venvs/baseball/baseball_stats
ExecStart=/home/jcg/.virtualenvs/baseball/bin/gunicorn -w 3 -b 0.0.0.0:8001 baseball_stats.wsgi

[Install]
WantedBy=multi-user.target

我收到此错误:

jcg@jcg-Inspiron-1011:/var/log$ systemctl status gunicorn
● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2016-05-16 13:59:18 EDT; 9min ago
  Process: 681 ExecStart=/home/jcg/.virtualenvs/baseball/bin/gunicorn -w 3 -b 0.0.0.0:8001 baseball_stats.wsgi 
 Main PID: 681 (code=exited, status=200/CHDIR)

May 16 13:59:18 jcg-Inspiron-1011 systemd[1]: Started gunicorn daemon.
May 16 13:59:18 jcg-Inspiron-1011 systemd[1]: gunicorn.service: Main process exited, code=exited, status=200/CH
May 16 13:59:18 jcg-Inspiron-1011 systemd[1]: gunicorn.service: Unit entered failed state.
May 16 13:59:18 jcg-Inspiron-1011 systemd[1]: gunicorn.service: Failed with result 'exit-code'.

但是如果我运行这个gunicorn starts:

(baseball) jcg@jcg-Inspiron-1011:~/venvs/baseball/baseball_stats$ /home/jcg/.virtualenvs/baseball/bin/gunicorn -w 3 -b 0.0.0.0:8001 baseball_stats.wsgi

我错过了什么,或者做错了什么?

【问题讨论】:

  • 工作目录在您的脚本和 shell 命令中似乎不一样。我希望这只是一个错字。
  • 好收获。我将 /.venvs 更改为 /venvs 但同样的问题
  • journalctl 没有给你更多的东西吗?

标签: django gunicorn systemd virtualenvwrapper


【解决方案1】:

对于未来的读者,我的问题是由于没有设置我的 django 应用程序所需的环境变量。 (settings.py 读取环境)。从命令行调用 gunicorn 时,该环境变量之前已设置。

使用 systemd 时,gunicorn.service 文件中需要以下内容:

  [Service]
  Environment=SECRET_KEY=secret

【讨论】:

  • 为了更清楚地说明,即使您在~/.bash_profile~/.bashrc 中导出了环境变量,您也需要为服务定义环境。您可以通过将EnviromentFile=home/user/.bash_profile 添加到[Service] 块来使用这些文件。
  • 阅读任何文章后,它工作正常...谢谢发布解决方案。
  • @calder.ty 只是一个更正:它是 EnvironmentFile=/home/user/.bash_profile - 注意 Environment 中的 n 和路径前面的斜杠,以便使绝对的。
  • 可以在wsgi文件中设置环境变量
【解决方案2】:

执行以下操作
首先你必须在系统中创建一个服务文件

$ sudo nano /etc/systemd/system/python_django.service

在服务中编写如下代码

[Unit]
Description = Python django service
After = network.target

[Service]
ExecStart = /etc/python/python_script.sh

[Install]
WantedBy = multi-user.target

然后在/etc/python/中使用以下脚本创建一个文件python_script.sh

#!/bin/bash
/usr/bin/gunicorn --access-logfile - -c /etc/python/config/python_django.py  python.wsgi:application

然后授予该文件的权限

$ chmod +x python_script.sh

/etc/python/config 中创建一个文件python_django.py 然后输入以下代码

command = '/usr/bin/gunicorn'
pythonpath = '/home/user/{python_dir}'
bind = '0.0.0.0:8000'
workers = 5
user = 'user'

然后运行这段代码

$ sudo systemctl enable python_django //it creates a symlink
$ sudo systemctl start python_django 
$ sudo systemctl status python_django 

现在你可以看到正在运行的服务了。

只需检查http://{ip}:8000 || $ curl "http://0.0.0.0:8000"

注意:请确保您的 gunicorn 文件存在于 /usr/bin/gunicorn 检查为$ ls -l /usr/bin/gunicorn

【讨论】: