Issue
I have gotta my project setup with uwsgi, django, nginx
Everything seems to be working fine but somehow I keep on getting the error on getting static files
I have been reading through online and tried all the possible ways but I keep on getting this permission denied
error on my static folder.
Can someone please let me know what I have done wrong with the permission and how I should change it?
this is my /var/log/nginx/error.log
open() "/root/project/static/*.css" failed (13: Permission denied), client: 2xx.xx.xx.xxx, server: _, request: "GET /static/*.css HTTP/1.1", host: "1xx.xx.xx.xxx"
This is my nginx site-available config
server {
listen 80 default_server;
listen [::]:80 default_server;
# root /var/www/html;
# Add index.php to the list if you are using PHP
# index index.html index.htm index.nginx-debian.html;
server_name _;
#location = /favicon.ico { access_log off; log_not_found off; }
#location /media {
# root /root/project/mediafiles;
#}
location ^~ /static/ {
allow all; # this is from one of the posts but no luck
auth_basic off; # this is from one of the posts but no luck
root /root/project;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi/project.sock;
}
}
as my folder permission
for project folder it's drwxr-xr-x 23 www-data www-data
for static folder it's drwxr-x--- 8 www-data www-data 4096 May 23 14:40 static
I never made the permission to static 755
too but no luck.
Anyways, this is using root as user instead of having an extra user and root is also in group of www-data
Thanks in advance for all the help.
EDIT:
As suggested this is the output of ps aux | grep nginx
root 810 0.0 0.0 124972 1440 ? Ss 02:18 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 811 0.0 0.0 125688 4840 ? S 02:18 0:00 nginx: worker process
www-data 812 0.0 0.0 125348 3196 ? S 02:18 0:00 nginx: worker process
root 1159 0.0 0.0 14224 1004 pts/0 S+ 04:25 0:00 grep --color=auto nginx
Solution
The problem I am guessing is the fact that your project root
directory is at /root
. The default permissions for /root
are:
drwx------ 14 root root 4096 May 8 12:47 root
As you can see, other users, such as www-data
don't even have read access to the /root
directory. In Linux FS, if you need to read something at a path/a/b/c
, you need to have read access to each of the folders in that path.
The Nginx worker process runs as user www-data
which is trying to open a file that is rooted at /root
where this user does not have read permissions, and therefore raising a Permission denied (13)
.
See this demo for more detail:
$ cat /root/test2/text.txt
cat: /root/test2/text.txt: Permission denied
$ sudo cat /root/test2/test.txt
A
$ sudo ls -la /root/ | grep test2
drwxrwxrwx 2 root root 4096 May 24 02:04 test2
Hope this makes sense. The solution would be on of the following:
- Run nginx workers as root (not recommended)
- Move your project directory to a location that is designed to be accessed by multiple users such as
/usr/local/share
or/var/www/
(recommended)
Answered By - rtindru Answer Checked By - Timothy Miller (WPSolving Admin)