Nginx 上的 Laravel:访问应用程序的 "public/" 会导致 "Permission denied",即使它与 Nginx 具有相同的用户

Laravel on Nginx: accessing to "public/" of the app results in "Permission denied" even though it has the same user as Nginx

我一直在尝试使用 CentOS 7 在 Nginx 上部署一个 b运行d 新 Laravel 6 应用程序,但我在错误日志中收到以下错误消息.

*13 stat() "/ROOT_OF_APP/public/" failed (13: Permission denied), 
client: 127.0.0.1, server: HOST_NAME, request: "GET / HTTP/1.1", host: "HOST_NAME"

*13 stat() "/ROOT_OF_APP/public/" failed (13: Permission denied), 
client: 127.0.0.1, server: HOST_NAME, request: "GET / HTTP/1.1", host: "HOST_NAME"

*13 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, 
client: 127.0.0.1, server: HOST_NAME, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "HOST_NAME"

第一行说"/ROOT_OF_APP/public/" failed (13: Permission denied)

所以,我运行sudo chown -R nignx:nginx /ROOT_OF_APP/public/sudo chmod -R 775 /ROOT_OF_APP/,并确保Nginx和PHP-FPM的用户和组都是nginx (这将在下面解释)。

问题是: 为什么nginx不能访问"public",即使owner/user是nginx?

(第三条消息(Primary script unknown)也很困扰,不知道是不是权限问题)

/etc/php-fpm.d/www.conf 中,您会看到这些行。

user = nginx
group = nginx

listen.owner = nginx
listen.group = nginx
listen.mode = 0660

/etc/nginx/nginx.conf 中,您会看到该行。

user nginx;

(我要注意 OS 是 CentOS 7,所以它没有 www-data user/group,不像 Ubuntu.)

如果我运行ps aux | grep php-fpm...

user     24394  0.0  0.0 112708   988 pts/1    S+   14:28   0:00 grep --color=auto php-fpm
root     26979  0.0  0.0 306464 10520 ?        Ss   13:57   0:00 php-fpm: master process (/etc/php-fpm.conf)
nginx    26985  0.0  0.0 318712  5804 ?        S    13:57   0:00 php-fpm: pool www
nginx    26986  0.0  0.0 318712  5796 ?        S    13:57   0:00 php-fpm: pool www
nginx    26987  0.0  0.0 318712  5800 ?        S    13:57   0:00 php-fpm: pool www
nginx    26988  0.0  0.0 318712  5800 ?        S    13:57   0:00 php-fpm: pool www
nginx    26989  0.0  0.0 318712  5804 ?        S    13:57   0:00 php-fpm: pool www

如果我运行ps aux | grep nginx...

root      2990  0.0  0.0 122420  5608 ?        Ss   14:01   0:00 nginx: master process /usr/sbin/nginx
nginx    26985  0.0  0.0 318712  5804 ?        S    13:57   0:00 php-fpm: pool www
...
# The 2nd line is repeated several times
...
nginx    31299  0.0  0.0 134672  4212 ?        S    14:15   0:00 nginx: worker process
# This "nginx: worker process" is repeated several times too

我完全一无所知...任何建议将不胜感激。

PS

这是配置文件的样子。

server {
        listen 80;
        listen [::]:80 ipv6only=on;

        access_log /var/log/nginx/MY-APP-access.log;
        error_log /var/log/nginx/MY-APP-error.log;

        root /ROOT_OF_APP/public;
        index index.php index.html index.htm;

        server_name HOST_NAME;

          location / {
            try_files $uri $uri/ /index.php?$args;
          }

          location ~* \.php$ {
              include /etc/nginx/fastcgi_params;
              fastcgi_pass 127.0.0.1:9000;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              fastcgi_index index.php;
        }

        location = /favicon.ico { log_not_found off; access_log off; }
          location = /robots.txt  { log_not_found off; access_log off; }
}

sudo nginx -t说明语法没问题

此外,"SELinux enforcing" 状态已更改为 "Permissive",因此 SELinux 不应是导致此问题的原因。

当我尝试将我的项目放在外面时,这发生在我身上 /var/www>

我建议将您的项目放在 /var/www/project_folder

然后根据需要更改您的 nginx 配置文件。

它会起作用的。

这听起来像是 SELinux 问题,尤其是正如其他人指出的那样,因为您位于非默认目录中。

SELinux 是否强制执行? (运行 "getenforce")

如果那又回来了"Enforcing"我敢打赌这就是问题所在。

如果是这样,您可以暂时禁用它,重新启动您的服务,看看问题是否消失(运行 "setenforce 0")

然后,假设您想要打开 SELinux,(至少可以说强烈建议)调查 "ausearch -m AVC" 的输出。 Post 回到这里寻求帮助。