php 如何修复错误:laravel.log 无法打开?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23411520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:42:46  来源:igfitidea点击:

How to fix Error: laravel.log could not be opened?

phplaravel

提问by FRR

I'm pretty new at laravel, in fact and I'm trying to create my very first project. for some reason I keep getting this error (I haven't even started coding yet)

事实上,我对 Laravel 还很陌生,我正在尝试创建我的第一个项目。出于某种原因,我不断收到此错误(我什至还没有开始编码)

Error in exception handler: The stream or file "/var/www/laravel/app/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied in /var/www/laravel/bootstrap/compiled.php:8423

I've read this has something to do with permissions but chmod -R 775 storagedidn't help at all.

我读过这与权限有关,但chmod -R 775 storage根本没有帮助。

Permissions

权限

采纳答案by FRR

To fix this issue, you need to change the ownership of the directory to the unix user that the webserver uses.

要解决此问题,您需要将目录的所有权更改为 Web 服务器使用的 unix 用户。

  1. Get out of the VM
  2. Using the console, go to your synced folder (vagrant)
  3. sudo chown -R $USER:www-data storage
  4. chmod -R 775 storage
  1. 退出虚拟机
  2. 使用控制台,转到您的同步文件夹(vagrant)
  3. sudo chown -R $USER:www-data storage
  4. chmod -R 775 存储

Even though I created the project within the VM using the VM user, the folder belonged to the user in the real computer; so, when trying to

虽然我在虚拟机中使用虚拟机用户创建了项目,但该文件夹属于真机中的用户;所以,当试图

Now it's working.

现在它正在工作。

Thanks to all those that helped me figure this thing out

感谢所有帮助我解决这个问题的人

EDIT:

编辑:

Actually, it still wasn't working, it still gave me a "permission denied" problem.

实际上,它仍然无法正常工作,它仍然给我一个“权限被拒绝”的问题。

Here's what I did, I modified my Vagrantfile like this:

这就是我所做的,我像这样修改了我的 Vagrantfile:

config.vm.synced_folder "./app","/var/www/", create:true,
:owner => "vagrant",
:group => "www-data",
:mount_options => ["dmode=775","fmode=664"]

回答by Hamid Parchami

Never set a directory to 777. you should change directory ownership. so set your current user that you are logged in with as owner and the webserver user (www-data, apache, ...) as the group. You can try this:

切勿将目录设置为 777。您应该更改目录所有权。因此,将您当前登录的用户设置为所有者,并将网络服务器用户(www-data、apache、...)设置为组。你可以试试这个:

sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache

then to set directory permission try this:

然后设置目录权限试试这个:

chmod -R 775 storage
chmod -R 775 bootstrap/cache

Update:

更新:

Webserver user and group depend on your webserver and your OS. to figure out what's your web server user and group use the following commands. for nginx use:

网络服务器用户和组取决于您的网络服务器和操作系统。要弄清楚您的 Web 服务器用户和组是什么,请使用以下命令。对于 nginx 使用:

ps aux|grep nginx|grep -v grep

ps aux|grep nginx|grep -v grep

for apache use:

对于 apache 使用:

ps aux | egrep '(apache|httpd)'

ps aux | egrep '(apache|httpd)'

回答by Antonio Carlos Ribeiro

Never use 777 for directories on your live server, but on your own machine, sometimes we need to do more than 775, because

千万不要在你的直播服务器上使用 777 作为目录,但是在你自己的机器上,有时我们需要做的比 775 多,因为

chmod -R 775 storage

Means

方法

7 - Owner can write
7 - Group can write
5 - Others cannot write!

If your webserver is not running as Vagrant, it will not be able to write to it, so you have 2 options:

如果您的网络服务器不是作为 Vagrant 运行,它将无法写入,因此您有 2 个选择:

chmod -R 777 storage

or change the group to your webserver user, supposing it's www-data:

或将组更改为您的网络服务器用户,假设它是www-data

chown -R vagrant:www-data storage

回答by Turan Zamanl?

It also may be SELinux. (Centos, RedHat)

它也可能是 SELinux。(Centos,红帽)

Determine status of SElinux on terminal:

确定终端上 SElinux 的状态:

$ sestatus

If status is enabled, write command to disable SElinux

如果状态已启用,则编写命令以禁用 SElinux

$ setenforce Permissive

Or you may execute this command

或者你可以执行这个命令

$ sudo setenforce 0

$ sudo setenforce 0

回答by Daniel Dewhurst

You need to adjust the permissions of storageand bootstrap/cache.

您需要调整的权限storagebootstrap/cache

  • cdinto your Laravel project.
  • sudo chmod -R 755 storage
  • sudo chmod -R 755 bootstrap/cache
  • cd进入你的 Laravel 项目。
  • sudo chmod -R 755 storage
  • sudo chmod -R 755 bootstrap/cache

You can try 777 if 755 doesn't work. 777 is not secure though!

如果 755 不起作用,您可以尝试 777。777虽然不安全!

Depending on how your web server is setup, you may be able to be more specific with your permissions, and only grant them to your web server user. Google WEB SERVER NAME Laravel file permissionsfor more information.

根据您的 Web 服务器的设置方式,您可以更具体地设置您的权限,并且只将它们授予您的 Web 服务器用户。谷歌WEB SERVER NAME Laravel file permissions了解更多信息。

At the time of writing, this is for Laravel 5.4

在撰写本文时,这是针对 Laravel 5.4

回答by Davron Achilov

Add to composer.json

添加 composer.json

"scripts": {
    "post-install-cmd": [
          "chgrp -R www-data storage bootstrap/cache",
          "chmod -R ug+rwx storage bootstrap/cache"
     ]
}

After composer install

composer install

回答by gauravbhai daxini

Run following commands and you can add sudoat starting of command depends on your system:

运行以下命令,您可以sudo在命令开始时添加取决于您的系统:

chmod -R 775 storage/framework
chmod -R 775 storage/logs
chmod -R 775 bootstrap/cache 

回答by Daniel Santos

For all Centos 7 users on a Laravel context, there is no need to disable Selinux, just run these commands:

对于 Laravel 上下文中的所有 Centos 7 用户,无需禁用 Selinux,只需运行以下命令:

yum install policycoreutils-python -y # might not be necessary, try the below first

semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/laravel/storage(/.*)?" # add a new httpd read write content to sellinux for the specific folder, -m for modify
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/laravel/bootstrap/cache(/.*)?" # same as the above for b/cache

restorecon -Rv /var/www/html/ # this command is very important to, it's like a restart to apply the new rules

Lastly, make sure your hosts, ips and virtual hosts are all correctly for remote accessing.

最后,确保您的主机、ips 和虚拟主机都正确用于远程访问。

Selinux is intended to restrict access even to root users, so only the necessary stuff might be accessed, at least on a generalist overview, it's extra security, disabling it is not a good practise, there are many links to learn Selinux, but for this case it is not even required.

Selinux 旨在限制甚至 root 用户的访问,因此只能访问必要的内容,至少在通才概述中,它具有额外的安全性,禁用它不是一个好习惯,有很多链接可以学习 Selinux,但为此如果它甚至不需要。

回答by Thilina Dharmasena

If you use cmd

如果你使用 cmd

sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache

If you use GUI

如果你使用图形界面

First go to the project and right click on the storage and check the properties and go to the Permissions tab

首先转到项目并右键单击存储并检查属性并转到权限选项卡

enter image description here

在此处输入图片说明

Change the permissions using below code

使用以下代码更改权限

sudo chmod -R 777 storage

Then your file properties may be

那么你的文件属性可能是

enter image description here

在此处输入图片说明

Then check your settings and execute laravel command it will work :)

然后检查您的设置并执行 laravel 命令它会工作:)

回答by Pratik

In Laravel, you should set ACL on storageand cachedirectory so that web server user can read/write on the directory. Open a new terminal and run following:

在Laravel,你应该设置ACLstoragecache目录,以便Web服务器用户可以读取/目录上写。打开一个新终端并运行以下命令:

HTTPDUSER=$(ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1)

sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX bootstrap/cache storage/
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX bootstrap/cache storage/

References:

参考:

https://symfony.com/doc/3.4/setup/file_permissions.html#using-acl-on-a-system-that-supports-setfacl-linux-bsd

https://symfony.com/doc/3.4/setup/file_permissions.html#using-acl-on-a-system-that-supports-setfacl-linux-bsd

https://linux.die.net/man/1/setfacl

https://linux.die.net/man/1/setfacl