在 Linux + Laravel 项目中,权限设置主要涉及 目录/文件权限、拥有者(owner)、以及 storage / bootstrap-cache 可写 等几个方面。下面给你一个实用、安全、常见的权限配置方案。
Laravel 以下目录必须 Web 服务用户可写:
storage/
bootstrap/cache/
常见需写操作:
storage/logsstorage/framework/cachestorage/framework/sessionsstorage/framework/views不同环境用户不同:
| 环境 | 用户 |
|---|---|
| Apache | www-data |
| Nginx + PHP-FPM | www-data 或 php-fpm |
| 本地开发 | 当前用户 |
查看 PHP 运行用户:
ps aux | grep php-fpm
假设 Web 用户是 www-data,你的用户名是 ubuntu:
sudo chown -R ubuntu:www-data /var/www/laravel
含义:
sudo find /var/www/laravel -type d -exec chmod 2755 {} \;
sudo find /var/www/laravel -type f -exec chmod 644 {} \;
说明:
2755:目录,组可写644:文件,安全sudo chmod -R 775 storage bootstrap/cache
sudo chown -R ubuntu:www-data storage bootstrap/cache
sudo chmod -R 777 storage
❌ 安全风险大,仅本地测试可用
如果是 CentOS 并启用 SELinux:
sudo setsebool -P httpd_read_user_content 1
sudo chcon -R -t httpd_sys_rw_content_t storage
The stream or file could not be opened原因:日志目录无写权限
解决:
sudo chmod -R 775 storage/logs
777php artisan cache:clear 等命令时用 sudo -u www-data如果你愿意,可以告诉我:
我可以给你精确命令。