Maaz Zindani

Loading the index…

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

Laravel cannot find or create its storage folders (sessions, logs)

Errorfile_put_contents(/var/www/app/storage/framework/sessions/Zk3...): Failed to open stream: No such file or directory

Short answer

Laravel writes sessions, views, cache and logs into subfolders of storage. If one of them is missing, or the web user may not create it, you get file_put_contents failed to open stream: No such file or directory, or Monolog's 'no existing directory' error. Recreate the folders, make storage writable, or move sessions off disk.

  • Applies to Laravel 12, PHP 8.3, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which of the stock storage folders exist and who owns them.

    ls -ld storage storage/framework/* storage/logs
  2. 2Match what you see, then jump to its fix

    1. 1ls reports No such file or directory for storage/framework/sessions, cache, views or storage/logs.A stock folder is missingFix
    2. 2The folders exist, but the error still says it could not be created, or sessions never appear.The web server user may not write to storageFix
    3. 3SESSION_DRIVER=file in .env, on a host with a read-only or shared disk.Sessions do not have to live on diskFix

What does the error look like?

  • file_put_contents(/var/www/app/storage/framework/sessions/Zk3...): Failed to open stream: No such file or directory
  • There is no existing directory at "/var/www/app/storage/logs" and it could not be created:

Cause 1 of 3

A stock folder is missing

1Check

A fresh Laravel project has storage/framework with the folders cache, sessions, testing and views, plus storage/app and storage/logs. A folder that did not come across when the project was copied or deployed is gone.

ls -d storage/app storage/logs storage/framework/cache storage/framework/sessions storage/framework/views

2Fix

Recreate the missing folders.

mkdir -p storage/app storage/logs
mkdir -p storage/framework/cache storage/framework/sessions storage/framework/testing storage/framework/views

3Verify

Load a page that starts a session, then list the sessions folder.

ls storage/framework/sessions | head

Confirmed by laravel.com

Did this fix it?

Cause 2 of 3

The web server user may not write to storage

1Check

Laravel needs write access to storage and bootstrap/cache for the user that runs the web server.

ps -o user= -C php-fpm8.3 | sort -u && ls -ld storage bootstrap/cache

2Fix

Give that user write access to both folders. On Debian and Ubuntu the user is usually www-data.

sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R ug+rwX storage bootstrap/cache

3Verify

Create a test file as the web user.

sudo -u www-data touch storage/framework/sessions/.write-test && echo ok

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

Sessions do not have to live on disk

1Check

The file driver stores sessions in storage/framework/sessions. Other drivers do not touch that folder.

grep SESSION_DRIVER .env

2Fix

Switch to the database driver, create the sessions table, and clear the cached config.

# .env
SESSION_DRIVER=database

php artisan make:session-table
php artisan migrate
php artisan config:clear

3Verify

Log in, then count rows in the sessions table.

php artisan tinker --execute="echo DB::table('sessions')->count();"

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Run mkdir -p for the storage folders in your deploy script, before the first request.
  • Run artisan commands as the same user as the web server, or fix ownership at the end of the deploy.
  • On servers with a read-only app folder, send sessions and logs to a database or stderr instead of files.

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

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: failed to open stream: Permission denied

The user that runs PHP (php-fpm or Apache, often www-data) cannot write to storage or bootstrap/cache.

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