【发布时间】:2017-02-16 13:26:18
【问题描述】:
我的数字海洋服务器 (Ubuntu 16.04) 中有一个基本的 django rest 应用程序,具有本地虚拟环境。 基本的wsgi.py是:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "workout_rest.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)
当我测试 Gunicorn 使用此命令为项目服务的能力时: gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application 一切正常。
所以我尝试设置 Gunicorn 以使用 systemd 服务文件。 我的 /etc/systemd/system/gunicorn.service 文件是:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ben
Group=www-data
WorkingDirectory=/home/ben/myproject
ExecStart=/home/ben/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/ben/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
我的 Nginx 配置是:
server {
listen 8000;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ben/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ben/myproject/myproject.sock;
}
}
我已将监听端口从 80 更改为 8000,因为 80 给了我一个 err_connection_refused 错误。 使用此命令启动服务器后:
sudo systemctl restart nginx
当我尝试运行我的网站时,我收到 502 Bad Gateway 错误。 我已经尝试过这些命令(在教程 cmets 中找到):
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl restart nginx
但没有任何改变。 当我用这个命令查看 Nginix 日志时:
sudo tail -f /var/log/nginx/error.log
我可以读到 sock 文件不存在:
2016/10/07 09:00:18 [crit] 24974#24974: *1 connect() to unix:/home/ben/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 86.197.20.27, server: 139.59.150.116, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ben/myproject/myproject.sock:/", host: "server_ip_adress:8000"
为什么没有创建这个 sock 文件?如何配置 django/gunicorn 来创建这个文件? 我在 Django 项目的 INSTALLED_APP 中添加了 gunicorn,但它没有改变任何东西。
编辑:
当我使用 nginx -t 测试 nginx 配置文件时,我收到一个错误:open() "/run/nginx.pid" failed (13: Permission denied)。
但是如果我用 sudo:sudo nginx -t 运行命令,则测试成功。这是否意味着我必须允许 'ben' 用户运行 Ngnix?
关于 gunicorn 日志文件,我找不到读取它们的方法。它们存储在哪里?
当我使用ps aux | grep gunicorn检查gunicorn是否正在运行时:
ben 26543 0.0 0.2 14512 1016 pts/0 S+ 14:52 0:00 grep --color=auto gunicorn
当您为 gunicorn 运行 systemctl enable 和 start 命令时会发生这种情况:
sudo systemctl enable gunicorn
Synchronizing state of gunicorn.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable gunicorn
sudo systemctl start gunicorn
I get no output with this command
sudo systemctl is-active gunicorn
active
sudo systemctl status gunicorn
● gunicorn.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2016-10-06 15:40:29 UTC; 23h ago
Oct 06 15:40:29 DevUsine systemd[1]: Started gunicorn.service.
Oct 06 18:52:56 DevUsine systemd[1]: Started gunicorn.service.
Oct 06 20:55:05 DevUsine systemd[1]: Started gunicorn daemon.
Oct 06 20:55:17 DevUsine systemd[1]: Started gunicorn daemon.
Oct 06 21:07:36 DevUsine systemd[1]: Started gunicorn daemon.
Oct 06 21:16:42 DevUsine systemd[1]: Started gunicorn daemon.
Oct 06 21:21:38 DevUsine systemd[1]: Started gunicorn daemon.
Oct 06 21:25:28 DevUsine systemd[1]: Started gunicorn daemon.
Oct 07 08:58:43 DevUsine systemd[1]: Started gunicorn daemon.
Oct 07 15:01:22 DevUsine systemd[1]: Started gunicorn daemon.
【问题讨论】:
-
以下是否出现错误? 1.用
nginx -t测试nginx配置文件。 2. 检查 gunicorn 日志文件。 3. 使用ps aux | grep gunicorn检查gunicorn是否在运行。如果其中任何一个出现错误,请添加到您的问题中。 -
当您为 gunicorn 运行 systemctl enable 和 start 命令时会发生什么?当您执行
sudo systemctl is-active gunicorn或sudo systemctl status gunicorn时,您会看到什么? -
@Ben 在
/var/log/syslog发布您所看到的内容。您是否使用sudo ufw delete allow 8000和sudo ufw allow 'Nginx Full'正确设置了防火墙?您发布的 nginx 文件是否正确符号链接到/etc/nginx/sites-enabled? -
@YPCrumble
/var/log/syslog为空。是的,我已经使用sudo ufw delete allow 8000和sudo ufw allow 'Nginx Full'正确设置了防火墙。我的 nginx 文件正确符号链接到/etc/nginx/sites-enabled。 (如果我运行此命令ls -l /etc/nginx/sites-enabledmyproject 文件以黄色突出显示。)
标签: python django nginx gunicorn