首页 > 解决方案 > Nginx 上的 Laravel:访问应用程序的“public/”会导致“权限被拒绝”,即使它与 Nginx 具有相同的用户

问题描述

我一直在尝试使用 CentOS 7 在 Nginx 上部署全新的 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/and sudo chmod -R 775 /ROOT_OF_APP/,并确保 Nginx 和 PHP-FPM 的用户和组都是nginx(这将在下面解释)。

问题是: 为什么 nginx 不能访问“公共”,即使所有者/用户是 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;

(我想指出操作系统是CentOS 7,所以它没有www-data用户/组,不像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

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

附言

这是配置文件的样子。

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 不应该是导致此问题的原因。

标签: phplaravelnginxcentos

解决方案


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

我建议把你的项目放在 /var/www/project_folder

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

它会起作用的。


推荐阅读