Ubuntu 16.04下Nginx部署Django遇权限拒绝错误求助
Hey there, I’ve dealt with this exact permission issue when deploying Django with Nginx on Ubuntu before—let’s get your site up and running smoothly.
Why This Happens
The error connect() to unix:/home/teddycrepineau/contoursandcolors/contoursandcolors.sock failed (13: Permission denied) means the Nginx process (which runs as the www-data user by default) doesn’t have access to your uWSGI socket file. That socket was probably created under your user account (teddycrepineau), and by default, www-data can’t read or write to files owned by another user’s private group.
Step-by-Step Fixes
Check the socket’s current permissions
First, confirm who owns the socket and what access it allows:ls -l /home/teddycrepineau/contoursandcolors/contoursandcolors.sockYou’ll almost certainly see it’s owned by
teddycrepineau:teddycrepineau, with tight permissions like600(only the owner can interact with it).Add Nginx’s user to your group
Givewww-dataaccess to files in your user group by adding it to theteddycrepineaugroup:sudo usermod -aG teddycrepineau www-dataThe
-aGflag ensures we append the group instead of overwriting any existing groupswww-databelongs to.Adjust socket permissions (optional but recommended)
To make sure future uWSGI restarts create the socket with the right permissions, add this line to your uWSGI config file (likeuwsgi.inior your systemd service file):chmod-socket = 664If you need to fix the existing socket immediately, run:
sudo chmod 664 /home/teddycrepineau/contoursandcolors/contoursandcolors.sockThis gives read/write access to the owner and group (which now includes
www-data).Restart services to apply changes
Restart Nginx to pick up the group membership change, and restart uWSGI to refresh the socket (if you updated its config):sudo systemctl restart nginx sudo systemctl restart your-uwsgi-service-name # Replace with your actual uWSGI service name
Quick Extra Check
Make sure your project directory has permissions that let www-data traverse into it:
sudo chmod 755 /home/teddycrepineau/contoursandcolors
This lets all users (including www-data) enter the directory to access the socket.
内容的提问来源于stack exchange,提问作者Teddy

