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

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)

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 storage didn't help at all.

Permissions

This question is related to php laravel

The answer is


Delete "/var/www/laravel/app/storage/logs/laravel.log" and try again:

rm storage/logs/laravel.log


I had this problem on Laravel Forge.

The issue was that I was using "Website Isolation" and logged in using SSH with the default "forge" user instead of with the isolated username.

This of course caused the issue as permissions were not correct when using the "forge" user for the website in question.

Using the correct isolated username to SSH into the server did the trick.

Perhaps this helps others in the same situation.


Add to composer.json

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

After composer install


You need to adjust the permissions of storage and bootstrap/cache.

  • cd into your Laravel project.
  • 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!

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 permissions for more information.

At the time of writing, this is for Laravel 5.4


I wasn't too keen on changing my folder permissions to 777. Here's how I went about fixing this issue.

First, I changed the user who is running the web server on my local machine(I run nginx, but the principles apply everywhere):

$> sudo vim /etc/nginx/nginx.conf
user <my_user> #inside nginx.conf
service nginx reload

Afterwards, I created another index.php file under the public/ folder to find out who was running my php-fpm version and where I would go about changing that:

<?php
phpinfo();
?>

Reloading the page, I found out that www-data was the user(under the environment section). I also found out I was running php 7.1. I proceeded to change the user:

$> sudo vim /etc/php/7.0/fpm/pool.d/www.conf 
#Look for www-data or the following variables: user, group, listen.user, listen.group.

Finally, I gave the following permissions to folders:

sudo chmod -R 775 ./storage/

Now, I made sure that I was the owner of the folders by using a simple:

ls -al

If you set the server and php-fpm users to yourself and the folders are owned by root for example, then you will keep encountering this issue. This can happen if you did a sudo laravel new <project> as root. In that case, make sure you use a recursive chown command on your project to change the user:group settings. In most default cases, www-data is the main setting for the server and php, in that case it's a matter of making sure the folder isn't out of www-data's reach.

My project is setup in my home directory. On Ubuntu 16.04 and Laravel 5.5.


1- nginx user and php-fpm and your site owner user must be same :

run command sudo vi /etc/nginx/nginx.conf change like bellow

user nginx nginx;

run command sudo vi /etc/nginx/nginx.conf change like bellow

listen.owner = nginx
listen.group = nginx
listen.mode = 0660

then restart nginx and php-fpm service

and go in project path and run below command

sudo chmod nginx:nginx -R .

2- change file SELinux security context by run following commands in project path

chcon -R -t httpd_sys_content_t .
chcon -R -t httpd_sys_rw_content_t .

This error can be fixed by disabling Linux.

Check if it has been enabled

sestatus

You try..

setenforce 0


try this

  1. cd /var/www/html
  2. setenforce 0
  3. service httpd restart

This solution is specific for laravel 5.5

You have to change permissions to a few folders: chmod -R -777 storage/logs chmod -R -777 storage/framework for the above folders 775 or 765 did not work for my project

chmod -R 775 bootstrap/cache 

Also the ownership of the project folder should be as follows (current user):(web server user)


If chmod does not work, you can try the following:

sudo nano /etc/php/7.x/fpm/pool.d/www.conf

Change the following paramaters:

user = www-data
group = www-data

To:

user = vagrant
group = vagrant

Remember to restart PHP FPM

sudo service php7.x-fpm restart

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

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

Had this issue and found this and it solved the issue.

enter image description here


For this error:

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

use this command in terminal:

sudo chmod -R 777 storage

If problem persists after setting permissions right for the storage and bootstrap/cache folders, first confirm by turning off selinux with the command

setenforce 0 If this works after the above command, this should allow writing, but you've turned off added security server-wide. That's bad. Turn SELinux back.

setenforce 1 Then finally use SELinux to allow writing of the file by using this command

chcon -R -t httpd_sys_rw_content_t storage And you're good to go!

This link helped a lot!


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

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:

chmod -R 777 storage

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

chown -R vagrant:www-data storage

I stuck on this issue tried different commands but these will help to solve the problem

php artisan route:clear
php artisan config:clear
php artisan cache:clear

Hope it's helped others too.


It also may be SELinux. (Centos, RedHat)

Determine status of SElinux on terminal:

$ sestatus

If status is enabled, write command to disable SElinux

$ setenforce Permissive

Or you may execute this command

$ sudo setenforce 0


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

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.

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.


(on Ubuntu) : Can be solved in 2 simple steps :

$ sudo chmod -R 777 storage 

And

$ sudo service apache2 restart

If you use 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 :)


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:

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:

ps aux|grep nginx|grep -v grep

for apache use:

ps aux | egrep '(apache|httpd)'


In my particular case I had a config file generated and cached into the bootstrap/cache/ directory so my steps where:

  1. Remove all generated cached files: rm bootstrap/cache/*.php
  2. Create a new laravel.log file and apply the update of the permissions on the file using:

    • chmod -R 775 storage

It might be late but may help someone, changing directory permissions worked for me.

Assuming that your Laravel project is in /var/www/html/ directory. Goto this directory.

cd /var/www/html/

Then change permissions of storage/ and bootstrap/cache/ directories.

sudo chmod -R 777 storage/
sudo chmod -R 777 bootstrap/cache/

Fixed my problem with this command in centos 7.6 Server

chcon -R -t httpd_sys_content_t $SITE_PATH

chcon -R -t httpd_sys_rw_content_t $SITE_PATH

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

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://linux.die.net/man/1/setfacl


This is work for me.

Essa foi a única solução que funcionou para mim. Estou usando Docker e Laradock

chmod -Rvc 775 storage


Maximum people's are suggesting to change file permission 777 or 775, which I believe not an appropriate approach to solve this problem. You just need to change the ownership of storage and bootstrap folder.

In below Image you can see all my files/folder are under the root user(except storage and bootstrap, because I changed the ownership ),but I logged in as a administrator(before changing ownership) that's why it always giving permission denied. So I need to change the ownership of this two folder to administrator

So how I did this, go to your project directory and run below commands. sudo chown -R yourusername:www-data storage, sudo chmod -R ug+w storage, sudo chown -R yourusername:www-data bootstrap, sudo chmod -R ug+w bootstrap


You could do:

chcon -R -t httpd_sys_rw_content_t storage