Laravel cannot write its log file or the storage folder
The stream or file "/var/www/app/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission deniedShort answer
The user that runs PHP (php-fpm or Apache, often www-data) cannot write to storage or bootstrap/cache. Find that user, give it write access to both folders, make sure a log file created by artisan or cron as another user does not lock it out, and on SELinux systems give the folders a writable web label.
Fix it fast
1Find out which cause you have
Find out which user PHP runs as, then see who owns storage and the log file and what that user may do there. The test at the end is the real answer: it tries to write as the web user.
ps -eo user,comm | grep -E 'php-fpm|apache2|httpd' | sort -u ls -ld storage storage/logs bootstrap/cache ls -l storage/logs sudo -u www-data test -w storage/logs && echo writable || echo NOT writable2Match what you see, then jump to its fix
- 1
ls shows root or your deploy user as owner with mode 755, and the test prints NOT writableThe web user does not own storage and cannot write to itFix - 2
The folder is writable, but storage/logs/laravel.log (or today's daily file) is owned by root or the deploy user with mode 644laravel.log was created by another user and is not group-writableFix - 3
The fix above works until the next deploy, queue worker or cron run creates new filesNew files keep appearing with the wrong owner: use ACLsFix - 4
Modes and owners look correct, getenforce prints Enforcing, and ls -Z shows a label that is not httpd_sys_rw_content_tSELinux blocks the web server although the modes are rightFix
- 1
What does the error look like?
The stream or file "/var/www/app/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission deniedThe stream or file "/var/www/app/storage/logs/laravel.log" could not be opened: failed to open stream: Permission deniedfile_put_contents(/var/www/app/storage/framework/views/....php): Failed to open stream: Permission denied
Cause 1 of 4
The web user does not own storage and cannot write to it
1Check
Laravel writes logs, compiled views, cache and sessions into storage and bootstrap/cache, so the process owner of the web server must be allowed to write to them. A fresh checkout is owned by whoever ran git or composer.
namei -l storage/logs/laravel.log2Fix
Give the web user's group ownership of the two folders and group write permission. Capital X adds the execute bit to folders only, so files do not become executable. Keep your deploy user as owner so deploys still work.
sudo chown -R "$USER":www-data storage bootstrap/cache
sudo chmod -R ug+rwX storage bootstrap/cache3Verify
The test now prints writable, and the log fills up.
sudo -u www-data test -w storage/logs && echo writable
sudo -u www-data touch storage/logs/laravel.log && echo okConfirmed by laravel.com
Cause 2 of 4
laravel.log was created by another user and is not group-writable
1Check
Laravel's single and daily log channels create the file with permission 0644 unless you set otherwise. If php artisan, a cron job or a queue worker ran as another user first, that user owns the file and PHP-FPM can only read it.
ls -l storage/logs2Fix
Fix the existing files, set the file mode in the channel, and run artisan and workers as the web user from now on.
sudo chown "$USER":www-data storage/logs/*.log
sudo chmod 664 storage/logs/*.log
// config/logging.php
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'permission' => 0664,
],
sudo -u www-data php artisan queue:work3Verify
The web user can open the log file for writing, and the mode shows group write.
sudo -u www-data test -w storage/logs/laravel.log && echo writable
ls -l storage/logsConfirmed by laravel.com
Cause 3 of 4
New files keep appearing with the wrong owner: use ACLs
1Check
Ownership applies to existing files only. A default ACL on a folder, on the other hand, is inherited as the access ACL of everything created inside it later, whichever user creates it.
getfacl storage storage/logs | head -n 202Fix
Give the web user access to what exists now, and add a default entry so new files inherit it. Run setfacl once for existing files, and once with -d for the default.
sudo setfacl -R -m u:www-data:rwX storage bootstrap/cache
sudo setfacl -R -d -m u:www-data:rwX storage bootstrap/cache3Verify
A file created by your user is writable for the web user.
touch storage/logs/acl-test.log
sudo -u www-data test -w storage/logs/acl-test.log && echo writable
rm storage/logs/acl-test.logConfirmed by man7.org
Cause 4 of 4
SELinux blocks the web server although the modes are right
1Check
On Red Hat family systems SELinux decides in addition to file modes. Web server processes may write only to files labelled as writable web content. Look for denials and at the labels.
getenforce
ls -Zd storage storage/logs
sudo ausearch -m AVC -ts recent2Fix
Label the folders as writable web content in the policy, then apply it to the existing files. chcon alone is lost when the system relabels files, so record the rule with semanage first. Adjust the path to your project.
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/app/storage(/.*)?"
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/app/bootstrap/cache(/.*)?"
sudo restorecon -R -v /var/www/app/storage /var/www/app/bootstrap/cache3Verify
The label shows httpd_sys_rw_content_t, and the page logs without errors.
ls -Zd storage storage/logsConfirmed by man7.org
How do I stop it coming back?
- Never run chmod 777 on storage; it gives every user on the machine write access to your logs, sessions and cache.
- Run deploys, artisan and queue workers as the same user as PHP-FPM, or use ACLs so files created by either work for both.
- Add the permission fix to the deploy script, so a new release directory starts with the right owners.
- When files are created in a container that bind-mounts the project, match the user ids inside and outside the container.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
PHP stops the script after 30 seconds
Symfony\Component\ErrorHandler\Error\FatalError: Maximum execution time of 60 seconds exceeded
PHP ends a script that runs past max_execution_time (30 seconds by default for web requests).
5 causes, each with a fixBack end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
Why a Filament panel feels slow on a Laravel server
The panel is fast on a small demo and slow once there are many resources, pages and widgets
A slow Filament panel on a server is most often running without the production caches.
3 causes, each with a fixBack end
Intervention Image will not save the file in a Laravel app
Can't write image data to path (/var/www/app/public/uploads/photo.jpg)
Intervention Image writes straight to a file path.
3 causes, each with a fixBack end
Laravel file upload returns 413 behind nginx
413 Content Too Large
A 413 means a limit refused the request body before Laravel could use it.
3 causes, each with a fixBack end
Laravel says the bootstrap/cache directory must be present and writable
The /var/www/app/bootstrap/cache directory must be present and writable.
Laravel writes its package manifest into bootstrap/cache and throws when that folder is missing or the current user cannot write to it.
2 causes, each with a fix