温馨提示×

linux laravel如何设权限

小樊
51
2026-09-25 11:41:50
栏目: 智能运维

在 Linux + Laravel 项目中,权限设置主要涉及两方面:

  1. Linux 文件/目录权限
  2. Laravel 运行用户与可写目录

下面按常见场景说明。


一、Linux 文件/目录权限(基础)

1️⃣ 推荐目录权限

通常:

# 项目根目录
chown -R www-data:www-data /var/www/laravel
chmod -R 755 /var/www/laravel

www-data 是 Nginx / Apache 默认用户(Ubuntu/Debian)
CentOS 通常是 nginx 或 apache

2️⃣ 必须可写的目录

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

二、使用当前用户 + Web 用户(推荐做法)

如果你是用 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

三、Nginx / Apache 权限问题排查

Nginx 用户

ps aux | grep nginx

常见:

  • www-data(Ubuntu)
  • nginx(CentOS)

Apache 用户

ps aux | grep apache

四、SELinux(CentOS 常见)

如果权限正确但仍 403:

getenforce

临时关闭测试:

setenforce 0

永久解决(推荐):

chcon -R -t httpd_sys_rw_content_t storage
chcon -R -t httpd_sys_rw_content_t bootstrap/cache

五、Laravel 官方推荐权限(总结)

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 无权限

如果你愿意,可以告诉我:

  • ✅ 系统(Ubuntu / CentOS)
  • ✅ Web 服务(Nginx / Apache)
  • ✅ 报错信息

我可以按你的环境给你精确命令。

0 踩