0

I use laravel new test to establish the Laravel project and move all directories into /usr/share/nginx/html/, the default root directory of Nginx. In order to check if the config of nginx is correct, I simply the config, trun on autoindex, and put index,html into /usr/share/nginx/html/test/public/, the location of index.php of laravel.

defaul.conf of nginx

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    root   /usr/share/nginx/html/test;
    index  index.html index.htm index.php;

    location / {
        autoindex on;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

When I access the index.html by localhost/public/, nginx returns me 403. I copy index.html into a new folder named /usr/share/nginx/html/test/public_b, and try to access the new index.html by localhost/public_b/. Nginx returns me the correct content! It is like a miracle. The stats of my directories as the follows.

$ll /usr/share/html/test
total 432
drwxrwxr-x.  6 nginx nginx     84 Feb 26 15:39 app
-rw-rw-r--.  1 nginx nginx   1686 Feb 26 15:39 artisan
drwxrwxr-x.  3 nginx nginx     34 Feb 26 15:39 bootstrap
-rw-rw-r--.  1 nginx nginx   1512 Feb 26 15:39 composer.json
-rw-rw-r--.  1 nginx nginx 145089 Feb 26 15:39 composer.lock
drwxrwxr-x.  2 nginx nginx    247 Feb 26 15:39 config
drwxrwxr-x.  5 nginx nginx     72 Feb 26 15:39 database
-rwxrwxr-x.  1 nginx nginx     13 Feb 26 17:46 index.html
-rw-rw-r--.  1 nginx nginx   1150 Feb 26 15:39 package.json
-rw-rw-r--.  1 nginx nginx   1040 Feb 26 15:39 phpunit.xml
drwxr-xr-x.  4 root  root     116 Feb 26 18:05 public
drwxr-xr-x.  2 root  root      24 Feb 26 17:50 public_c
drwxrwxr-x.  5 nginx nginx     45 Feb 26 15:39 resources
drwxrwxr-x.  2 nginx nginx     75 Feb 26 16:50 routes
-rw-rw-r--.  1 nginx nginx    563 Feb 26 15:39 server.php
drwxr-xr-x.  5 nginx nginx     46 Feb 26 15:39 storage
drwxrwxr-x.  4 nginx nginx     83 Feb 26 15:39 tests
drwxrwxr-x. 37 nginx nginx   4096 Feb 26 15:40 vendor
-rw-rw-r--.  1 nginx nginx    549 Feb 26 15:39 webpack.mix.js
-rw-rw-r--.  1 nginx nginx 258941 Feb 26 15:39 yarn.lock

$ ll /usr/share/nginx/html/test/public
total 12
drwxrwxr-x. 2 nginx nginx   21 Feb 26 15:39 css
-rw-rw-r--. 1 nginx nginx    0 Feb 26 15:39 favicon.ico
-rwxr-xr-x. 1 root  root    13 Feb 26 17:49 index.html
-rw-rw-r--. 1 nginx nginx 1823 Feb 26 15:39 index.php
drwxrwxr-x. 2 nginx nginx   20 Feb 26 15:39 js
-rw-rw-r--. 1 nginx nginx   24 Feb 26 15:39 robots.txt

$ ll /usr/share/nginx/html/test/public_c/
total 4
-rwxr-xr-x. 1 root root 13 Feb 26 17:50 index.html

I have read a lot of relative questions and pages, but nothing help against this problem. What is wrong in my actions or settings?


Update

Thanks @Adam Kozlowski. I tried chmod 777 public -R and changing owner to both of root and nginx, but the problem was not solved.

IvanaGyro
  • 598
  • 7
  • 25

2 Answers2

4

I found the problem: SELinux blocked Nginx to access the folders.

$ ls /usr/share/nginx/html/test/ -Z
drwxr-xr-x. root  root  unconfined_u:object_r:config_home_t:s0 public
drwxr-xr-x. root  root  unconfined_u:object_r:httpd_sys_content_t:s0 public_c

Only the files with the tag, httpd_sys_content_t, can be accessed by HTTP.

Command:

chcon -R -t httpd_sys_content_t /usr/share/nginx/html/test

And I also need to add httpd_sys_rw_content_t to /usr/share/nginx/html/test/storage to let Laravel work.

Command:

chcon -R -t httpd_sys_rw_content_t/usr/share/nginx/html/test/storage

Referring Permissions Issue with Laravel on CentOS

IvanaGyro
  • 598
  • 7
  • 25
0

Set proper permission for directory, as a root user:

chmod a+rwx directory-name -R
Adam Kozlowski
  • 5,606
  • 2
  • 32
  • 51