在Digital ocean上部署Django、Gunicorn、Nginx和Virtualenv会给我带来502坏网关&;古尼康罐头盒';不读取密钥

在Digital ocean上部署Django、Gunicorn、Nginx和Virtualenv会给我带来502坏网关&;古尼康罐头盒';不读取密钥,django,nginx,deployment,virtualenv,gunicorn,Django,Nginx,Deployment,Virtualenv,Gunicorn,我已经尝试部署了2天了,尽管我阅读了很多文章、StackOverflow问题和数字海洋教程,但似乎无法让它正常工作 我的主要教程是: 当我绑定gunicorn文件(见下面的命令)并转到我的ip地址8001时,一切正常 gunicorn --bind 0.0.0.0:8001 vp.wsgi:application 但是在我创建和编辑我的gunicorn.service文件的地方: sudo nano /etc/systemd/system/gunicorn.service [Unit]

我已经尝试部署了2天了,尽管我阅读了很多文章、StackOverflow问题和数字海洋教程,但似乎无法让它正常工作

我的主要教程是:

当我绑定gunicorn文件(见下面的命令)并转到我的ip地址8001时,一切正常

gunicorn --bind 0.0.0.0:8001 vp.wsgi:application 
但是在我创建和编辑我的gunicorn.service文件的地方:

sudo nano /etc/systemd/system/gunicorn.service 

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

[Service]
User=tony
Group=www-data
WorkingDirectory=/home/tony/vp/vp/
ExecStart=/home/tony/vp/vpenv/bin/gunicorn --workers 3 --bind unix:/home/tony/vp/vp/vp.sock vp.wsgi:application

[Install]
WantedBy=multi-user.target
2017/04/23 13:41:19 [crit] 15491#15491: *2 connect() to unix:/home/tony/vp/vp/vp.sock failed (2: No such file or directory) while connecting to upstream, client: Client.IP, server: Server.IP, request: "GET / HTTP/1.1", upstream: "http://unix:/home/tony/vp/vp/vp.sock:/", host: "Server.IP"
2017/04/23 13:41:19 [crit] 15491#15491: *2 connect() to unix:/home/tony/vp/vp/vp.sock failed (2: No such file or directory) while connecting to upstream, client: Client.IP, server: Server.IP, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/tony/vp/vp/vp.sock:/favicon.ico", host: "Server.IP", referrer: "http://Server.IP/"
import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")

application = get_wsgi_application()
和我的nginx文件为了隐私,我用我的ip地址替换了我的ip地址

我收到一个错误的网关502

即使在重新加载所有内容之后:

(vpenv) ~/vp/vp$ sudo systemctl daemon-reload 
(vpenv) ~/vp/vp$ sudo systemctl start gunicorn
(vpenv) ~/vp/vp$ sudo systemctl enable gunicorn
(vpenv) ~/vp/vp$ sudo systemctl restart nginx
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl restart nginx
所以我检查了gunicorn的状态:

(vpenv) ~/vp/vp$ sudo systemctl status gunicorn
并获取错误:

 gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2017-04-23 13:41:09 UTC; 18s ago
 Main PID: 15438 (code=exited, status=3)

Apr 23 13:41:09 vp-first gunicorn[15438]:     SECRET_KEY = os.environ["VP_SECRET_KEY"]
Apr 23 13:41:09 vp-first gunicorn[15438]:   File "/home/tony/vp/vpenv/lib/python3.5/os.py", line 7
Apr 23 13:41:09 vp-first gunicorn[15438]:     raise KeyError(key) from None
Apr 23 13:41:09 vp-first gunicorn[15438]: KeyError: 'VP_SECRET_KEY'
Apr 23 13:41:09 vp-first gunicorn[15438]: [2017-04-23 13:41:09 +0000] [15445] [INFO] Worker exitin
Apr 23 13:41:09 vp-first gunicorn[15438]: [2017-04-23 13:41:09 +0000] [15438] [INFO] Shutting down
Apr 23 13:41:09 vp-first gunicorn[15438]: [2017-04-23 13:41:09 +0000] [15438] [INFO] Reason: Worke
Apr 23 13:41:09 vp-first systemd[1]: gunicorn.service: Main process exited, code=exited, status=3/
Apr 23 13:41:09 vp-first systemd[1]: gunicorn.service: Unit entered failed state.
Apr 23 13:41:09 vp-first systemd[1]: gunicorn.service: Failed with result 'exit-code'.
 ^X
我已经在~./bashrc(和did source~./bashrc)和virtualenv activate文件(和did source vpenv/bin/activate)中放置了我的密钥

找不到.sock文件

一些注意事项:

之前,我遇到了另一个gunicorn无法启动的错误,我的gunicorn和nginx配置路径如下所示:

[Service]
Environment=VP_SECRET_KEY=<value>
古尼康:

WorkingDirectory=/home/tony/vp/
    ExecStart=/home/tony/vp/vpenv/bin/gunicorn --workers 3 --bind unix:/home/tony/vp/vp.sock vp.wsgi:application
Nginx:

location / {
                include proxy_params;
                proxy_pass http://unix:/home/tony/vp/vp.sock;
            }
正如您所看到的,路径是vp/vp.sock,而不是像现在这样的vp/vp/vp.sock

当我这样做时:

$ ps -aux | grep gunicorn
我得到:

tony     15624  0.0  0.1  12944   976 pts/3    S+   13:57   0:00 grep --color=auto gunicorn
这意味着有一个错误

mynginx错误日志文件:

sudo nano /etc/systemd/system/gunicorn.service 

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

[Service]
User=tony
Group=www-data
WorkingDirectory=/home/tony/vp/vp/
ExecStart=/home/tony/vp/vpenv/bin/gunicorn --workers 3 --bind unix:/home/tony/vp/vp/vp.sock vp.wsgi:application

[Install]
WantedBy=multi-user.target
2017/04/23 13:41:19 [crit] 15491#15491: *2 connect() to unix:/home/tony/vp/vp/vp.sock failed (2: No such file or directory) while connecting to upstream, client: Client.IP, server: Server.IP, request: "GET / HTTP/1.1", upstream: "http://unix:/home/tony/vp/vp/vp.sock:/", host: "Server.IP"
2017/04/23 13:41:19 [crit] 15491#15491: *2 connect() to unix:/home/tony/vp/vp/vp.sock failed (2: No such file or directory) while connecting to upstream, client: Client.IP, server: Server.IP, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/tony/vp/vp/vp.sock:/favicon.ico", host: "Server.IP", referrer: "http://Server.IP/"
import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")

application = get_wsgi_application()
这是我的wsgi.py文件:

sudo nano /etc/systemd/system/gunicorn.service 

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

[Service]
User=tony
Group=www-data
WorkingDirectory=/home/tony/vp/vp/
ExecStart=/home/tony/vp/vpenv/bin/gunicorn --workers 3 --bind unix:/home/tony/vp/vp/vp.sock vp.wsgi:application

[Install]
WantedBy=multi-user.target
2017/04/23 13:41:19 [crit] 15491#15491: *2 connect() to unix:/home/tony/vp/vp/vp.sock failed (2: No such file or directory) while connecting to upstream, client: Client.IP, server: Server.IP, request: "GET / HTTP/1.1", upstream: "http://unix:/home/tony/vp/vp/vp.sock:/", host: "Server.IP"
2017/04/23 13:41:19 [crit] 15491#15491: *2 connect() to unix:/home/tony/vp/vp/vp.sock failed (2: No such file or directory) while connecting to upstream, client: Client.IP, server: Server.IP, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/tony/vp/vp/vp.sock:/favicon.ico", host: "Server.IP", referrer: "http://Server.IP/"
import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")

application = get_wsgi_application()
是的,我使用多个设置文件

我不得不说,这是我第一次部署,但我尽我所能理解一切


希望你能帮忙

我创建的新用户没有访问.bashrc的权限

我所做的是将环境变量放在gunicorn.service文件中,如下所示:

[Service]
Environment=VP_SECRET_KEY=<value>
完成了