Support the ongoing development of Laravel.io →
Cache Views
0

The error occurs because the application process (likely the web server or PHP process) does not have the correct permissions to write to the storage directory where cached files are stored. Here's how you can permanently fix this issue:


1. Ensure Correct Directory Permissions

The storage and bootstrap/cache directories must be writable by the user running the web server (e.g., www-data for Nginx/Apache on Ubuntu/Debian).

Run the following commands to fix the permissions:

# Give ownership of the directories to the web server user
sudo chown -R www-data:www-data /var/www/app-folder/storage /var/www/app-folder/bootstrap/cache

# Set directory permissions to 755 (read, write, and execute for owner; read and execute for others)
sudo chmod -R 755 /var/www/app-folder/storage /var/www/app-folder/bootstrap/cache

# Set file permissions to 644 (read and write for owner; read-only for others)
sudo find /var/www/app-folder/storage -type f -exec chmod 644 {} \;
sudo find /var/www/app-folder/bootstrap/cache -type f -exec chmod 644 {} \;

Adjust www-data to match the user your web server is running as.


2. Use Laravel's Built-In Commands to Fix Permissions

Laravel provides commands to clear and regenerate caches safely:

php artisan cache:clear
php artisan view:clear
php artisan config:clear

After clearing caches, you can pre-cache configurations and views:

php artisan config:cache
php artisan view:cache

Ensure these commands are executed by the same user as the web server (e.g., www-data).


3. Deploy Scripts and Automation

If you often encounter permission issues during deployment, update your deployment process to reset permissions as part of the workflow. For example:

Add These Steps to Your Deployment Script:

  1. Reset ownership and permissions:
    sudo chown -R www-data:www-data /var/www/app-folder/storage /var/www/app-folder/bootstrap/cache
    sudo chmod -R 755 /var/www/app-folder/storage /var/www/app-folder/bootstrap/cache
    
  2. Clear and cache Laravel configurations:
    php artisan cache:clear
    php artisan view:clear
    php artisan config:clear
    php artisan config:cache
    php artisan view:cache
    

Example Deployment Script:

#!/bin/bash

# Navigate to the application directory
cd /var/www/app-folder

# Pull latest changes from the repository
git pull origin main

# Reset permissions
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 755 storage bootstrap/cache

# Clear and cache configurations and views
php artisan cache:clear
php artisan view:clear
php artisan config:clear
php artisan config:cache
php artisan view:cache

# Restart the web server (if needed)
sudo systemctl restart php8.1-fpm

4. Persistent Solutions

a) Set the Correct User for CLI Commands

Ensure all artisan commands are executed as the same user as the web server (www-data):

sudo -u www-data php artisan cache:clear

Alternatively, use a deployment tool like Envoyer or scripts to automate this.

b) File System Issues

Ensure that your server's file system doesn't have mount options or ACLs restricting write access to the storage directory. Check with:

mount | grep /var/www

If mounted with restrictive options like noexec or nosuid, remount it with appropriate permissions.


5. Monitoring for Recurrence

  • Add a log or alert for when the error happens.
  • Use a tool like Monit to monitor the Laravel storage directory and notify you of permission changes.

With these steps, your app should no longer experience intermittent "permission denied" errors. Let me know if you need further assistance!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.