Home > Blockchain >  digital ocean deployment error: 502 Bad Gateway
digital ocean deployment error: 502 Bad Gateway

Time:05-14

I had my site up and running, but the admin CSS was not loading, I am not sure what I did that the server did not agree, but now the entire site is down with a 502 Bad Gateway message.

Here are the content of some key files

sudo nano /etc/systemd/system/gunicorn.socket file:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

gunicorn.service file (sudo nano /etc/systemd/system/gunicorn.service)

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

[Service]
User=eson
Group=www-data
WorkingDirectory=/home/eson/example
ExecStart=/home/eson/example/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          example.wsgi:application
[Install]
WantedBy=multi-user.target

Here is what Nginx has sudo nano /etc/nginx/sites-available/example:

server {
    server_name www.example.com example.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/eson/example;
    }

    location /media/ {
        root /home/eson/example;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name www.example.com example.com;
    return 404; # managed by Certbot
}

When I type sudo systemctl status gunicorn.socket, I get:

● gunicorn.socket - gunicorn socket
     Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
     Active: failed (Result: service-start-limit-hit) since Wed 2022-05-11 01:42:19 UTC; 2s ago
   Triggers: ● gunicorn.service
     Listen: /run/gunicorn.sock (Stream)

When I run file /run/gunicorn.sock I get /run/gunicorn.sock: socket

Nginx looks OK:

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

But, when I run sudo tail -30 /var/log/nginx/error.log I get:

2022/05/11 15:26:47 [error] 739#739: *49 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 192.241.219.87, server: www.example.com, request: "GET /owa/auth/x.js HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/owa/auth/x.js", host: "128.199.2.252"
2022/05/11 15:29:47 [error] 739#739: *51 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 192.241.212.218, server: www.example.com, request: "GET /ecp/Current/exporttool/microsoft.exchange.ediscovery.exporttool.application HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/ecp/Current/exporttool/microsoft.exchange.ediscovery.exporttool.application", host: "128.199.2.252"
2022/05/11 15:30:50 [error] 739#739: *53 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 192.241.221.172, server: www.example.com, request: "GET /owa/auth/logon.aspx HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/owa/auth/logon.aspx", host: "128.199.2.252"
2022/05/11 15:49:48 [error] 739#739: *55 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 72.143.229.153, server: www.example.com, request: "GET /admin/login/?next=/admin/ HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/admin/login/?next=/admin/", host: "example.com"
2022/05/11 16:13:25 [crit] 739#739: *57 SSL_do_handshake() failed (SSL: error:14201044:SSL routines:tls_choose_sigalg:internal error) while SSL handshaking, client: 23.92.22.17, server: 0.0.0.0:443
2022/05/11 16:21:15 [crit] 739#739: *61 connect() to unix:/home/django/gunicorn.socket failed (2: No such file or directory) while connecting to upstream, client: 45.9.20.101, server: _, request: "GET /_ignition/execute-solution HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/_ignition/execute-solution", host: "128.199.2.252:80"

and my firewall allows the followin apps:

sudo ufw app list
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH
  Postfix
  Postfix SMTPS
  Postfix Submission

When I start and enable gunicorn, gunicorn socket status is fine:

sudo systemctl status gunicorn.socket
● gunicorn.socket - gunicorn socket
     Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
     Active: active (listening) since Wed 2022-05-11 16:14:05 UTC; 11min ago
   Triggers: ● gunicorn.service
     Listen: /run/gunicorn.sock (Stream)
      Tasks: 0 (limit: 2339)
     Memory: 0B
     CGroup: /system.slice/gunicorn.socket

But, gunicorn status is NOT:

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 Wed 2022-05-11 14:01:54 UTC; 2h 23min ago
TriggeredBy: ● gunicorn.socket
   Main PID: 1775 (code=exited, status=1/FAILURE)

I did try to restart everything:

sudo systemctl daemon-reload 
sudo systemctl restart gunicorn
sudo systemctl enable gunicorn.socket

When I run sudo tail -F /var/log/nginx/error.log:

2022/05/11 15:07:00 [error] 739#739: *43 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 137.226.113.44, server: www.example.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "www.example.com"
2022/05/11 15:07:29 [crit] 739#739: *45 connect() to unix:/home/django/gunicorn.socket failed (2: No such file or directory) while connecting to upstream, client: 92.255.85.190, server: _, request: "GET / HTTP/1.0", upstream: "http://unix:/home/django/gunicorn.socket:/"
2022/05/11 15:23:42 [crit] 739#739: *47 connect() to unix:/home/django/gunicorn.socket failed (2: No such file or directory) while connecting to upstream, client: 87.251.101.78, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "128.199.2.252:80"
2022/05/11 15:26:47 [error] 739#739: *49 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 192.241.219.87, server: www.example.com, request: "GET /owa/auth/x.js HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/owa/auth/x.js", host: "128.199.2.252"
2022/05/11 15:29:47 [error] 739#739: *51 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 192.241.212.218, server: www.example.com, request: "GET /ecp/Current/exporttool/microsoft.exchange.ediscovery.exporttool.application HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/ecp/Current/exporttool/microsoft.exchange.ediscovery.exporttool.application", host: "128.199.2.252"
2022/05/11 15:30:50 [error] 739#739: *53 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 192.241.221.172, server: www.example.com, request: "GET /owa/auth/logon.aspx HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/owa/auth/logon.aspx", host: "128.199.2.252"
2022/05/11 15:49:48 [error] 739#739: *55 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 72.143.229.153, server: www.example.com, request: "GET /admin/login/?next=/admin/ HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/admin/login/?next=/admin/", host: "example.com"
2022/05/11 16:13:25 [crit] 739#739: *57 SSL_do_handshake() failed (SSL: error:14201044:SSL routines:tls_choose_sigalg:internal error) while SSL handshaking, client: 23.92.22.17, server: 0.0.0.0:443
2022/05/11 16:21:15 [crit] 739#739: *61 connect() to unix:/home/django/gunicorn.socket failed (2: No such file or directory) while connecting to upstream, client: 45.9.20.101, server: _, request: "GET /_ignition/execute-solution HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/_ignition/execute-solution", host: "128.199.2.252:80"
2022/05/11 16:44:28 [error] 739#739: *67 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 185.39.144.147, server: www.example.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "www.example.com"

digital ocean doc suggests that

**connect() to unix:/run/gunicorn.sock failed (2: No such file or directory)**

This indicates that Nginx was unable to find the gunicorn.sock file at the given location. You should compare the proxy_pass location defined within /etc/nginx/sites-available/myproject file to the actual location of the gunicorn.sock file generated by the gunicorn.socket systemd unit. If you cannot find a gunicorn.sock file within the /run directory, it generally means that the systemd socket file was unable to create it. Go back to the section on checking for the Gunicorn socket file to step through the troubleshooting steps for Gunicorn.

So I did, and there it says to run file /run/gunicorn.sock and I get /run/gunicorn.sock: socket, identical to the output they have.

Also, when I run: sudo journalctl -u gunicorn.socket

I get this:

-- Logs begin at Tue 2022-02-22 09:51:29 UTC, end at Wed 2022-05-11 01:49:37 UTC. --
May 10 21:53:50 django-s-1vcpu-2gb-sfo3-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-lim>
May 10 23:51:14 django-s-1vcpu-2gb-sfo3-01 systemd[1]: Listening on gunicorn socket.
May 10 23:52:17 django-s-1vcpu-2gb-sfo3-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-lim>
May 11 01:18:28 django-s-1vcpu-2gb-sfo3-01 systemd[1]: Listening on gunicorn socket.
May 11 01:20:47 django-s-1vcpu-2gb-sfo3-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-lim>
May 11 01:35:43 django-s-1vcpu-2gb-sfo3-01 systemd[1]: Listening on gunicorn socket.
May 11 01:36:19 django-s-1vcpu-2gb-sfo3-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-lim>
May 11 01:40:16 django-s-1vcpu-2gb-sfo3-01 systemd[1]: Listening on gunicorn socket.
May 11 01:40:33 django-s-1vcpu-2gb-sfo3-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-lim>
May 11 01:40:51 django-s-1vcpu-2gb-sfo3-01 systemd[1]: Listening on gunicorn socket.
May 11 01:42:19 django-s-1vcpu-2gb-sfo3-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-lim>
lines 1-12/12 (END)

I did run sudo journalctl -u gunicorn, and I found an error there. Here is the Traceback:

May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]: Traceback (most recent call last):
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     worker.init_process()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     self.load_wsgi()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     self.wsgi = self.app.wsgi()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     self.callable = self.load()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     return self.load_wsgiapp()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     return util.import_app(self.app_uri)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/util.py", line 359, in import_app
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     mod = importlib.import_module(module)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     return _bootstrap._gcd_import(name[level:], package, level)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "<frozen importlib._bootstrap_external>", line 848, in exec_module
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/example/wsgi.py", line 16, in <module>
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     application = get_wsgi_application()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/env/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     django.setup(set_prefix=False)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/env/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     apps.populate(settings.INSTALLED_APPS)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/env/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     app_config = AppConfig.create(entry)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/home/eson/example/env/lib/python3.8/site-packages/django/apps/config.py", line 224, in create
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     import_module(entry)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:     return _bootstrap._gcd_import(name[level:], package, level)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]:   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]: ModuleNotFoundError: No module named 'corsheaders'
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4393]: [2022-05-11 16:50:35  0000] [4393] [INFO] Worker exiting (pid: 4393)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]: Traceback (most recent call last):
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/arbiter.py", line 209, in run
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     self.sleep()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/arbiter.py", line 357, in sleep
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     ready = select.select([self.PIPE[0]], [], [], 1.0)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/arbiter.py", line 242, in handle_chld
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     self.reap_workers()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     raise HaltServer(reason, self.WORKER_BOOT_ERROR)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]: During handling of the above exception, another exception occurred:
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]: Traceback (most recent call last):
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/bin/gunicorn", line 8, in <module>
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     sys.exit(run())
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/app/base.py", line 231, in run
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     super().run()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/app/base.py", line 72, in run
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     Arbiter(self).run()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/arbiter.py", line 229, in run
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     self.halt(reason=inst.reason, exit_status=inst.exit_status)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/arbiter.py", line 342, in halt
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     self.stop()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/arbiter.py", line 393, in stop
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     time.sleep(0.1)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/arbiter.py", line 242, in handle_chld
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     self.reap_workers()
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:   File "/home/eson/example/env/lib/python3.8/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]:     raise HaltServer(reason, self.WORKER_BOOT_ERROR)
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 gunicorn[4375]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 systemd[1]: gunicorn.service: Main process exited, code=exited, status=1/FAILURE
May 11 16:50:35 django-s-1vcpu-2gb-sfo3-01 systemd[1]: gunicorn.service: Failed with result 'exit-code'

I checked my settings.py file for the corsheader, and here it is:

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # 3rd party apps:
    'corsheaders',
    ...
    ]

MIDDLEWARE = [
    # 3rd party middleware
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    ...
    ]

CORS_ALLOWED_ORIGINS = [
        "https://example.com",
        "http://example.com",
        "http://localhost:8000",
        "http://localhost:3000",
        "http://127.0.0.1:8000",
    ]

So, what are my problems? Is it that

"Nginx was unable to find the gunicorn.sock file at the given location"

is it the corsheader stuff? Or may be both? And, how to fix it? I did have the same code before making some changes to fix the css problem in admin page, and it did not complain about corseheader. Thank you for reading through this long log and question. I thought you need to see the full picture to be able to tell what could the cause be. I really appreciate your help as this has kept the site down at a crucial time for us.

EDIT 1: I should also note that when I run sudo nano /run/gunicorn.sock, the file is opened with this message:

[ Error reading /run/gunicorn.sock: No such device or address ]

EDIT 2:

This Stackover flow post helped me debug things a bit further. Running the command

gunicorn --log-file=- <app_name>.wsgi:application

I found out that a couple of packages (python-dotenv) package was not installed on the droplet while it was installed on my local machine. After installing the package, I still get errors as the .env file is not set on the droplet. So, basically my problem now is if it is OK to track .env file, if I should create a separate .env file on the droplet and put all my environment variables there, or if there is another way to set environment variables on digitalocean droplets (note droplets are different from their app platform)

CodePudding user response:

So, my problems, which could be someone else's problems too, were:

  1. I did not have all my packages installed on my virtual environment (hence the infamous 502 bad gateway)
  2. My environment variables were not set (So, I got 500 Server Error). I just made a .env file on the server and copied all my environment variables there (it is not recommended to track .env file)
  3. My migrations were not migrated to my production DB. So, I have to python manage.py migrate

I was able to debug the problem by setting the DEBUG = True in settings.py and see where I get the errors and what those programming errors really mean (e.g. if you get an AttributeError, that means you have not migrated some migration files). Oh, make sure you turn the DEBUG = False afterward to avoid exposing the site. I hope this saves someone 3 days of digging and debugging work ;)

  • Related