python - 如何在nginx自动唤醒virtualenv和gunicorn?

标签 python ubuntu nginx virtualenv gunicorn

我有一个 digital ocean 服务器和一个正常工作的项目,但每次我重新启动服务器,或者我关闭ssh session 到这个gunicorn服务 已停止,然后我无法连接。

服务器有 ubuntu 18 和 NGINX 作为网络服务器。

我的 gunicorn 语句是:

gunicorn --bind unix:/tmp/gunicorn.sock computationalMarketing.wsgi:application

此外,我还需要在执行此语句之前启用虚拟环境

有没有办法每次都启动virtualenv & gunicorn 并且不用担心?

谢谢

编辑:

我正在创建 systemd 服务文件(下面的附加脚本),但它显示以下错误:

Warning: The unit file, source configuration file or drop-ins of gunicorn.service changed on disk. Run 'systemctl daemon-reload' to reload units.
● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2018-07-03 11:57:19 UTC; 10s ago
  Process: 8446 ExecStart=/var/www/computationalMarketing/bin/activate (code=exited, status=217/USER)
 Main PID: 8446 (code=exited, status=217/USER)

Jul 03 11:57:19 tfm-rius systemd[1]: Starting gunicorn daemon...
Jul 03 11:57:19 tfm-rius systemd[8446]: gunicorn.service: Failed to determine user credentials: No such process
Jul 03 11:57:19 tfm-rius systemd[8446]: gunicorn.service: Failed at step USER spawning /var/www/computationalMarketing/bin/activate: No such process
Jul 03 11:57:19 tfm-rius systemd[1]: gunicorn.service: Main process exited, code=exited, status=217/USER
Jul 03 11:57:19 tfm-rius systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Jul 03 11:57:19 tfm-rius systemd[1]: Failed to start gunicorn daemon.
root@tfm-rius:/var/www/computationalMarketing/computationalMarketing/code/computationalMarketing#

脚本

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

[Service]
Type=oneshot
User=root
Group=www-data
WorkingDirectory=/var/www/computationalMarketing
ExecStart=/var/www/computationalMarketing/bin/activate
ExecStart=/var/www/computationalMarketing/computationalMarketing/code/computationalMarketing/gunicorn --access-logfile --bind unix:/tmp/gunicorn.sock computationalMarketing.wsgi:application

[Install]
WantedBy=multi-user.target

问题

此错误与服务主体中使用的用户有关,或者可能与 execStart 命令之一有关?

编辑目的:展示我解决问题的下一步。

最佳答案

您不需要显式启动 virtualenv。你可以在你的 virtualenv 中安装 gunicorn 并像 /path/to/your/virtualenv/bin/gunicorn --pythonpath=/path/to/your/virtualenv/lib/python2.7/site-packages <other args> 一样运行它, 它将由 virtualenv python 运行

关于python - 如何在nginx自动唤醒virtualenv和gunicorn?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51102039/

相关文章:

ruby-on-rails - 太多重定向 : passenger+nginx SSLRequirement

python - 不间断的 nginx + 2 gunicorns 设置

python - Scrapy DEFAULT_REQUEST_HEADERS 不起作用

python - 用于企业名称的 python 网络抓取器的逻辑

python - 匹配两个具有最大重复次数的唯一字符。如何使用正则表达式提取它?

javascript - weex js 安装失败

python - 检查日期是否属于 pandas 数据框

php - 在 ubuntu 中启用 php-curl 功能

c - 理解这个 shell 脚本分配

nginx - 如何在特定类型的 URL 上禁用 mod_pagespeed