在 Linux 上运行 Laravel 报权限错误,绝大多数情况都和文件/目录权限或 PHP 运行用户有关。下面按「最常见原因 → 排查方法 → 解决方式」给你说明。
storage 和 bootstrap/cache 目录没写权限Laravel 需要写入:
storage/bootstrap/cache/常见报错:
The stream or file "/var/www/laravel/storage/logs/laravel.log" could not be opened
Permission denied
✅ 解决:
chmod -R 755 storage bootstrap/cache
chown -R www-data:www-data storage bootstrap/cache
www-data是 Nginx/Apache 默认用户(Ubuntu)
比如:
ubuntu 用户上传代码www-data 运行就会导致“你看得到,PHP 写不了”
✅ 查看 PHP 用户:
ps aux | grep php
✅ 统一用户(推荐):
chown -R www-data:www-data /var/www/laravel
如果你用 CentOS,SELinux 会悄悄拦截写权限。
报错不明显,但就是写不进去。
✅ 临时测试:
setenforce 0
✅ 永久解决:
setsebool -P httpd_unified 1
.env 文件不存在或不可读报错:
No application encryption key has been specified
✅ 解决:
cp .env.example .env
php artisan key:generate
chmod 644 .env
执行 artisan 报权限错误:
php artisan config:cache
✅ 清缓存:
php artisan cache:clear
php artisan config:clear
# 项目根目录
chown -R www-data:www-data /var/www/laravel
# 目录权限
find /var/www/laravel -type d -exec chmod 755 {} \;
# 文件权限
find /var/www/laravel -type f -exec chmod 644 {} \;
# 可写目录
chmod -R 775 storage bootstrap/cache
storage/logs 可写bootstrap/cache 可写.env 存在你可以把具体报错信息贴出来(哪怕一行),我可以精确告诉你是哪一类权限问题,不用瞎试。