Laravel: file_put_contents() permission denied — correct storage/framework/cache permissions?
Asked Answered
S

5

16

I'm having struggles with editing the Laravel cache, which is located in storage/framework/cache. I've got a job running that saves to a certain cache, but every time the job runs, this error occurs:

ERROR: file_put_contents(/var/www/html/---/storage/framework/cache/data/3c/c7/3cc7fd54b5a3cb08ceb0754f58371cec1196159a): failed to open stream: Permission denied

Details

  • When I save to the same cache (e.g. with the same key, there is no error)
  • I am running on nginx
  • Already have I run this command sudo chown -R www-data:www-data storage in the folder the Laravel application is located, as well as sudo chmod -R 775 /home/<user>/<laravel folder>/storage
  • Performing ls -lh /storage/framework/cache returns the following: drwsrwsr-x 55 www-data www-data 4.0K Jan 18 20:56 data.
  • Now I'm just wondering what the full, correct, Laravel permission set is and how to restore that set-up.

Any help is appreciated! Thank you in advance.

Sorci answered 19/1, 2019 at 8:4 Comment(0)
S
30

I cleared the cache completely using sudo php artisan cache:clear. Afterwards, the problem never occurred.

Opposed to Ismoil's answer: never make the Laravel storage folder 777. It poses a security risk.

Sorci answered 20/1, 2019 at 18:17 Comment(3)
if it help you. Why are you not accept answer? Yes you can't do this it risk you can set 775 . But sometimes you need to fix your problem and return to 775Escarpment
As per @Ismoil Shifoev, he set the permission to 775 not 777. Is there any possible risk with 775?Petua
I came across the exactly same issue. Really not sure what happened here. I got the error when accessing a site from Australia, but not when accessing it from Europe (presumably because it wasn't trying to write to the same cache file).Ramonaramonda
E
3

1) If you take a look at your cache.php config file you'll see that for the file driver the storage/framework/cache/data folder is set for writing:

'file' => [
    'driver' => 'file',
    'path' => storage_path('framework/cache/data'),
],

That means that the permissions for that folder must be properly set so that the web server user can successfully write to that folder.

2) or you can just running this command for me it solved my problem

php artisan cache:clear 
chmod -R 775 storage/
composer dump-autoload
Escarpment answered 19/1, 2019 at 10:9 Comment(2)
Thank you, Ismoil. What does composer dump-autoload do, actually?Sorci
It will just regenerated all classes that need to be included in projectEscarpment
B
3

Actually just need

php artisan cache:clear 

But its just temporary, when login account with new ip or something. Its still got issued.

So try to chmod:

chmod -R 775 storage

and

chmod -R 775 storage/*

Its solve problems

Barn answered 12/1, 2022 at 8:41 Comment(0)
H
-1

If you're using redis then you should set the .env variable

CACHE_DRIVER=redis
Helvellyn answered 19/1, 2022 at 15:46 Comment(0)
P
-2

To resolve the issue, I executed the command sudo php artisan optimize:clear to completely clear the optimization. This action prevented the problem from reoccurring.

Prolamine answered 26/3 at 4:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.