[Fixed]-Nginx Django and Gunicorn. Gunicorn sock file is missing?

27👍

Well, since I don’t have enough rep to comment, I’ll mention here that there is not a lot of specificity suggested by the missing socket, but I can tell you a bit about how I started in your shoes and got things to work.

The long and short of it is that gunicorn has encountered a problem when run by upstart and either never got up and running or shut down. Here are some steps that may help you get more info to track down your issue:

  • In my case, when this happened, gunicorn never got around to doing any error logging, so I had to look elsewhere. Try ps auxf | grep gunicorn to see if you have any workers going. I didn’t.
  • Looking in the syslog for complaints from upstart, grep init: /var/log/syslog, showed me that my gunicorn service had been stopped because it was respawning too fast, though I doubt that’ll be your problem since you don’t have a respawn in your conf. Regardless, you might find something there.
  • After seeing gunicorn was failing to run or log errors, I decided to try running it from the command line. Go to the directory where your manage.py lives and run the expanded version of your upstart command against your gunicorn instance. Something like (Replace all of the vars with the appropriate litterals instead of the garbage I use.):

    /path/to/your/virtualenv/bin/gunicorn --name myapp --workers 4 --max-requests 10 --user appuser --group webusers --log-level debug --error-logfile /somewhere/I/can/find/error.log --bind unix:/tmp/myapp.socket myapp.wsgi

  • If you’re lucky, you may get a python traceback or find something in your gunicorn error log after running the command manually. Some things that can go wrong:

    • django errors (maybe problems loading your settings module?). Make sure your wsgi.py is referencing the appropriate settings module on the server.
    • whitespace issues in your upstart script. I had a tab hiding among spaces that munged things up.
    • user/permission issues. Finally, I was able to run gunicorn as root on the command line but not as a non-root user via the upstart config.

Hope that helps. It’s been a couple of long days tracking this stuff down.

👤swendr

9👍

I encountered the same problem after following Michal Karzynski’s great guide ‘Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL‘.

And this is how I solved it.

I had this variable in the bash script used to start gunicorn via Supervisor (myapp/bin/gunicorn_start):

SOCKFILE={{ myapp absolute path }}/run/gunicorn.sock

Which, when you run the bash script for the first time, creates a ‘run’ folder and a sock file using root privileges. So I sudo deleted the run folder, and then recreated it without sudo privileges and voila! Now if you rerun Gunicorn or Supervisor you won’t have the annoying missing sock file error message anymore!

TL;DR

  1. Sudo delete run folder.
  2. Recreate it without sudo privileges.
  3. Run Gunicorn again.
  4. ????
  5. Profit

4👍

The error could also arise when you haven’t pip installed a requirement. In my case, looking at the gunicorn error logs, I found that there was a missing module. Usually happens when you forget to pip install new requirements.

1👍

Well, I worked on this issue for more than a week and finally was able to figure it out.
Please follow links from digital ocean , but they did not pinpoint important issues one which includes

  1. no live upstreams while connecting to upstream
  2. *4 connect() to unix:/myproject.sock failed (13: Permission denied) while connecting to upstream
  3. gunicorn OSError: [Errno 1] Operation not permitted
  4. *1 connect() to unix:/tmp/myproject.sock failed (2: No such file or directory)

    etc.

These issues are basically permission issue for connection between Nginx and Gunicorn.
To make things simple, I recommend to give same nginx permission to every file/project/python program you create.

To solve all the issue follow this approach:
First thing is :

  1. Log in to the system as a root user
  2. Create /home/nginx directory.
  3. After doing this, follow as per the website until Create an Upstart Script.
  4. Run chown -R nginx:nginx /home/nginx
  5. For upstart script, do the following change in the last line :
    exec gunicorn –workers 3 –bind unix:myproject.sock -u nginx -g nginx wsgi
    DONT ADD -m permission as it messes up the socket. From the documentation of Gunicorn, when -m is default, python will figure out the best permission
  6. Start the upstart script
  7. Now just go to /etc/nginx/nginx.conf file.
    Go to the server module and append:

    location / {
    include proxy_params;
    proxy_pass http<>:<>//unix:/home/nginx/myproject.sock;
    }
    REMOVE <>
    Do not follow the digitalocean aricle from here on

    1. Now restart nginx server and you are good to go.

1👍

I had the same problem and found out that I had set the DJANGO_SETTINGS_MODULE to production settings in the the gunicorn script and the wsgi settings were using dev.

I pointed the DJANGO_SETTINGS_MODULE to dev and everything worked.

Leave a comment