"storage/logs/laravel-2019-11-22.log" could not be opened: failed to open stream: Permission denied

24,842

Solution 1

Alright I got the answer:

AWS AMI uses webapp as the web user, not apache or ec2-user as the file shows. In that case, the webapp user has no access rights over those files.

sudo chown $USER:webapp ./storage -R

find ./storage -type d -exec chmod 775 {} \;

find ./storage -type f -exec chmod 664 {} \;

Solution 2

Using the console, go to your synced folder (vagrant)

sudo chown -R $USER:www-data storage
chmod -R 775 storage
Share:
24,842
Kyle Corbin Hurst
Author by

Kyle Corbin Hurst

Updated on December 06, 2020

Comments

  • Kyle Corbin Hurst
    Kyle Corbin Hurst over 3 years

    I got the following error when entering my site on a production server:

    The stream or file "/var/app/current/storage/logs/laravel-2019-11-22.log" could not be opened: failed to open stream: Permission denied

    I tried running the following commands and got Permisions denied in the terminal:

    php artisan cache:clear
    php artisan config:clear
    php artisan config:cache
    php artisan optimize:clear
    

    I ran chmod -R 775 storage/logs/ and composer dump-autoload and I was able to get onto the home page of my site without any errors. After surfing around the site a bit more I was getting the same error in various areas and not in others:

    Again same error

    The stream or file "/var/app/current/storage/logs/laravel-2019-11-22.log" could not be opened: failed to open stream: Permission denied

    I deleted the following files and ran php artisan cahce:clear:

    /bootstrap/cache/packages.php
    /bootstrap/cache/services.php
    /bootstrap/cache/config.php.php
    

    The only other advice I've seen is run:

    sudo chmod -R 777 storage/*
    

    Which seems like a bad idea on a production server, but seems to be upvoted answer. Should I just give my storage director 777 permissions and why? Is there another way of fixing this?

    Edit:

    I'm trying to do this as stated here:

    // set current user as owner and the webserver user as the group
    sudo chown -R $USER:www-data storage
    sudo chown -R $USER:www-data bootstrap/cache
    // set directory permission to be 775
    chmod -R 775 storage
    chmod -R 775 bootstrap/cache
    

    but when I run sudo chown -R $USER:www-data storage I get this error:

    chown: invalid group: ‘myusername:www-data’

  • Lemmings19
    Lemmings19 about 3 years
    running ps aux and seeing what users was runnig php-fpm and changing the user to that worked for me... for some reason the user was www-data.