Laravel权限被拒绝

11
今晚我尝试运行composer install,所有的依赖项都已正确安装,然后composer运行了php artisan clear compiled,但是出现了问题。
下面是错误信息:
> php artisan clear-compiled

                                                                                                                          
  [ErrorException]                                                                                                        
  file_put_contents(/srv/something/site/bootstrap/cache/services.php): failed to open stream: No such file or directory  
                                                                                                                          

Script php artisan clear-compiled handling the post-install-cmd event returned with an error


                      
  [RuntimeException]  
  Error Output:       
                      


install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-plugins] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-progress] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--ignore-platform-reqs] [--] [<packages>]...

自然地,我搜索了一下,发现在Laracast论坛上的每个人都说要创建我缺少的目录,所以我这样做了,命令再次正常运行。但是当我访问任何页面时,都会出现以下内容:file_put_contents(/differentNameDependingOnPageButAlwaysRandomLettersAndNumbers.php): failed to open stream: Permission denied 例如,这是登录页面的回溯信息:
ErrorException in Filesystem.php line 81:
file_put_contents(/8ff8cea6e3bb10ecec87b9d62c64f9768c4c4ab1.php): failed to open stream: Permission denied
in Filesystem.php line 81
at HandleExceptions->handleError('2', 'file_put_contents(/8ff8cea6e3bb10ecec87b9d62c64f9768c4c4ab1.php): failed to open stream: Permission denied', '/srv/something/site/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php', '81', array('path' => '/8ff8cea6e3bb10ecec87b9d62c64f9768c4c4ab1.php', 'contents' => '<?php $__env->startSection('title'); ?> Admin Login <?php $__env->stopSection(); ?> <?php $__env->startSection('form'); ?> <form class="form-horizontal" method="POST" action = "login"> <?php echo e($error); ?><?php /*TODO this doesn't show up now, when you do login make it show up*/ ?> <fieldset> <!-- Form Name --> <h1 class="section-heading" style="text-align: center;">Login</h1> <!-- Text input--> <div class="form-group"> <label class="col-md-4 control-label" for="email">Email</label> <div class="col-md-4"> <input id="email" name="email" type="email" id = "email" placeholder="user@domain.com" class="form-control input-md" required=""> </div> </div> <!-- Password input--> <div class="form-group"> <label class="col-md-4 control-label" for="password">Password</label> <div class="col-md-4"> <input id="password" name="password" type="password" placeholder="********" class="form-control input-md" required=""> </div> </div> <!-- Button --> <div class="form-group"> <label class="col-md-4 control-label" for="Submit"></label> <div class="col-md-4"> <button id="submit" name="submit" class="btn btn-primary">Submit</button> </div> </fieldset> </form> <?php $__env->stopSection(); ?> <?php $__env->startSection('message'); ?> <p class="text-muted" style="text-align:center">Don't have an account yet? Click <a href="../register">here</a> to register.</p> <?php $__env->stopSection(); ?> <?php echo $__env->make('auth.partials.master', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>', 'lock' => false))
at file_put_contents('/8ff8cea6e3bb10ecec87b9d62c64f9768c4c4ab1.php', '<?php $__env->startSection('title'); ?> Admin Login <?php $__env->stopSection(); ?> <?php $__env->startSection('form'); ?> <form class="form-horizontal" method="POST" action = "login"> <?php echo e($error); ?><?php /*TODO this doesn't show up now, when you do login make it show up*/ ?> <fieldset> <!-- Form Name --> <h1 class="section-heading" style="text-align: center;">Login</h1> <!-- Text input--> <div class="form-group"> <label class="col-md-4 control-label" for="email">Email</label> <div class="col-md-4"> <input id="email" name="email" type="email" id = "email" placeholder="user@domain.com" class="form-control input-md" required=""> </div> </div> <!-- Password input--> <div class="form-group"> <label class="col-md-4 control-label" for="password">Password</label> <div class="col-md-4"> <input id="password" name="password" type="password" placeholder="********" class="form-control input-md" required=""> </div> </div> <!-- Button --> <div class="form-group"> <label class="col-md-4 control-label" for="Submit"></label> <div class="col-md-4"> <button id="submit" name="submit" class="btn btn-primary">Submit</button> </div> </fieldset> </form> <?php $__env->stopSection(); ?> <?php $__env->startSection('message'); ?> <p class="text-muted" style="text-align:center">Don't have an account yet? Click <a href="../register">here</a> to register.</p> <?php $__env->stopSection(); ?> <?php echo $__env->make('auth.partials.master', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>', '0') in Filesystem.php line 81
at Filesystem->put('/8ff8cea6e3bb10ecec87b9d62c64f9768c4c4ab1.php', '<?php $__env->startSection('title'); ?> Admin Login <?php $__env->stopSection(); ?> <?php $__env->startSection('form'); ?> <form class="form-horizontal" method="POST" action = "login"> <?php echo e($error); ?><?php /*TODO this doesn't show up now, when you do login make it show up*/ ?> <fieldset> <!-- Form Name --> <h1 class="section-heading" style="text-align: center;">Login</h1> <!-- Text input--> <div class="form-group"> <label class="col-md-4 control-label" for="email">Email</label> <div class="col-md-4"> <input id="email" name="email" type="email" id = "email" placeholder="user@domain.com" class="form-control input-md" required=""> </div> </div> <!-- Password input--> <div class="form-group"> <label class="col-md-4 control-label" for="password">Password</label> <div class="col-md-4"> <input id="password" name="password" type="password" placeholder="********" class="form-control input-md" required=""> </div> </div> <!-- Button --> <div class="form-group"> <label class="col-md-4 control-label" for="Submit"></label> <div class="col-md-4"> <button id="submit" name="submit" class="btn btn-primary">Submit</button> </div> </fieldset> </form> <?php $__env->stopSection(); ?> <?php $__env->startSection('message'); ?> <p class="text-muted" style="text-align:center">Don't have an account yet? Click <a href="../register">here</a> to register.</p> <?php $__env->stopSection(); ?> <?php echo $__env->make('auth.partials.master', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>') in BladeCompiler.php line 102
at BladeCompiler->compile('/srv/something/site/resources/views/auth/login.blade.php') in CompilerEngine.php line 51
at CompilerEngine->get('/srv/something/site/resources/views/auth/login.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'error' => '')) in View.php line 135
at View->getContents() in View.php line 106
at View->renderContents() in View.php line 80
at View->render() in Response.php line 53
at Response->setContent(object(View)) in Response.php line 197
at Response->__construct(object(View)) in Router.php line 1030
at Router->prepareResponse(object(Request), object(View)) in ControllerDispatcher.php line 95
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(authController), object(Route), object(Request), 'getAdminLogin') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\authController', 'getAdminLogin') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 703
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 705
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 678
at Router->dispatchToRoute(object(Request)) in Router.php line 654
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54

到目前为止,我已经尝试以下方法来解决这个问题:
  1. 检查权限,将所有内容更改为 777 并由 www-data 拥有
  2. 尝试重新克隆没有供应商文件的空存储库,但无济于事,在运行composer install时仍然出现相同的错误
  3. 运行 php artisan cache:clear
  4. 运行 php artisan clear-compiled
  5. 运行 php artisan dump-autoload
  6. 根据 irc 的建议修改了 site/config/view.php ,并将 'compiled' => realpath(storage_path().'/framework/views') 修改为 'compiled' => storage_path('framework/views'), 这导致出现不同的错误:file_put_contents(/srv/something/site/storage/framework/views/8ff8cea6e3bb10ecec87b9d62c64f9768c4c4ab1.php): failed to open stream: No such file or directory (可能是因为它不应该在那里,但是谁知道这些信息可能会有用)
所有这些都无济于事。奇怪的是,这个问题似乎突然出现了。感谢您的帮助!

2
这似乎很明显是权限问题。我知道你说你执行了一个 777,但是你在哪里执行的呢?我建议尝试递归执行 777 命令在存储目录下:chmod -R 777 storage - jpschroeder
@jpschroeder 我已经尝试过了,但无济于事。这是我的Laravel根目录和存储目录的ls -l:https://git.io/vzxAE - Jake Sylvestre
2
你的ls -l显示整个目录都是由root拥有,而存储目录不可写,所以我认为你没有正确地更改权限。cd {laravel_root}并运行sudo chown -R www-data:www-data .``sudo chmod -R 777 storage - judereid
@judereid 我刚刚修复了它,但它仍然像以前一样出现了相同的问题(新的ls -l):https://git.io/vzhWr。我相信错误与php artisan clear-compiled错误有关。 - Jake Sylvestre
2
将权限设置为777是一个不好的习惯,考虑将文件夹所属组设置为您的Web服务器用户,并仅授予该组写入权限。请参考此答案:https://dev59.com/JF0a5IYBdhLWcg3wLV8G#37266353 - Daan
4个回答

30
原来是我缺少了 laravel_root/storage/ 的一些视图目录。为了解决这个问题,我需要执行以下操作:
  1. cd {laravel_root}/storage
  2. mkdir -pv framework/views app framework/sessions framework/cache
  3. cd ..
  4. chmod 664 -R storage
  5. chown -R www-data:www-data storage
之后,重新加载页面,问题就被修复了。感谢在 freenode 上的 #laravel 频道里的 Someguy123 解决了这个问题!

1
很高兴您成功地使其运行。奇怪的是,您不得不手动创建这些目录,因为它们是原始 laravel 仓库 的一部分。安装时您是否使用了 composer create-project 命令? - jpschroeder
@jpschroeder 是的,问题出在 git 不小心删除了文件。 - Jake Sylvestre
3
设置文件夹权限为777是一个不好的习惯,考虑将文件夹所属组设置为Web服务器用户,并只给该组写入权限。请参考此答案:https://dev59.com/JF0a5IYBdhLWcg3wLV8G#37266353 - Daan
在Windows上怎么样? - ßiansor Å. Ålmerol
请确保在Windows上使用管理员用户,@ßiansorÅ.Ålmerol。 - Jake Sylvestre
我做完这个之后,在 Artisan 命令上遇到了权限被拒绝的问题。 - Jay Bienvenu

1
如果您的Laravel应用程序文件夹权限为777,但仍然出现错误,则是因为Seliux已阻止它。可以使用以下命令解除应用程序文件夹的阻止。
su -c "chcon -R -h -t httpd_sys_script_rw_t /var/www/laravel-folder"

如果您使用了SElinux(请检查/var/log/audit/audit.log),并且尝试包含位于http根文件夹之外的php文件,请参阅https://dev59.com/aW_Xa4cB1Zd3GeqP3J4d - Solo.dmitry
永远不要使用777权限。请参见stackoverflow.com/a/37266353/470749。 - Ryan

1

我有同样的问题。这是在使用git clone创建新项目之后出现的。事实证明,原因是我在gitignore文件中忽略了/storage/framework/views/目录。我只需执行mkdir storage/framework/views/sudo chown -R www-data:www-data storage命令就可以恢复正常了。希望可以帮到其他人。


0

实际上,对我而言只有一个解决方案有效:添加sudo: sudo php artisan storage:link - 就像这样


这不是一个答案,它本可以作为对原帖的回复而不是一个答案。下次请注意,谢谢。 - Pluto
随意使用sudo来运行某些东西,仅仅为了检查是否解决了问题,这总是一个坏主意。 - Nico Haase

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接