在 Linux + Laravel 项目中,权限设置主要涉及两方面:
下面按常见场景说明。
通常:
# 项目根目录
chown -R www-data:www-data /var/www/laravel
chmod -R 755 /var/www/laravel
www-data是 Nginx / Apache 默认用户(Ubuntu/Debian)
CentOS 通常是nginx或apache
Laravel 这些目录需要 写权限:
storage
bootstrap/cache
设置示例:
chmod -R 775 storage
chmod -R 775 bootstrap/cache
如果仍报错:
chown -R www-data:www-data storage
chown -R www-data:www-data bootstrap/cache
如果你是用 deploy 用户 部署,而 Web 是 www-data:
sudo chown -R deploy:www-data /var/www/laravel
sudo chmod -R 775 /var/www/laravel
sudo chmod -R 775 storage bootstrap/cache
并把你当前用户加入 www-data 组:
sudo usermod -aG www-data deploy
ps aux | grep nginx
常见:
www-data(Ubuntu)nginx(CentOS)ps aux | grep apache
如果权限正确但仍 403:
getenforce
临时关闭测试:
setenforce 0
永久解决(推荐):
chcon -R -t httpd_sys_rw_content_t storage
chcon -R -t httpd_sys_rw_content_t bootstrap/cache
chown -R www-data:www-data .
chmod -R 755 .
chmod -R 775 storage bootstrap/cache
| 错误 | 原因 |
|---|---|
Permission denied |
storage 无写权限 |
| 500 错误 | bootstrap/cache 不可写 |
| 上传失败 | storage/app 无权限 |
| 日志不生成 | storage/logs 无权限 |
如果你愿意,可以告诉我:
我可以按你的环境给你精确命令。