502 Bad Gateway: Permission Denied for Gunicorn Socket
2024/09/29 14:04:13 [crit] 6631#6631: *1 connect() to unix:/home/ubuntu/myproject/Ecom_project/ecom_p/gunicorn.sock failed (13: Permission denied) while connecting to upstream, client: 103.175.89.121, server: ecomweb-abhishadas.buzz, request: "GET / HTTP/1.1", upstream: "``http://unix``:/home/ubuntu/myproject/Ecom_project/ecom_p/gunicorn.sock:/", host: "www.ecomweb-abhishadas.buzz"
What I Tried: Checked Socket Permissions:
I ran the command ls -l /home/ubuntu/myproject/Ecom_project/ecom_p/gunicorn.sock
to verify the permissions of the socket file.
I noticed that the permissions did not allow the Nginx user (www-data) to access the socket.
Changed Socket Permissions:
I modified the permissions of the socket file using chmod
to ensure that the Nginx user has the necessary permissions to access it.
Example command: sudo chmod 660 /home/ubuntu/myproject/Ecom_project/ecom_p/gunicorn.sock
Set Socket Ownership.
I ensured that the socket file is owned by the user running Gunicorn (e.g., ubuntu or whatever user you specified).
Example command: sudo chown ubuntu:www-data /home/ubuntu/myproject/Ecom_project/ecom_p/gunicorn.sock
Restarted Services.
I restarted both Nginx and Gunicorn to apply the changes.
Commands used:
sudo systemctl restart nginx
sudo systemctl restart gunicorn
(or the specific command you use to start Gunicorn).
What I Was Expecting:
I was expecting that after adjusting the permissions and ownership of the Gunicorn socket file, Nginx would be able to connect to Gunicorn without the permission denied error. This would allow my Django application to be served properly, resolving the 502 Bad Gateway error when accessing my site.