【问题标题】:The stream or file could not be opened in append mode: failed to open stream: Permission denied无法以附加模式打开流或文件:无法打开流:权限被拒绝
【发布时间】:2021-10-01 15:07:53
【问题描述】:

我正在使用 NGINX 和 PHP7.4-fpm 在 RHEL 7 机器中托管一个 Laravel 项目

在我的nginx.conf 中,我将用户设置如下

user nexta;

在我的.../.../php-fpm.d/www.conf

user = nexta
group = nexta

listen = /var/run/php-fpm/php-fpm.sock

listen.owner = nexta
listen.group = nexta
listen.mode = 0660

使用此设置,我设法运行了 Laravel 应用程序,但是,我遇到了权限被拒绝错误

上面写着:

The stream or file "/home/nexta/mywebsite.com/storage/logs/laravel-2021-07-25.log" could not be opened in append mode: failed to open stream: Permission denied

我尝试了以下方法

  1. 设置 chown 和 chmod
sudo chown -R nexta:nexta /home/nexta/mywebsite.com
sudo chown -R nexta:nexta /home/nexta/mywebsite.com/storage
sudo chown -R nexta:nexta /home/nexta/mywebsite.com/bootstrap/cache

sudo chmod -R 775 /home/nexta/mywebsite.com/storage
sudo chmod -R 775 /home/nexta/mywebsite.com/bootstrap/cache
  1. 尝试使用 777(我知道这不是一个好习惯)
sudo chmod -R 777 /home/nexta/mywebsite.com/storage
sudo chmod -R 777 /home/nexta/mywebsite.com/bootstrap/cache
  1. 将 nginx.conf 中的用户更改为 nginx,并将站点更改为 nginx。错误仍然存​​在,我也无法查看我的应用程序

  2. 执行以下操作:

php artisan cache:clear
composer dumpautoload

我还注意到,如果我运行php artisan cahce:clear(注意错字),这会触发错误并且应用程序能够访问日志文件,但是当通过 http 请求访问应用程序时,应用程序无法访问访问日志文件。

我也尝试在 php 请求中回显 whoami(使用非 laravel 应用程序),得到以下响应

Returned with status 0 and output: Array ( [0] => nexta )

也就是说,当应用通过http请求执行时,用户是nexta。

此时,我什至不确定是什么导致了错误。

【问题讨论】:

  • 检查谁拥有/home/nexta/mywebsite.com/storage/logs/laravel-2021-07-25.log 有时我以root身份运行某些东西,它以root身份生成文件,然后这对我的网络用户来说是个问题
  • @apokryfos 我忘了说,日志文件也归 Nexta 所有

标签: php laravel nginx rhel


【解决方案1】:

无论出于何种原因,您都应该锁定该文件。

您可以通过以下任一方式解决他的问题:

rm <log file>

或者如果你想保留它

mv <log file> <log_file>-old

【讨论】:

    【解决方案2】:

    无论出于何种原因,以下命令解决了我的问题

    chcon -R -t httpd_sys_rw_content_t storage
    
    chcon -R -t httpd_sys_rw_content_t bootstrap/cache
    

    【讨论】:

      猜你喜欢
      • 2021-10-03
      • 2021-07-18
      • 2019-02-10
      • 2021-10-03
      • 2021-02-09
      • 2021-06-26
      • 2020-11-21
      • 2019-06-30
      • 2021-05-11
      相关资源
      最近更新 更多