在 Linux 上运行 Laravel 出现“权限错误(Permission denied)”是非常常见的问题,通常不是代码问题,而是文件/目录权限和属主配置不对。
下面按最常见原因 → 解决办法给你梳理。
Laravel 需要写入:
storage/
bootstrap/cache/
The stream or file "/var/www/laravel/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied
cd /var/www/your-project
sudo chmod -R 775 storage bootstrap/cache
sudo chown -R www-data:www-data storage bootstrap/cache
www-data是 Apache/Nginx 的运行用户(不同系统可能是nginx、apache)
ps aux | grep nginx
ps aux | grep apache
ls -l /var/www/your-project
✅ 正确情况:
deploy)www-data)sudo chown -R deploy:www-data /var/www/your-project
sudo chmod -R 775 storage bootstrap/cache
如果你是 CentOS / Rocky / Alma:
ls -Z /var/www/your-project/storage
sudo setenforce 0
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/your-project/storage(/.*)?"
sudo restorecon -Rv /var/www/your-project/storage
执行命令时报权限错:
php artisan cache:clear
sudo chown -R www-data:www-data .
sudo chmod -R 755 .
sudo chmod -R 775 storage bootstrap/cache
❌ 错误做法:
sudo php artisan storage:link
✅ 正确:
php artisan storage:link
否则 storage 下文件变成 root,web 无法写。
sudo composer install
会导致 vendor 属主为 root。
sudo chown -R deploy:www-data /var/www/your-project
cd /var/www/your-project
sudo chown -R $USER:www-data .
sudo find . -type f -exec chmod 644 {} \;
sudo find . -type d -exec chmod 755 {} \;
sudo chmod -R 775 storage bootstrap/cache
777775 + 正确属主storage / bootstrap/cache可以把完整报错信息或:
ls -l
ps aux | grep nginx
贴出来,我可以直接帮你精准定位是哪一类权限问题。