温馨提示×

centos装laravel要啥权限

小樊
35
2026-09-24 02:58:11
栏目: 智能运维

在 CentOS 上部署 Laravel,主要涉及目录权限和用户权限两方面。下面按“必须知道的结论 + 详细说明”来讲。


一、核心结论(最重要)

Laravel 只需要 web 服务用户对以下目录有写权限:

storage
bootstrap/cache

通常是:

chmod -R 755 storage bootstrap/cache
chown -R nginx:nginx storage bootstrap/cache
# 或 apache:apache,看你用的 Web 服务

✅ 不要整个项目都 777 ❌ 不要用 root 跑 web 服务


二、CentOS 常见组合

1️⃣ Nginx + PHP-FPM(最常见)

  • Web 用户:nginx 或 php-fpm 的用户(通常是 nginx / apache / www)
  • 查看方式:
ps aux | grep php-fpm
ps aux | grep nginx

三、推荐的标准权限方案(安全 & 正确)

假设:

  • 项目路径:/var/www/laravel
  • Web 用户:nginx

1️⃣ 项目所有者(你部署用)

chown -R deploy:deploy /var/www/laravel

2️⃣ 给 Web 用户写权限

chown -R nginx:nginx /var/www/laravel/storage
chown -R nginx:nginx /var/www/laravel/bootstrap/cache

chmod -R 755 /var/www/laravel/storage
chmod -R 755 /var/www/laravel/bootstrap/cache

四、SELinux(CentOS 特有坑 ⚠️)

CentOS 默认开启 SELinux,很多时候不是权限问题,而是 SELinux 不让写!

查看状态

getenforce

如果是 Enforcing,要放行 Web 写权限:

setsebool -P httpd_unified 1
setsebool -P httpd_can_write 1

或针对目录:

semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/laravel/storage(/.*)?"
restorecon -Rv /var/www/laravel/storage

五、常见错误现象 & 原因

现象 原因
The stream or file could not be opened storage 没写权限
Permission denied SELinux / 用户不对
failed to open stream: Permission denied bootstrap/cache
上传文件失败 storage/app 没权限

六、Composer 安装时的权限

Composer 不要用 root 跑!

sudo -u deploy composer install

或:

composer install --no-root

七、简单粗暴但不推荐(不安全的做法)

❌ 仅用于测试环境:

chmod -R 777 /var/www/laravel

八、总结一句话

CentOS 上装 Laravel:项目归你,storage/bootstrap/cache 给 Web 用户,SELinux 别忘了关或放行。

如果你告诉我:

  • CentOS 版本
  • Nginx / Apache
  • PHP 版本
  • 是否用 Docker

我可以给你一套精确到命令的权限配置。

0 踩