Laravel: file_put_contents() permission denied — correct storage/framework/cache permissions?

Laravel: file_put_contents() permission denied — correct storage/framework/cache permissions?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel: file_put_contents() permission denied — correct storage/framework/cache permissions?

The error file_put_contents() failed to open stream: Permission deniedinside thestorage/framework/cachedirectory is a classic symptom of a mismatch between your web server's working user and the filesystem's ownership. When you runsudo chown -R www-data:www-data storage`, it might seem like the problem is solved, but if the parent directories lack the correct group permissions or sticky bits, the write operation will still fail.

The specific error log indicates that a job is attempting to overwrite a file at /var/www/html/---/storage/framework/cache/data/3c/c7/.... The fact that saving to the same cache key works suggests a race condition or a lock file issue, but the primary blocker remains the filesystem permissions preventing the www-data user from creating new files in specific subdirectories.

Understanding the Ownership Chain

In Laravel, the application code usually runs under the www-data user (on Debian/Ubuntu) or nginx / www (on CentOS/RHEL). The storage directory and its contents must be writable by this same user. However, ownership alone (chown) is rarely sufficient. You also need to ensure that group permissions allow the web server to traverse parent directories.

If your application directory /var/www/html/---/ is owned by a different user or group than www-data, and that directory does not have world-readable/traversable permissions, the www-data process will be blocked before it even reaches the storage folder. The ls -lh output in your case shows drwsrwsr-x, which indicates the sticky bit and setgid bit are active on the data directory, but you must verify the chain upwards.

The Setgid Bit and Group Permissions

Your current setup uses a group-based permission model (www-data:www-data). This is generally preferred over world-writable directories because it isolates permissions to specific users. The setgid bit (the s in drwsrwsr-x) ensures that any new files created inside data inherit the www-data group, regardless of who creates them.

However, if you run chmod -R 775, you might be inadvertently making the entire directory tree world-accessible on some systems or failing to propagate permissions correctly if the parent directories are restrictive. A more surgical approach is often better:

  1. Ensure the web user owns the storage folder:

    sudo chown -R www-data:www-data /path/to/laravel/storage
    sudo chown -R www-data:www-data /path/to/laravel/bootstrap/cache
    
  2. Set the correct base permissions (775) for directories:

    find /path/to/laravel/storage -type d -exec chmod 775 {} \;
    
  3. Ensure files are created with 664 permissions:

    find /path/to/laravel/storage -type f -exec chmod 664 {} \;
    

The Sticky Bit Nuance

The ls -lh output shows the sticky bit (t) on the data directory. In standard Unix file systems, the sticky bit is typically applied to /tmp to prevent users from deleting each other's files. Applying it to a Laravel cache directory is unusual and can sometimes cause confusion regarding write access depending on how the web server spawns processes.

Unless you have a specific requirement for the sticky bit (such as preventing one job from deleting another's cache files in a shared environment), it is safer to remove it:

chmod -t /path/to/laravel/storage/framework/cache/data # Removes sticky bit if present
# Or simply ensure standard directory permissions are set without special bits unless necessary.

Nginx and SELinux Considerations

Since you are running on Nginx, verify that the Nginx process is indeed running as www-data (or nginx). On some distributions, Nginx runs as root by default for performance or configuration reasons, though this is rare. If your server has SELinux enabled, changing ownership to www-data might not be enough; you would need to set the correct file context:

sudo restorecon -Rv /path/to/laravel/storage

Troubleshooting the "Same Key" Behavior

You mentioned that saving to the same cache key works. This often happens because the initial write succeeds (perhaps due to a lingering socket or a brief window of permission availability), but subsequent writes fail if the file ownership changes unexpectedly or if the directory permissions are inconsistent. It can also indicate that a lock file is being created but not cleaned up properly, leading to a stale handle.

Ensure that your cache driver is configured correctly in config/cache.php. If you are using the file driver, Laravel automatically handles locking via atomic operations (flock). However, if the underlying filesystem permissions prevent the creation of the initial file, the lock mechanism cannot function, leading to intermittent errors.

Final Verification

To restore a robust permission set without relying on world-writable directories:

  1. Set ownership recursively to www-data.
  2. Apply 755 to all directories (owner can read/write/execute, group/others can read/execute).
  3. Apply 644 to existing files.
  4. Remove any sticky bits unless strictly necessary.

This configuration ensures that www-data has full control over the cache while preventing other system users from writing sensitive data. If issues persist, check the Nginx error logs (/var/log/nginx/error.log) for more granular details on whether the process is being terminated before it can write due to resource limits or stricter SELinux policies.

Our network