在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时,一切正常

  1. gunicorn --bind 0.0.0.0:8001 vp.wsgi:application

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

  1. sudo nano /etc/systemd/system/gunicorn.service
  2. [Unit]
  3. Description=gunicorn daemon
  4. After=network.target
  5. [Service]
  6. User=tony
  7. Group=www-data
  8. WorkingDirectory=/home/tony/vp/vp/
  9. ExecStart=/home/tony/vp/vpenv/bin/gunicorn --workers 3 --bind unix:/home/tony/vp/vp/vp.sock vp.wsgi:application
  10. [Install]
  11. WantedBy=multi-user.target

还有我的Nginx文件(出于隐私目的,我用my_ip_address替换了我的IP地址)

  1. sudo nano /etc/Nginx/sites-available/vp
  2. server {
  3. listen 80;
  4. server_name my_ip_address;
  5. location = /facivon.ico { access_log off; log_not_found off; }
  6. location /static/ {
  7. root /home/tony/vp;
  8. }
  9. location / {
  10. include proxy_params;
  11. proxy_pass http://unix:/home/tony/vp/vp/vp.sock;
  12. }
  13. }

我收到错误的网关502错误.

即使重新加载了所有内容

  1. (vpenv) ~/vp/vp$sudo systemctl daemon-reload
  2. (vpenv) ~/vp/vp$sudo systemctl start gunicorn
  3. (vpenv) ~/vp/vp$sudo systemctl enable gunicorn
  4. (vpenv) ~/vp/vp$sudo systemctl restart Nginx

因此,我检查了gunicorn的状态:

  1. (vpenv) ~/vp/vp$sudo systemctl status gunicorn

并得到错误

  1. gunicorn.service - gunicorn daemon
  2. Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
  3. Active: Failed (Result: exit-code) since Sun 2017-04-23 13:41:09 UTC; 18s ago
  4. Main PID: 15438 (code=exited,status=3)
  5. Apr 23 13:41:09 vp-first gunicorn[15438]: SECRET_KEY = os.environ["VP_SECRET_KEY"]
  6. Apr 23 13:41:09 vp-first gunicorn[15438]: File "/home/tony/vp/vpenv/lib/python3.5/os.py",line 7
  7. Apr 23 13:41:09 vp-first gunicorn[15438]: raise KeyError(key) from None
  8. Apr 23 13:41:09 vp-first gunicorn[15438]: KeyError: 'VP_SECRET_KEY'
  9. Apr 23 13:41:09 vp-first gunicorn[15438]: [2017-04-23 13:41:09 +0000] [15445] [INFO] Worker exitin
  10. Apr 23 13:41:09 vp-first gunicorn[15438]: [2017-04-23 13:41:09 +0000] [15438] [INFO] Shutting down
  11. Apr 23 13:41:09 vp-first gunicorn[15438]: [2017-04-23 13:41:09 +0000] [15438] [INFO] Reason: Worke
  12. Apr 23 13:41:09 vp-first systemd[1]: gunicorn.service: Main process exited,code=exited,status=3/
  13. Apr 23 13:41:09 vp-first systemd[1]: gunicorn.service: Unit entered Failed state.
  14. Apr 23 13:41:09 vp-first systemd[1]: gunicorn.service: Failed with result 'exit-code'.
  15. ^X

我已经将我的秘密密钥放置在〜./ bashrc中(并做了源〜./ bashrc)和我的virtualenv激活文件中(并做了了源vpenv / bin / activate).

.sock文件无处可寻!

一些注意事项:

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

独角兽:

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

Nginx

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

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

当我做:

  1. $ps -aux | grep gunicorn

我得到:

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

这意味着有错误.

我的Nginx错误日志文件

  1. 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"
  2. 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文件

  1. import os
  2. from django.core.wsgi import get_wsgi_application
  3. os.environ.setdefault("DJANGO_SETTINGS_MODULE","config.settings.production")
  4. application = get_wsgi_application()

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

我不得不说这是我第一次部署,但我会尽力了解所有内容.

希望你能帮忙!!!

最佳答案
我创建的新用户无权访问.bashrc

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

  1. [Service]
  2. Environment=VP_SECRET_KEY=<value>

重新开始一切:

  1. sudo systemctl daemon-reload
  2. sudo systemctl start gunicorn
  3. sudo systemctl enable gunicorn
  4. sudo systemctl restart Nginx

并做了!

猜你在找的Nginx相关文章