Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5: laravel.log could not be opened: Permission Denied

To stop you from the start, there is NO permission issue. /storage is recursively chmodded 777 and whole project folder is chowned by apache:apache

I even renamed the log file to ...-old and apache created a new one... if it didn't have actual write permissions it would not had been allowed to create it.

Running under CentOS release 6.6 (Final)

Deployed the project from git, the homestead works for my colleague.

Full error:

[Mon May 18 10:17:58 2015] [error] [client 86.124.208.14] PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/var/www/vhosts/mapper.pavementlayers.com/storage/logs/laravel-2015-05-18.log" could not be opened: failed to open stream: Permission denied' in /var/www/vhosts/mapper.pavementlayers.com/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:84\nStack trace:\n#0 /var/www/vhosts/mapper.pavementlayers.com/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(88): Monolog\Handler\StreamHandler->write(Array)\n#1 /var/www/vhosts/mapper.pavementlayers.com/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\RotatingFileHandler->write(Array)\n#2 /var/www/vhosts/mapper.pavementlayers.com/vendor/monolog/monolog/src/Monolog/Logger.php(265): Monolog\Handler\AbstractProcessingHandler->handle(Array)\n#3 /var/www/vhosts/mapper.pavementlayers.com/vendor/monolog/monolog/src/Monolog/Logger.php(543): Monolog\Logger->addRecord(400, 'exception 'Symf...', Array)\n#4 /var/www/vhosts/mapper.pavementl in /var/www/vhosts/mapper.pavementlayers.com/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 84

like image 673
ied3vil Avatar asked May 18 '15 14:05

ied3vil


People also ask

How do I give permission to storage in laravel?

Change all file permissions to 644. Change all folder permissions to 755. For storage and bootstrap cache (special folders used by laravel for creating and executing files, not available from outside) set permission to 777, for anything inside.

Can't open append mode Linux?

If you aren't running your application as root on your web server, then it wont have write access based on the permissions you've provided. The error is complaining about permission denial for opening in append mode - it doesn't have permission to write to the file, only root does.

Could not be opened in append mode failed to open stream Permission denied PHP?

If you encountered an error that says “Could Not Be Opened In Append Mode: Failed To Open Stream: Permission Denied” such us the the image below, you can solve this by simply changing the permission access of certain file. The chmod 666 will allow all users to read and write but cannot execute the file.


2 Answers

It could be that SElinux is preventing Apache from creating this file.

To test this you could disable SElinux temporally with the following command:

setenforce 0

This will place SElinux in permissive mode. This means that you still receive an error message in your SElinux log file but SElinux will not block the command.

To activate SElinux again you can type:

setenforce 1

Or reboot your CentOS server.

Unfortunately, I had also problems with Laravel 5 on CentOS and the cause was SElinux. I ended up with disabling SElinux. I know it is not the right thing to do but I haven't had time to get the two working together yet!


Update

So I finally had sometime to investigate this further and I got SELinux working together with Laravel 5. I have just updated this post for people that might run into this issue cause. Disabling SELinux is not the best strategy as mentioned above.

Three things need to be done:

  1. The folders Storage and Bootstrap/Cache need to have the right SELinux context. This can be achieved via the following commands:

    semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/<Laravel Site>/storage(/.*)?"
    
    semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/<Laravel Site>/bootstrap/cache(/.*)?"
    
  2. The SELinux context needs to be applied on the directories:

    restorecon -Rv "/var/www/<Laravel Site>/storage"
    
    restorecon -Rv "/var/www/<Laravel Site>/bootstrap/cache"
    
  3. The Apache user needs to have the rights to create files in both directories. This can be achieved via a ACL in CentOS 7:

    setfacl -R -m u:apache:rwX storage/
    
    setfacl -R -m u:apache:rwX bootstrap/cache/
    

The last thing you need to do is to enable SELinux again.

like image 109
Jirennor Avatar answered Sep 19 '22 15:09

Jirennor


Try those commands for laravel 5

$ php artisan cache:clear 

$ sudo chmod -R 777 app/storage 

$ composer.phar dump-autoload

This happens because laravel do not have the permissions to write to the log file, at least for my case.

like image 43
Mustafa Dwekat Avatar answered Sep 20 '22 15:09

Mustafa Dwekat