Laravel: laravel.log could not be opened, permission denied in storage
Fix "The stream or file storage/logs/laravel.log could not be opened: Permission denied": give the web user write access to storage and bootstrap/cache.
Laravel writes logs, cache and sessions into the storage and bootstrap/cache folders. If the user that runs PHP cannot write there, you get a permission error or a blank 500 page. Give that user ownership of those two folders.
What to do, step by step
- Find the user that runs PHP: ps aux | grep php-fpm or apache2. On Ubuntu it is usually www-data.
- Give that user the two folders (commands below).
- Make the folders writable for the owner and group.
- Clear old cached files: php artisan optimize:clear.
- Run artisan commands as the same user, such as sudo -u www-data php artisan cache:clear, so new files are not owned by root.
Commands you can copy
sudo chown -R www-data:www-data storage bootstrap/cachesudo chmod -R ug+rwx storage bootstrap/cachephp artisan optimize:clearsudo chcon -R -t httpd_sys_rw_content_t storage bootstrap/cacheOther causes to check
- It broke again after running artisan as root
- A command run as root created root-owned log or cache files. Run the chown command again, and avoid sudo with artisan.
- SELinux is enabled (Fedora, CentOS, Rocky)
- Mode bits are not enough. Allow writing with sudo chcon -R -t httpd_sys_rw_content_t storage bootstrap/cache.
- Docker container
- The user inside the container may not match the owner on your computer. Run chown from inside the container, or run it as your own user ID.
- No log is written but the page is blank
- Check that APP_DEBUG and the log path are right in .env, and read the web server error log for the real error.
- Please provide a valid cache path
- The storage/framework folders are missing. Create storage/framework/cache, sessions and views, then fix permissions.
Questions about Laravel permission denied
- Should I use chmod 777?
- No. It lets every user on the server write to those folders. Give the web user ownership and use 775 instead.
- Which folders need write access?
- storage and bootstrap/cache, including everything inside them. The rest of the project can stay read-only for the web user.
- Why is the folder fine but the new log still fails?
- The daily log file is created by whichever user writes first. If that was root, the web user cannot append to it. Delete the file or fix its owner.
Config generators for this
- php.ini settingsSet the PHP values most sites need: upload size, memory, run time, errors, time zone and OPcache.