django gunicorn sock file not created by wsgi

时光毁灭记忆、已成空白 提交于 2019-11-30 04:15:35

问题


I have a basic django rest application in my digital ocean server (Ubuntu 16.04) with a local virtual environment. The basic wsgi.py is:

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)

I have followed step by step this tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

When I test Gunicorn's ability to serve the project with this command: gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application All works well.

So I've tried to setup Gunicorn to use systemd service file. My /etc/systemd/system/gunicorn.service file is:

[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

My Nginx configuration is:

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;
    }
}

I've changed listen port from 80 to 8000 because 80 give me a err_connection_refused error. After starting the server with this command:

sudo systemctl restart nginx

When I try to run my website, I get an 502 Bad Gateway error. I've tried these commands (found on the tutorial comments):

sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl restart nginx

but nothing changes. When I take a look at the Nginix logs with this command:

sudo tail -f /var/log/nginx/error.log

I can read that sock file doesn't exists:

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"

Why this sock file isn't created? How can I configure django/gunicorn to create this file? I have added gunicorn in my INSTALLED_APP in my Django project but it doesn't change anything.

EDIT:

When I test the nginx config file with nginx -t I get an error: open() "/run/nginx.pid" failed (13: Permission denied). But if I run the command with sudo: sudo nginx -t, the test is successful. Does that mean that I have to allow 'ben' user to run Ngnix?

About gunicorn logfile, I cannot find a way to read them. Where are they stored?

When I check whether gunicorn is running by using ps aux | grep gunicorn:

ben      26543  0.0  0.2  14512  1016 pts/0    S+   14:52   0:00 grep --color=auto gunicorn

Here is hat happens when you run the systemctl enable and start commands for gunicorn:

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:


I had to change the permissions of my sock folder:

sudo chown ben:www-data /home/ben/myproject/

Another thing is that I have changed the sock location after reading in many post that it's not a good pratice to keep the sock file in the django project. My new location is:

/home/ben/run/

Don't forget to change permissions:

sudo chown ben:www-data /home/ben/run/

To be sure that gunicorn is refreshed, run these commands:

pkill gunicorn
sudo systemctl daemon-reload
sudo systemctl start gunicorn

That will kill the gunicorn processes and start new ones.

You can run this command to make the process start at server boot:

sudo systemctl enable gunicorn

All works well now.




回答2:


While the accepted answer works, there is one (imo major) issue with it, which is that the gunicorn web server is (probably) running as root, which is not recommended. The reason you end up needing to chown the socket is because it is owned by root:root, because that is the user/group your init job assumes by default. There are multiple ways to get your job to assume another role. As of this time (with gunicorn 19.9.0), in my opinion, the simplest solution to this is to use the --user and --group flags provided as part of the gunicorn command. This means your server can start with the user/group you specify. In your case:

exec gunicorn --user ben --group www-data --bind unix:/home/ben/myproject/myproject.sock -m 007 wsgi

will start gunicorn under ben:www-data user and create a socket owned by ben:www-data with the permissions 770, or read/write/execute privilege for the user ben and group www-data on the socket, which is exactly what you ned in this case.




回答3:


I have given path to the sock file outside my project. I needed to just create the directory so that the gunicorn can create the file inside that directory as I had had mentioned that path in the .services file. Basically, I made sure that I had all directories existing according to the path in the .services file. No need to change permissions or ownership



来源:https://stackoverflow.com/questions/39919053/django-gunicorn-sock-file-not-created-by-wsgi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!