首页 文章

Laravel存储框架和日志文件/文件夹权限问题

提问于
浏览
1

当我们从不同的源生成Laravel的文件/文件夹权限时,我遇到了问题 . 这可能有点长,但请耐心等待 .

Scenario

1)通过 php artisan test:command 触发的名为 TestCommand.phpartisan 命令具有以下代码:

public function handle(){
  \Log::info("Test Command Executed from Command Line");
}

2) TestController.php 中的控制器功能,在访问 http://localhost/test 时触发,具有以下代码:

public function test(){
  \Log::info("Test Command Executed from Web View");
}

单独触发时,这两个函数都能正常工作, but as soon as one is triggered, the other will not work, 因为 storage/logs 中生成的日志文件现在具有不正确的权限,用户和/或组 .

Situation 1

如果我运行 php artisan test:command ,我会得到以下日志文件:

-rw-r--r-- | 1 | tim | wheel | 76B | 25 Aug 14:03 | laravel-2017-08-25.log

导航到 http://localhost/test 会返回以下错误:

此页面无法正常工作localhost目前无法处理此请求 . HTTP错误500

没有显示Laravel堆栈跟踪(“Woops,出错”),因为只有在写入Laravel日志后才会发生 .

Situation 2

如果我首先导航到 http://localhost/test ,我会得到这个日志文件:

-rw-r--r-- | 1 | _www | wheel | 72B | 25 Aug 14:06 | laravel-2017-08-25.log

并运行 php artisan test:command 返回以下内容:

PHP致命错误:未捕获异常'UnexpectedValueException',消息'流或文件“... / storage / logs / laravel-2017-08-25.log”无法打开:无法打开流:权限被拒绝'in ... / vendor / monolog / monolog / src / Monolog / Handler / StreamHandler.php:97堆栈跟踪:...

Conclusion

请注意,用户现在是 _www 而不是 tim ,这是在 storage/logs 中给定文件的生成源时可以预期的 . 追溯性地,通过运行可以很容易地修复它:

sudo chmod -R 777 storage/logs

但出于多种原因,这不是一个好的解决方案:

  • 777对于 生产环境 Web服务器来说是一个坏主意,755似乎不适用于我的配置 .

  • 每次创建新文件时(每日日志,任何时间为 storage/framework/* 中的文件等)都需要再次运行此命令 .

  • 我无法直接访问此命令必须每天运行x次的网络服务器,这意味着我不得不让别人做错 .

鉴于这一切,如何统一文件以允许任何一个源创建文件,然后根据需要修改文件?

Webserver是apache,dev环境是Mac, 生产环境 是linux

Update

我认为一个好的解决方案是在同一组中使用 tim_www ,并使用 storage/logs 分配给该组

chgrp -R <user_group> storage

但接下来的问题是,默认情况下,Laravel中任何生成的文件夹的权限都是 644 ,它不允许 group 进行写访问,只能读取 .

如果可能的话,将考虑修改它 .

1 回答

  • 0

    找到了解决这个问题的方法;访问控制列表!

    在Mac上,有一个 chmod +a ... 选项,它会将默认目录权限添加到您指定的任何文件夹中 . 在我的情况下,我想使用允许 tim_www 完全访问 storage 下的文件/文件夹的ACL,因为它们都会不断地访问/写入它:

    chmod -R +a 'tim allow read,write,delete,add_file,add_subdirectory,file_inherit,directory_inherit' storage
    chmod -R +a '_www allow read,write,delete,add_file,add_subdirectory,file_inherit,directory_inherit' storage
    

    递归地应用此选项可以在存储目录中的Laravel生成的任何文件夹上设置ACL,最重要的是 storage/framework/cache .

    在Linux上,ACL似乎略有不同,但想法是一样的:

    chmod g+s storage
    setfacl -R -d -m u::rwx storage
    setfacl -R -d -m g::rx storage
    setfacl -R -d -m o::rx storage
    

    这将有效地将存储(和所有子文件夹)的文件权限设置为 755 ,这似乎是我的配置需要允许正确写入 .

相关问题