public目录只需755权限,因其仅被web服务器读取而不写入;设777会引发严重安全风险,如恶意脚本写入shell文件。storage和bootstrap/cache必须归属web服务器用户(如nginx)并设775权限,必要时还需配置selinux上下文。

public 目录本身不需要写权限,755 就够了;真正要设写权限的是 storage 和 bootstrap/cache,且必须归属 Web 服务器用户(如 nginx 或 apache)。
为什么 public 目录不能设 777
public 是 Web 服务器读取静态资源(index.php、CSS、JS、图片)的入口目录,它只被读取,不被 Laravel 写入。设成 777 会带来明显安全风险:任何能执行 PHP 的恶意脚本都可能往里面写 shell 文件。
-
public目录权限应为755(所有者可读写执行,组和其他人仅读执行) - 里面所有文件(如
index.php)建议是644 - 若用
php artisan storage:link创建了public/storage符号链接,该链接本身权限无关紧要,关键是它指向的storage/app/public目录是否可被 Web 服务器读取
storage 和 bootstrap/cache 权限怎么配才对
这两个目录是 Laravel 运行时写日志、缓存、上传文件、编译视图的地方,必须让 Web 服务器进程能写入,但又不能开放给所有人随意修改。
- 先确认 Web 服务器用户:Nginx 通常是
nginx,Apache 在 CentOS 是apache,Ubuntu/Debian 多为www-data - 执行:
sudo chown -R nginx:nginx /path/to/laravel/storage<br>sudo chown -R nginx:nginx /path/to/laravel/bootstrap/cache
- 再设权限:
sudo chmod -R 775 /path/to/laravel/storage<br>sudo chmod -R 775 /path/to/laravel/bootstrap/cache
- 如果系统启用了 SELinux(常见于 CentOS/RHEL),还得补一句:
sudo chcon -R -t httpd_sys_rw_content_t /path/to/laravel/storage<br>sudo chcon -R -t httpd_sys_rw_content_t /path/to/laravel/bootstrap/cache
遇到 “No input file specified” 怎么办
这通常不是权限问题,而是 Web 服务器没正确把请求转发给 public/index.php,尤其在共享主机或非标准 Nginx/Apache 配置下。
- 检查 Web 根目录是否真的指向
laravel/public(不是项目根目录) - Nginx 用户:确认
root指令指向/path/to/laravel/public,且try_files $uri $uri/ /index.php?$query_string;存在 - Apache 用户:确保
.htaccess在public目录下且生效(AllowOverride All已开启);若仍报错,可临时用这个最小化规则:RewriteEngine On<br>RewriteCond %{REQUEST_FILENAME} !-f<br>RewriteCond %{REQUEST_FILENAME} !-d<br>RewriteRule ^ index.php [L] - PHP-FPM 用户:确认
security.limit_extensions包含.php,且doc_root未错误限制
最容易被忽略的一点:改完权限后,一定要重启 Web 服务(sudo systemctl restart nginx 或 sudo systemctl restart httpd),否则变更不生效;SELinux 上下文修改也必须配合重启或 restorecon 才能彻底落地。











