Nginx displaying failed (13: Permission denied) when trying to access new site

37,891

Solution 1

Following the guide from this website did it for me (as root):

setsebool -P httpd_enable_homedirs 1
setenforce 0
systemctl restart nginx
systemctl daemon-reload

Solution 2

The solution for me was to set the /home/user/public_html permissions to 755. By default, it was being created with 751 permissions. This was blocking the nginx user from being able to 'read' it. Certain web hosting panels like VestaCP, CPanel, and others may inadvertently do this when adding a new site through their interface.

Solution: sudo chmod 755 ~/public_html (adjust path to your public_html folder)

Solution 3

It is your home directory permission that is denying access to nginx.

Try:

ls -ld /home/website

then

setfacl -R -m u:nginx:rwx /home/website

Or

chown -R nginx:nginx /home/website
chmod 655 /home/website
Share:
37,891

Related videos on Youtube

Chris
Author by

Chris

Updated on September 18, 2022

Comments

  • Chris
    Chris over 1 year

    I am trying to set up my own web server to learn a bit more about server admin.

    I have decided that I want to serve each sites files from a public_html folder inside the users /home directory.

    I have installed Nginx, edited the nginx.conf and changed the username / group to nginx.

    I have added a new user for the new site and changed the vhosts file to look like so;

    server {
        listen         80;
        listen         [::]:80;
        server_name    website.com www.website.com;
        root           /home/website/public_html;
        index          index.html index.htm index.php;
    
        location / {
          try_files $uri $uri/ =404;
        }
    
        location ~* \.php$ {
        fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    But when I try and get to the site, it returns a 404 Not Found.

    When I check the error log, I am seeing the following errors;

    2019/01/02 19:49:45 [crit] 18248#0: *1 stat() "/home/website/public_html/" failed (13: Permission denied)
    

    Any chance someone has come across this before and could tell me how to handle it?

    I have had a look around and saw some posts about getenforce, but when i run it, it says Disabled.

    I am using CentOS7 if that makes any difference.

    Cheers,

    • Jenny D
      Jenny D over 5 years
    • Chris
      Chris over 5 years
      @JennyD .. Im using nginx not Apache
    • Jenny D
      Jenny D over 5 years
      You need the same basic set of permissions regardless of which webserver you're running. Obviously you need to replace the username apache with whatever username nginx is running under.
    • Chris
      Chris over 5 years
      No, that didn't work ... Im still getting the exact same errors
    • Michael Hampton
      Michael Hampton over 5 years
      Don't serve web sites from user home directories, for a wide variety of reasons.
    • chicks
      chicks over 5 years
      Is selinux enabled?
  • samayres1992
    samayres1992 over 5 years
    default user for nginx is www-data so it's likely going to need to be sudo chown -R www-data:www-data /home/website
  • JKhan
    JKhan over 5 years
    @samayres1992 user created for centos/rhel is 'nginx'
  • Paul Dydyshko
    Paul Dydyshko over 4 years
    @TedKhi Samayres is right though. Unless you also explain that you need to change the default user that nginx runs as by updating /etc/nginx/nginx.conf to change the user www-data line to read user nginx instead, then the above would not work (unless they had already done that).
  • Renil Babu
    Renil Babu about 4 years
    worked for me. I had to do 777 instead of 655. Thanks
  • TetraDev
    TetraDev over 3 years
    @RenilBabu Using 777 is a bad idea - that means ANYONE can write to you directory. Use 755 instead