在 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 服务
nginx 或 php-fpm 的用户(通常是 nginx / apache / www)ps aux | grep php-fpm
ps aux | grep nginx
假设:
/var/www/laravelnginxchown -R deploy:deploy /var/www/laravel
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
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 不要用 root 跑!
sudo -u deploy composer install
或:
composer install --no-root
❌ 仅用于测试环境:
chmod -R 777 /var/www/laravel
CentOS 上装 Laravel:项目归你,storage/bootstrap/cache 给 Web 用户,SELinux 别忘了关或放行。
如果你告诉我:
我可以给你一套精确到命令的权限配置。