[Django]-Django gunicorn sock file not created by wsgi

51πŸ‘

βœ…

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.

πŸ‘€Ben

5πŸ‘

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.

0πŸ‘

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

πŸ‘€Hashim

-2πŸ‘

Try run

sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl status gunicorn.service

The last line helped me to re-create .scok file

πŸ‘€Marco Aprea

Leave a comment