在Digital ocean上部署Django,Gunicorn,Nginx,Virtualenv给我带来了502 Bad Gateway,而Gunicorn无法读取密钥

前端之家收集整理的这篇文章主要介绍了在Digital ocean上部署Django,Gunicorn,Nginx,Virtualenv给我带来了502 Bad Gateway,而Gunicorn无法读取密钥 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我已经尝试部署了2天,即使我阅读了许多文章,StackOverflow问题和Digital Ocean Tutorials,似乎也无法使它正常工作.

我的主要教程是这个: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时,一切正常

@H_301_9@gunicorn --bind 0.0.0.0:8001 vp.wsgi:application

但是在我创建和编辑我的gunicorn.service文件的部分:

@H_301_9@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地址)

@H_301_9@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错误.

即使重新加载了所有内容

@H_301_9@(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的状态:

@H_301_9@(vpenv) ~/vp/vp$sudo systemctl status gunicorn

并得到错误

@H_301_9@ 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配置路径如下所示:

独角兽:

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

Nginx

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

如您所见,路径不是现在的vp / vp.sock,而是vp / vp / vp.sock.

当我做:

@H_301_9@$ps -aux | grep gunicorn

我得到:

@H_301_9@tony 15624 0.0 0.1 12944 976 pts/3 S+ 13:57 0:00 grep --color=auto gunicorn

这意味着有错误.

我的Nginx错误日志文件

@H_301_9@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,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文件

@H_301_9@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文件中,如下所示:

@H_301_9@[Service] Environment=VP_SECRET_KEY=<value>

重新开始一切:

@H_301_9@sudo systemctl daemon-reload sudo systemctl start gunicorn sudo systemctl enable gunicorn sudo systemctl restart Nginx

并做了!

猜你在找的Nginx相关文章