python - 使用 Gunicorn 部署 Django 应用程序时出错?

标签 python linux django gunicorn systemd

我尝试使用 ngnix-gunicorn 部署一个 django 应用程序,如下所示: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04 我在没有 virtualenv 的情况下做到了。这是“gunicorn.service”文件。 当我尝试制作 systemd 时,我遇到了错误。

[Unit]
Description=gunicorn daemon
After=network.target
[Service]
Environment=SECRET_KEY='*uj$'
User=root
Group=www-data
WorkingDirectory=/root/myproject
ExecStart=/usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/root/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

这是创建 systemd 时的错误。

● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2017-08-26 14:05:19 UTC; 6s ago
  Process: 24747 ExecStart=/usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/root/test1/test1.sock test1.wsgi:applica
 Main PID: 24747 (code=exited, status=200/CHDIR)

Aug 26 14:05:19 ip-172-31-1-229 systemd[1]: Started gunicorn daemon.
Aug 26 14:05:19 ip-172-31-1-229 systemd[1]: gunicorn.service: Main process exited, code=exited, status=200/CHDIR
Aug 26 14:05:19 ip-172-31-1-229 systemd[1]: gunicorn.service: Unit entered failed state.
Aug 26 14:05:19 ip-172-31-1-229 systemd[1]: gunicorn.service: Failed with result 'exit-code'.

最佳答案

我尝试用 supervisord 运行 gunicorn。它在以下配置下工作得非常好。

[program:analytics]
command = /home/ubuntu/code/analytics/bin/gunicorn example.wsgi:application --name example --workers 4 --user=ubuntu --timeout=3600 --bind 127.0.0.1:8000
user = ubuntu ; User to run as
stdout_logfile = /home/ubuntu/code/example/current/slog/gunicorn_start.log  ; Where to write log messages
redirect_stderr = true     ; Save stderr in the same log
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8,PYTHONPATH=/home/ubuntu/code/current/example/bin/python ; Set UTF-8 as default encoding

您可以尝试将 supervisord 安装为 pip,并使用 supervisord.conf 的 include 参数将上述配置包含到其中。

更多信息:http://supervisord.org/

关于python - 使用 Gunicorn 部署 Django 应用程序时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45896304/

相关文章:

python - 如何使用图像和单独的值作为输入来训练 Keras 模型?混合输入

python - 如何从 python 中的 timedelta64[ns] 对象中提取天数作为整数

python - 获取 "No Module Named Spotipy"

html - 在 Linux 上创建到本地可移动存储设备的 href 链接

c - 如何在 c 中为每个函数调用创建新线程

python - 使用 Django RequestFactory 而不是表单数据的 POST 文档

django - 在 Django 中加密 cookie

python - 如何导入我自己的 python 文件?

c++ - 多线程环境下读取变量总是有效吗?

python - Django session 变量重置