配置 gunicorn 时出错

配置 gunicorn 时出错

我一直在关注这个教程在 Digital Ocean 上部署我的 django 项目。我正在尝试配置gunicorn

我的项目结构看起来与此类似:

在此处输入图片描述

在我的settings.py我使用DEBUG=False

我创建了 gunicorn.socket 和 gunicorn.service。

/etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

/etc/systemd/system/gunicorn.service

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

[Service]
User=someuser
Group=www-data
WorkingDirectory=/home/someuser/myproject
ExecStart=/home/someuser/myproject/myprojectenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          Myproject.wsgi:application

[Install]
WantedBy=multi-user.target

我启动并启用 Gunicorn 套接字:

sudo systemctl start gunicorn.socket 
sudo systemctl enable gunicorn.socket

检查进程的状态以确定它是否能够启动:

sudo systemctl status gunicorn.socket

这是我得到的,然后它返回到命令行。

Failed to dump process list, ignoring: No such file or directory
●  gunicorn.socket - gunicorn socket
   Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
   Active: active (listening) since Sat 2019-05-04 23:12:03 UTC; 13s ago
   Listen: /run/gunicorn.sock (Stream)
   CGroup: /system.slice/gunicorn.socket

May 04 23:12:03 myproject systemd[1]: Listening on gunicorn socket.

接下来,我检查 /run 目录中是否存在 gunicorn.sock 文件:

file /run/gunicorn.sock
/run/gunicorn.sock: socket

似乎文件或目录不存在,但没有提供更多详细信息。 gunicorn.sock 似乎存在。

我决定完成整个教程并看看会发生什么:

这是 gunicorn 套接字的日志:

$ sudo journalctl -u gunicorn.socket

-- Logs begin at Fri 2019-05-03 21:38:51 UTC, end at Tue 2019-05-07 20:05:36 UTC. --
May 07 20:04:42 myproject systemd[1]: Listening on gunicorn socket.

测试套接字激活

$ sudo systemctl status gunicorn

gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

为了测试套接字激活机制,我们可以通过输入以下命令通过 curl 向套接字发送连接:

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

运行这个,转到 myip:8000,教程说我应该在终端中看到我的应用程序的 HTML 输出,但这并没有发生。

过了一会儿,什么也没发生,我进入了终端:

<h1>Bad Request (400)</h1>

要验证 Gunicorn 服务是否正在运行:

$ sudo systemctl status gunicorn

gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-05-07 20:06:15 UTC; 1min 5s ago
 Main PID: 1884 (gunicorn)
    Tasks: 4 (limit: 1152)
   CGroup: /system.slice/gunicorn.service
           1884 /home/someuser/myproject/myprojectenv/bin/python3 /home/someuser/myproject/myprojectenv/bin/gunicorn --access-
           1915 /home/someuser/myproject/myprojectenv/bin/python3 /home/someuser/myproject/myprojectenv/bin/gunicorn --access-
           1916 /home/someuser/myproject/myprojectenv/bin/python3 /home/someuser/myproject/myprojectenv/bin/gunicorn --access-
           1917 /home/someuser/myproject/myprojectenv/bin/python3 /home/someuser/myproject/myprojectenv/bin/gunicorn --access-

May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1904] [INFO] Booting worker with pid: 1904
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1905] [INFO] Booting worker with pid: 1905
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1902)
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1904)
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1905)
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1915] [INFO] Booting worker with pid: 1915
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1916] [INFO] Booting worker with pid: 1916
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1917] [INFO] Booting worker with pid: 1917
May 07 20:07:02 myproject gunicorn[1884]:  - - [07/May/2019:20:07:02 +0000] "GET / HTTP/1.1" 400 26 "-" "curl/7.58.0"
May 07 20:07:10 myproject gunicorn[1884]:  - - [07/May/2019:20:07:10 +0000] "GET / HTTP/1.1" 400 26 "-" "curl/7.58.0"

检查 gunicorn 日志:

$ sudo journalctl -u gunicorn

-- Logs begin at Fri 2019-05-03 21:38:51 UTC, end at Tue 2019-05-07 20:07:58 UTC. --
May 07 20:06:15 myproject systemd[1]: Started gunicorn daemon.
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1884] [INFO] Starting gunicorn 19.9.0
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1884] [INFO] Listening at: unix:/run/gunicorn.sock (1884)
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1884] [INFO] Using worker: sync
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1902] [INFO] Booting worker with pid: 1902
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1904] [INFO] Booting worker with pid: 1904
May 07 20:06:16 myproject gunicorn[1884]: [2019-05-07 20:06:16 +0000] [1905] [INFO] Booting worker with pid: 1905
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1902)
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1904)
May 07 20:06:46 myproject gunicorn[1884]: [2019-05-07 20:06:46 +0000] [1884] [CRITICAL] WORKER TIMEOUT (pid:1905)
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1915] [INFO] Booting worker with pid: 1915
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1916] [INFO] Booting worker with pid: 1916
May 07 20:06:47 myproject gunicorn[1884]: [2019-05-07 20:06:47 +0000] [1917] [INFO] Booting worker with pid: 1917
May 07 20:07:02 myproject gunicorn[1884]:  - - [07/May/2019:20:07:02 +0000] "GET / HTTP/1.1" 400 26 "-" "curl/7.58.0"
May 07 20:07:10 myproject gunicorn[1884]:  - - [07/May/2019:20:07:10 +0000] "GET / HTTP/1.1" 400 26 "-" "curl/7.58.0"

之后就是nginx的配置:

$ sudo nano /etc/nginx/sites-available/Myproject 

server {
    listen 80;
    server_name xxx.xxx.xxx.xxx;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /staticfiles/ {
        root /home/someuser/myproject ;
    }

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

通过将文件链接到 sites-enabled 目录来启用该文件:

$ sudo ln -s /etc/nginx/sites-available/Myproject /etc/nginx/sites-enabled

输入以下命令测试 Nginx 配置是否存在语法错误:

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

如果没有报告错误,请继续并输入以下命令重新启动 Nginx:

$ sudo systemctl restart nginx

最后,我们需要打开防火墙以允许端口 80 上的正常流量。由于我们不再需要访问开发服务器,因此我们可以删除打开端口 8000 的规则:

$ sudo ufw delete allow 8000
Rule deleted
Rule deleted (v6)

$ sudo ufw allow 'Nginx Full'
Rule added
Rule added (v6)

您现在应该可以转到您的服务器的域或 IP 地址来查看您的应用程序。

转到xxx.xxx.xxx.xxx:8000,我了解到该站点无法访问,响应时间太长。

在我经历的这一切中settings.py,我有这样的经历:

ALLOWED_HOSTS = ["xxx.xxx.xxx.xxx"]  # my droplet's ip

我熟悉 Python,但不熟悉服务器和部署,所以我很茫然。

几个月前,我曾按照类似的教程成功安装了 Ubuntu 16.04,但现在却一直遇到问题。

似乎出现了一些奇怪的字符,因为我从 PyCharm 终端复制粘贴了输出。

相关内容