在数字海洋上部署Django,Gunicorn,Nginx,Virtualenv给了我502 Bad Gateway& Gunicorn无法读取Secret Key

时间:2017-04-23 14:12:28

标签: django nginx deployment virtualenv gunicorn

我现在已经尝试部署2天了,即使我阅读了很多文章,StackOverflow问题和数字海洋教程,我似乎无法让它工作。

我的主要教程是这个:https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04?comment=47694#create-and-configure-a-new-django-project

当我绑定我的gunicorn文件(参见下面的命令)并转到 my_ip_address 时: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

我的 nginx文件我用my_ip_address替换了我的IP地址以保护隐私

sudo nano /etc/nginx/sites-available/vp

server {
    listen 80;
    server_name my_ip_address;

    location = /facivon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/tony/vp;
    }

    location / {
            include proxy_params;
            proxy_pass http://unix:/home/tony/vp/vp/vp.sock;
        }
}

我收到错误的网关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

所以我检查了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(并做了源〜。/ bashrc)和我的virtualenv激活文件中(并且做了源vpenv / bin / activate)。

.sock文件无处可寻!

有些说明:

之前,我得到了另一个错误,即gunicorn无法启动,我的gunicorn和nginx配置路径看起来像这样:

Gunicorn:

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

这意味着有错误。

我的 nginx错误日志文件:

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/"

这是我的 wsgi.py 文件:

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

是的,我使用多个设置文件。

我不得不说这是我第一次部署,但我尽力了解一切。

希望你能帮忙!!!

1 个答案:

答案 0 :(得分:4)

我创建的新用户无权访问.bashrc

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

[Service]
Environment=VP_SECRET_KEY=<value>

重启一切:

sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl restart nginx

完成了!