django gunicorn sock file not created by wsgi

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.


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.