我正在尝试使用 nginx 和 gunicorn 在 digitalocean 上部署 django 项目。我的项目具有以下结构
projects
|_isli
|_isli
|_forecast #project directory
|_manage.py
|_forecast.sock
|_forecast
|_wsgi.py
|_settings.py
|_urls.py
我的项目在根目录中创建而没有创建额外的 sudo 用户。我知道这不是正确的解决方案,但我决定这样做。在settings.py允许的主机内的文件中,我指定了 IP 地址
ALLOWED_HOSTS = ['165.22.23.233']
在官方 digitalocean 文档中有关于使用 nginx 和 gunicorn 部署 django 的教程 使用 Nginx 和 Gunicorn部署 django
在本文中使用了 gunicorn 设置为 socet 的方法,这是我的设置/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/root/projects/isli/isli/forecast
ExecStart=/root/projects/isli/env/bin/gunicorn --log-level debug --error-logfile /var/log/gunicorn/error.log --access-logfile /var/log/gunicorn/access.log --workers 3 --bind unix:/root/projects/isli/isli/forecast/forecast.sock forecast.wsgi:application
[Install]
WantedBy=multi-user.target
在创建 gunicorn.service 文件之后,我运行它而systemctl start gunicorn不是systemctl enable gunicorn在我的项目目录中创建了 forecast.sock 文件之后,我在/etc/nginx/sites-available/forecast 下面设置了 nginx
server {
listen 165.22.23.233:80;
location = /favicon.ico {access_log off; log_not_found off;}
location / {
include proxy_params;
proxy_pass http://unix:/root/projects/isli/isli/forecast/forecast.sock;
}
}
比systemctl restart nginx
当我尝试http://165.22.23.233:80从浏览器访问时,它会提示我 502 bad gateway。在/var/log/nginx/error.log文件中之后,我看到以下内容
2020/02/09 16:29:01 [crit] 13533#13533: *11 connect() to unix:/root/projects/isli/isli/forecast/forecast.sock failed (
13: Permission denied) while connecting to upstream, client: 178.176.218.110, server: , request: "GET / HTTP/1.1", upstream: "http://unix:/root/projects/isli/isli/forecast/forecast.sock:/", host: "165.22.23.233"
正如我通过这个错误理解的那样,我的问题是 nginx 无法访问/root/projects/isli/isli/forecast/forecast.sock文件。之后,我尝试通过以下方式检查对上述路径的每个实体的权限
namei -nom /root/projects/isli/isli/forecast/forecast.sock
这是输出
f: /root/projects/isli/isli/forecast/forecast.sock
drwxr-xr-x root root /
drwx------ root root root
drwxr-xr-x root root projects
drwxr-xr-x root root isli
drwxr-xr-x root root isli
drwxr-xr-x root root forecast
srwxrwxrwx root root forecast.sock
在上面的输出中,root 用户对我的套接字路径的每个实体都有权限,但是为什么错误说我权限被拒绝