Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx Django and Gunicorn. Gunicorn sock file is missing?

I have an ansible provisioned VM based on this one https://github.com/jcalazan/ansible-django-stack but for some reason trying to start Gunicorn gives the following error:

Can't connect to /path/to/my/gunicorn.sock

and in nginx log file:

connect() to unix:/path/to/my/gunicorn.sock failed (2: No such file or directory) while connecting to upstream

And actually the socket file is missing in the specified directory. I have checked the permissions of the directory and they are fine.

Here is my gunicorn_start script:

NAME="{{ application_name }}"
DJANGODIR={{ application_path }}
SOCKFILE={{ virtualenv_path }}/run/gunicorn.sock
USER={{ gunicorn_user }}
GROUP={{ gunicorn_group }}
NUM_WORKERS={{ gunicorn_num_workers }}

# Set this to 0 for unlimited requests. During development, you might want to
# set this to 1 to automatically restart the process on each request (i.e. your
# code will be reloaded on every request).
MAX_REQUESTS={{ gunicorn_max_requests }}

echo "Starting $NAME as `whoami`"

# Activate the virtual environment.
cd $DJANGODIR
. ../../bin/activate

# Set additional environment variables.
. ../../bin/postactivate

# Create the run directory if it doesn't exist.
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Programs meant to be run under supervisor should not daemonize themselves
# (do not use --daemon).
exec gunicorn \
    --name $NAME \
    --workers $NUM_WORKERS \
    --max-requests $MAX_REQUESTS \
    --user $USER --group $GROUP \
    --log-level debug \
    --bind unix:$SOCKFILE \
    {{ application_name }}.wsgi

Can anyone suggest what else could cause the missing socket file?

Thanks

like image 702
kalo Avatar asked Feb 24 '15 06:02

kalo


2 Answers

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.

like image 136
swendr Avatar answered Sep 21 '22 22:09

swendr


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
like image 26
BringBackCommodore64 Avatar answered Sep 22 '22 22:09

BringBackCommodore64