将 Django 应用程序部署到数字海洋失败(502 Bad Gateway)

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

大家好,我在使用 Gunicorn 和 Ngnix 将 Django 应用程序部署到数字海洋时遇到了一些麻烦。我正在按照我在 udemy 上购买的视频教程中的步骤进行操作,我们正在按照 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。当我按照步骤配置 gunicorn 和 Nginx 时,我在使用 IP 地址访问时得到 502 Bad Gateway

我想知道为什么我会遇到这个问题,我注意到当我使用 sudo systemctl status gunicorn.socket 检查 gunicorn 的状态时,我得到:

   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)

这里有一个 link 项目,如果需要,我试图添加到服务器 https://github.com/opeodedeyi/medxFinal

当我 运行 sudo nginx -t 我得到

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

以防万一我的 gunicorn 服务文件是这样设置的

[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

只是解释我的文件夹的结构,在 medxFinal 里面是 venv 和 medx 文件夹,在 medx 文件夹里面是 manage.py 和其他文件夹,包括另一个 medx 文件夹,最后一个 medx 里面是 settings.py、wsgi 和第一个 URL 所在的位置。

运行宁sudo tail -F /var/log/nginx/error.log我看到这个

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"

运行宁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.

您检查过您的 gunicorn.service 和 gunicorn.socket 文件了吗? 您是否为 gunicorn.service 文件中的用户添加了正确的用户名?

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

如果是,请尝试这些命令

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

如果上述命令在终端中打印出 html 页面,那么您就可以开始了,否则请更改 gunicorn.service 文件并重试

sudo systemctl daemon-reload
sudo systemctl restart gunicorn

我认为你的服务文件应该是这样的,基于你的github repo

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

尝试sudo tail -F /var/log/nginx/error.log 并刷新以查看日志中的更新。从结果很容易找到解决方案