laravel运行必需storage和bootstrap/cache可写,需执行sudo chown -r $user:www-data storage bootstrap/cache与sudo chmod -r 775 storage bootstrap/cache,并验证touch test.txt;同时nginx须拦截.env、创建storage符号链接、关闭app_debug并清空缓存。

确认storage和bootstrap/cache目录可写
这两个目录是Laravel运行时写入日志、缓存、编译视图、会话文件的必经路径,权限错误将直接导致500错误或页面空白。
执行命令设置目录所有权与权限:sudo chown -R $USER:www-data storage bootstrap/cache → sudo chmod -R 775 storage bootstrap/cache
【必须用www-data作为组名,不能用nginx或其他用户组,否则PHP-FPM无法写入】
验证是否生效:touch storage/test.txt && rm storage/test.txt,若报错Permission denied则需重查chown与chmod顺序。
禁止.env文件被Web直接访问
Nginx配置中必须显式拦截.env请求,否则攻击者可通过https://yoursite.com/.env直接下载全部数据库密码与API密钥。
在站点对应的Nginx配置文件server块内添加:location ~ /\.env { deny all; }
重载配置生效:sudo nginx -t && sudo systemctl reload nginx
创建public可访问的上传符号链接
用户上传的头像、附件等应存于storage/app/public/下,但需通过/public/storage路径对外提供服务——这依赖符号链接打通路径。
第一步:确保storage/app/public目录已存在(如无则手动创建)
第二步:在项目根目录执行php artisan storage:link
第三步:检查链接是否成功:ls -l public/storage,输出应为public/storage -> ../storage/app/public
【若提示“The link already exists.”却无法访问上传文件,请先rm public/storage再重试】
设置APP_DEBUG=false并清空所有缓存
APP_DEBUG=true在生产环境会暴露完整异常堆栈、环境变量、SQL语句,属于高危配置。
方法一:直接编辑.env,确认含APP_ENV=production和APP_DEBUG=false两行
方法二:执行强制清除命令链:php artisan config:clear → php artisan cache:clear → php artisan view:clear → php artisan route:clear
注意:config:cache仅在APP_DEBUG=false后才允许执行,否则报错终止。











