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

Python 如何在nginx自动唤醒virtualenv和gunicorn?,python,ubuntu,nginx,virtualenv,gunicorn,Python,Ubuntu,Nginx,Virtualenv,Gunicorn,我有一个digital ocean服务器,它有一个正常工作的项目,但是每次我重新启动服务器,或者关闭ssh会话,gunicorn服务就会停止,然后我就无法连接 该服务器将ubuntu 18和NGINX作为web服务器 我的gunicorn声明如下: gunicorn --bind unix:/tmp/gunicorn.sock computationalMarketing.wsgi:application 在执行此语句之前,我还需要启用虚拟环境 有没有办法每次都启动virtualenv&gun

我有一个digital ocean服务器,它有一个正常工作的项目,但是每次我重新启动服务器,或者关闭ssh会话,gunicorn服务就会停止,然后我就无法连接

该服务器将ubuntu 18和NGINX作为web服务器

我的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#
剧本

问题:

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


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

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

这可能不是主题,但如果您计划进入Centos7,那么这就是生成nginx+gunicorn+mongo的脚本,没有虚拟环境,使用systemD服务,它将确保每次重新启动时都能启动。您应该使用systemD服务启动gunicorn。DigitalOcean已经开始与gunicorn合作了。感谢@DanielRoseman提供的文档,我正在关注并应用它们。但是我会编辑这个问题,因为我有一些疑问,我认为你的用户不应该是“root”,它应该是nginx用户,通常也是www数据。@DanielRoseman我更改了www数据的用户,现在错误是:gunicorn.service:无法执行命令:权限被拒绝我应该给该用户777个路径权限:/var/www/computationalMarketing/bin/activateDon不建议对最近的系统使用rc.local,OP应该使用systemd。感谢@EugenePrimako,我附上了新数据,以便更好地了解我以后所做的工作。
[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