如何在Nginx上自动唤醒virtualenv和gunicorn?

时间:2018-06-29 12:42:16

标签: python ubuntu nginx virtualenv gunicorn

我有一台数字海洋服务器,其项目正常运行,但是每次我reboot服务器,或者我关闭ssh session时,gunicorn service都会停止并且然后我无法连接。

该服务器具有ubuntu 18和NGINX作为Web服务器。

我的gunicorn语句是:

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

我还需要在执行此语句之前启用virtual environtment

是否有一种方法可以每次都启动virtualenvgunicorn而不用担心呢?

谢谢

编辑:

我正在创建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命令之一有关?

编辑目的:显示下一步以解决问题。

1 个答案:

答案 0 :(得分:1)

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

运行