简体   繁体   中英

Deploying django app to digital ocean unsuccessful (502 Bad Gateway)

Good day guys, I am in a bit of a mess here with deploying a Django app to the digital ocean using Gunicorn and Ngnix. I am following the steps in a video tutorial I bought on udemy, where we are following a link https://gist.github.com/bradtraversy/cfa565b879ff1458dba08f423cb01d71#disable-root-login . while following the tutorial, everything worked to the point before I set up gunicorn, which means I was able to see the website through the IP address. but, only that the static files were not handled by Nginx and I was running the server using python manage.py runserver 0.0.0.0:8000 when I was at this point https://gist.github.com/bradtraversy/cfa565b879ff1458dba08f423cb01d71#run-server . when I followed the step and configured gunicorn and Nginx, I was getting 502 Bad Gateway when I visited using the IP address.

I was wondering why I was getting this issue, and I noticed that when I checked the status of gunicorn using sudo systemctl status gunicorn.socket I was getting:

   gunicorn.socket - gunicorn socket
   Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
   Active: failed (Result: service-start-limit-hit) since Fri 2020-02-28 03:03:11 UTC; 7s ago
   Listen: /run/gunicorn.sock (Stream)

here is a link to the project I was trying to add to the server if needed https://github.com/opeodedeyi/medxFinal

when i run sudo nginx -t i get

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

just incase this is how my gunicorn service file is set up

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

[Service]
User=djangoadmin
Group=www-data
WorkingDirectory=/home/djangoadmin/pyapps/medxFinal
ExecStart=/home/djangoadmin/pyapps/medxFinal/venv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          medx.wsgi:application

[Install]
WantedBy=multi-user.target

and just the explain how my folder is structured, inside the medxFinal is where the venv and medx folders are, inside that medx folder is where the manage.py is and other folders including another medx folder are found, inside the last medx is where the settings.py, wsgi, and first URL is.

running sudo tail -F /var/log/nginx/error.log i see this

2020/02/28 07:33:51 [error] 15252#15252: *12 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 95.217.130.88, server: 64.225.59.162, request: "POST //vendor/phpunit/src/Util/PHP/eval-stdin.php HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock://vendor/phpunit/src/Util/PHP/eval-stdin.php", host: "64.225.59.162", referrer: "https://www.google.com/"
2020/02/28 07:33:52 [error] 15252#15252: *12 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 95.217.130.88, server: 64.225.59.162, request: "POST //wp-content/plugins/cloudflare/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock://wp-content/plugins/cloudflare/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php", host: "64.225.59.162", referrer: "https://www.google.com/"
2020/02/28 07:33:52 [error] 15252#15252: *12 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 95.217.130.88, server: 64.225.59.162, request: "POST //wp-content/plugins/dzs-videogallery/class_parts/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock://wp-content/plugins/dzs-videogallery/class_parts/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php", host: "64.225.59.162", referrer: "https://www.google.com/"
2020/02/28 07:33:52 [error] 15252#15252: *12 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 95.217.130.88, server: 64.225.59.162, request: "POST //wp-content/plugins/jekyll-exporter/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock://wp-content/plugins/jekyll-exporter/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php", host: "64.225.59.162", referrer: "https://www.google.com/"
2020/02/28 07:33:52 [error] 15252#15252: *12 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 95.217.130.88, server: 64.225.59.162, request: "POST //wp-content/plugins/mm-plugin/inc/vendors/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock://wp-content/plugins/mm-plugin/inc/vendors/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php", host: "64.225.59.162", referrer: "https://www.google.com/"
2020/02/28 07:33:52 [error] 15252#15252: *12 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 95.217.130.88, server: 64.225.59.162, request: "POST //www/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock://www/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php", host: "64.225.59.162", referrer: "https://www.google.com/"
2020/02/28 08:58:29 [error] 15252#15252: *45 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 46.119.158.129, server: 64.225.59.162, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "64.225.59.162:80"
2020/02/28 09:28:40 [error] 15252#15252: *47 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 189.51.114.40, server: 64.225.59.162, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "64.225.59.162:80"
2020/02/28 10:01:41 [error] 15252#15252: *51 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 105.112.56.238, server: 64.225.59.162, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "64.225.59.162"
2020/02/28 10:07:46 [error] 15252#15252: *54 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 78.165.205.193, server: 64.225.59.162, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "64.225.59.162:80"

running sudo journalctl -u nginx

-- Logs begin at Thu 2020-02-27 03:32:45 UTC, end at Fri 2020-02-28 11:18:20 UTC. --
Feb 27 10:41:35 ubuntu1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 27 10:41:35 ubuntu1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 27 10:41:35 ubuntu1 systemd[1]: Started A high performance web server and a reverse proxy server.
Feb 27 13:26:42 ubuntu1 systemd[1]: Stopping A high performance web server and a reverse proxy server...
Feb 27 13:26:42 ubuntu1 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Feb 27 13:26:42 ubuntu1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 27 13:26:42 ubuntu1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 27 13:26:42 ubuntu1 systemd[1]: Started A high performance web server and a reverse proxy server.
Feb 27 13:29:33 ubuntu1 systemd[1]: Stopping A high performance web server and a reverse proxy server...
Feb 27 13:29:33 ubuntu1 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Feb 27 13:29:33 ubuntu1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 27 13:29:33 ubuntu1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 27 13:29:33 ubuntu1 systemd[1]: Started A high performance web server and a reverse proxy server.
Feb 27 13:31:13 ubuntu1 systemd[1]: Stopping A high performance web server and a reverse proxy server...
Feb 27 13:31:13 ubuntu1 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Feb 27 13:31:13 ubuntu1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 27 13:31:13 ubuntu1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 27 13:31:13 ubuntu1 systemd[1]: Started A high performance web server and a reverse proxy server.
Feb 27 13:50:26 ubuntu1 systemd[1]: Stopping A high performance web server and a reverse proxy server...
Feb 27 13:50:26 ubuntu1 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Feb 27 13:50:26 ubuntu1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 27 13:50:26 ubuntu1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 27 13:50:26 ubuntu1 systemd[1]: Started A high performance web server and a reverse proxy server.
Feb 27 13:51:21 ubuntu1 systemd[1]: Stopping A high performance web server and a reverse proxy server...
Feb 27 13:51:21 ubuntu1 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Feb 27 13:51:21 ubuntu1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 27 13:51:21 ubuntu1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 27 13:51:21 ubuntu1 systemd[1]: Started A high performance web server and a reverse proxy server.
Feb 27 13:57:12 ubuntu1 systemd[1]: Stopping A high performance web server and a reverse proxy server...
Feb 27 13:57:12 ubuntu1 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Feb 27 13:57:12 ubuntu1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 27 13:57:12 ubuntu1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 27 13:57:12 ubuntu1 systemd[1]: Started A high performance web server and a reverse proxy server.
Feb 27 14:54:08 ubuntu1 systemd[1]: Stopping A high performance web server and a reverse proxy server...
Feb 27 14:54:09 ubuntu1 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Feb 27 14:54:09 ubuntu1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 27 14:54:09 ubuntu1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 27 14:54:09 ubuntu1 systemd[1]: Started A high performance web server and a reverse proxy server.
Feb 27 14:56:15 ubuntu1 systemd[1]: Stopping A high performance web server and a reverse proxy server...
Feb 27 14:56:15 ubuntu1 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Feb 27 14:56:15 ubuntu1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 27 14:56:15 ubuntu1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 27 14:56:15 ubuntu1 systemd[1]: Started A high performance web server and a reverse proxy server.
Feb 28 03:29:52 ubuntu1 systemd[1]: Stopping A high performance web server and a reverse proxy server...
Feb 28 03:29:52 ubuntu1 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Feb 28 03:29:52 ubuntu1 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 28 03:29:52 ubuntu1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 28 03:29:52 ubuntu1 systemd[1]: Started A high performance web server and a reverse proxy server.

Did you check your gunicorn.service and gunicorn.socket file? Did you add the correct user name for user in gunicorn.service file?

[Service]
User=<yourusername>
Group=www-data

if so try these commands

curl --unix-socket /run/gunicorn.sock localhost

if the above command prints out the html page in the terminal, then you are good to go or else change the gunicorn.service file and try again

sudo systemctl daemon-reload
sudo systemctl restart gunicorn

I think your service file should be like this, based on your github repo

WorkingDirectory=/home/djangoadmin/pyapps/medxFinal/medx

Try sudo tail -F /var/log/nginx/error.log and refresh to see the updates in the log. From the result it is easy to find a solution

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