Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel cannot write its log file or the storage folder

ErrorThe stream or file "/var/www/app/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied

Short 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.

  • Applies to Laravel 12, PHP-FPM 8.3, Ubuntu 24.04, Rocky Linux 9 (SELinux)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 writable
  2. 2Match what you see, then jump to its fix

    1. 1ls 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. 2The 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. 3The 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. 4Modes 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

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 denied
  • The stream or file "/var/www/app/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied
  • file_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.log

2Fix

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/cache

3Verify

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 ok

Confirmed by laravel.com

Did this fix it?

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/logs

2Fix

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:work

3Verify

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/logs

Confirmed by laravel.com

Did this fix it?

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 20

2Fix

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/cache

3Verify

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.log

Confirmed by man7.org

Did this fix it?

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 recent

2Fix

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/cache

3Verify

The label shows httpd_sys_rw_content_t, and the page logs without errors.

ls -Zd storage storage/logs

Confirmed by man7.org

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.