Few Laravel errors are as common - and as repeatedly mis-fixed - as file_put_contents(): failed to open stream: Permission denied pointing at a file in storage/ or bootstrap/cache. The internet's most copied answer, chmod -R 777 storage, makes the error go away by making every file writable by every process on the machine. It also guarantees the problem returns on the next deploy, because the real issue is ownership, not permission bits.
Why the error happens
Laravel writes constantly to storage/logs, storage/framework/{cache,sessions,views} and bootstrap/cache. On a typical server, PHP-FPM (and the queue worker, and the scheduler cron) run as one user - often www-data - while you deploy and run artisan commands as another, such as ubuntu or deploy. The moment your deploy user runs php artisan config:cache or a migration that touches the cache, the files it creates belong to your user. PHP-FPM can no longer write to them, and the next web request throws Permission denied.
Check who owns what
ls -la storage/logs | head
# -rw-r--r-- 1 ubuntu ubuntu laravel.log <- created by your deploy user
# PHP-FPM runs as www-data and cannot append to this file
If the owner of files inside storage/ is your SSH or deploy user instead of the PHP-FPM user, you have found the cause.
The fix that survives deployments
Give the web user group ownership, keep your deploy user in that group, and set the setgid bit so new files inherit the group:
sudo chown -R $USER:www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
sudo find storage bootstrap/cache -type d -exec chmod g+s {} \;
The setgid bit on directories is the part people miss: it makes every new file created inside them belong to the www-data group automatically, no matter which user created it. With group write in place, both your deploy user and PHP-FPM can write, today and after every future deployment.
Do not forget the other writers
Queue workers and the Laravel scheduler usually run under yet another context (Supervisor or cron). Make sure those services also run as a user in the www-data group, or as www-data itself:
# /etc/supervisor/conf.d/laravel-worker.conf
[program:laravel-worker]
user=www-data
...
If the worker writes a log or cache file as root or as your deploy user without the group setup, the same error returns through the web request that touches the file next.
One exception: ephemeral containers
If you deploy into Docker containers where storage is ephemeral or mounted, prefer fixing ownership in the image or entrypoint and keep storage/ on a named volume. The chmod 777 habit is especially dangerous here because it tends to leak into the Dockerfile and then into production images.
Summary
Permission denied on storage/ means the process writing the file is not the process that owns it. Fix group ownership, add the setgid bit so new files inherit it, and align your web server, queue workers and cron to the same group. Your deployments will stop breaking permissions - no 777 required.