简体   繁体   中英

Deploying Django, Gunicorn, Nginx, Virtualenv on Digital ocean gives me 502 Bad Gateway & Gunicorn can't read Secret Key

I've been trying to deploy for 2 days now and It seems like I can't get it to work even though I went through many articles, StackOverflow questions, and Digital Ocean Tutorials.

My main tutorial is this one: 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

when I bind my gunicorn file (see command below) and go to my_ip_address :8001 everything works fine

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

But at the part where I created and edited my gunicorn.service file:

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

And my nginx file ( I replaced my ip address with my_ip_address for privacy )

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;
        }
}

I get a bad gateway 502 error.

Even after reloading everything:

(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

So I checked the status of gunicorn:

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

And get the error:

 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

I have placed my Secret Key in both ~./bashrc (and did source ~./bashrc), and in my virtualenv activate file (and did source vpenv/bin/activate).

The .sock file is nowhere to be found!

Some notes:

Before, I was getting an other error that gunicorn could not boot and my gunicorn and nginx config paths looked like this:

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;
            }

As you can see the paths were vp/vp.sock not vp/vp/vp.sock as they are now.

When I do:

$ ps -aux | grep gunicorn

I get:

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

Which means there is an error.

my nginx error log file:

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

Here is my wsgi.py file:

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

And yes I use multiple settings files.

I have to say that this is my first time deploying but I do my best to understand everything.

Hope you can help!!!

The new user I created did not have permission to access .bashrc

What I did was I placed my environment variables inside my gunicorn.service file like this:

[Service]
Environment=VP_SECRET_KEY=<value>

restarted everything:

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

And done!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM