生产环境Laravel需严格设置storage和bootstrap/cache为775权限并归属Web用户组(如www),.env设600,启用SELinux时须配置httpd_sys_rw_content_t上下文。

直接给 storage 和 bootstrap/cache 目录写权限,不是 chmod 777 就完事——那等于把日志、缓存、session 全部暴露给任意进程,生产环境必须避免。
Web 服务器用户组必须匹配项目所有者
宝塔默认用 www 用户跑 Nginx/PHP,CentOS Apache 默认是 apache,Ubuntu Nginx 默认是 www-data。不统一用户,chown 就白设。
- 先确认 Web 服务运行用户:
ps aux | grep -E '(nginx|httpd|apache2)'或看宝塔面板「网站」→「设置」→「配置文件」里user行 - 再统一归属:
sudo chown -R www:www /www/wwwroot/your_project(宝塔)或sudo chown -R apache:apache /var/www/html/your_project(CentOS Apache) - 别漏掉软链接目标目录:如果
public/storage是指向storage/app/public的软链,storage/app/public本身也要被www组可写
chmod 要分类型、分目录,不能一刀切
755 和 644 是读执行/读的底线,但 storage 和 bootstrap/cache 必须允许组写入(775),否则日志写不进、缓存生成失败、php artisan config:cache 报错。
Laravel 13.2.0 是基于 PHP 8.3+ 的高性能框架,官方推荐通过 Composer 安装。它内置 AI SDK、JSON:API Resources 及原生向量搜索,支持属性驱动开发与队列路由,大幅提升开发效率。相比旧版,13.2.0 优化了缓存 TTL 管理与实时通信,无需 Redis 即可横向扩展。作为现代 Web 开发首选,它兼顾安全与极速体验,助您快速构建企业级应用。
- 通用目录(
app、config、routes等):find /path -type d -exec chmod 755 {} \; - 通用文件(
.php、.env、.gitignore):find /path -type f -exec chmod 644 {} \; - 关键可写目录:
chmod -R 775 storage bootstrap/cache -
.env文件必须严格限制:chmod 600 .env,防止被 web 访问到
SELinux 启用时,chmod 正确也照样报 Permission denied
CentOS/RHEL 默认开启 SELinux,它会拦截 Web 进程对某些路径的写操作,哪怕 ls -l 看权限完全正确。错误现象典型如:The stream or file "/var/www/project/storage/logs/laravel.log" could not be opened in append mode: failed to open stream: Permission denied。
- 检查是否启用:
sestatus,输出enabled就要处理 - 临时放行(仅调试):
sudo setenforce 0,如果页面立刻正常,说明就是 SELinux 拦截 - 永久修复(推荐):
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/project/storage(/.*)?",然后sudo restorecon -Rv /var/www/project/storage - 同理处理
bootstrap/cache:sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/project/bootstrap/cache(/.*)?"
最常被忽略的是 .env 权限和 SELinux 上下文——前者泄露密钥,后者让所有 chmod 都失效。部署后第一件事不是刷新页面,而是 ls -l .env 和 sestatus 各看一眼。

















