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:
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.
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
).
If you often encounter permission issues during deployment, update your deployment process to reset permissions as part of the workflow. For example:
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
php artisan cache:clear
php artisan view:clear
php artisan config:clear
php artisan config:cache
php artisan view:cache
#!/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
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.
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.
With these steps, your app should no longer experience intermittent "permission denied" errors. Let me know if you need further assistance!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community