在 CentOS 上部署 Laravel 时,权限问题主要集中在 storage、bootstrap/cache 目录以及 PHP 运行用户(nginx/php-fpm) 的权限配置。下面是一套标准、安全的做法。
通常:
nginx 或 php-fpmapacheps aux | grep php-fpm
ps aux | grep nginx
一般 CentOS 下 PHP-FPM 用户是:
www-data # Ubuntu
apache # CentOS Apache
nginx # CentOS Nginx(常见)
假设:
/var/www/laravelnginxchown -R nginx:nginx /var/www/laravel
如果是开发环境,也可以是你自己 + web 用户:
chown -R youruser:nginx /var/www/laravel
# 目录 755
find /var/www/laravel -type d -exec chmod 755 {} \;
# 文件 644
find /var/www/laravel -type f -exec chmod 644 {} \;
chmod -R 775 storage
chmod -R 775 bootstrap/cache
并确保属主是 web 用户:
chown -R nginx:nginx storage bootstrap/cache
CentOS 默认开启 SELinux,否则会 403 / 500。
getenforce
chcon -R -t httpd_sys_content_t /var/www/laravel
chcon -R -t httpd_sys_rw_content_t /var/www/laravel/storage
chcon -R -t httpd_sys_rw_content_t /var/www/laravel/bootstrap/cache
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/laravel/storage(/.*)?"
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/laravel/bootstrap/cache(/.*)?"
restorecon -Rv /var/www/laravel
server {
listen 80;
server_name example.com;
root /var/www/laravel/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
storage/logs/laravel.log Permission denied✅ storage 没写权限 / SELinux 没开
✅ 检查 bootstrap/cache
The stream or file could not be opened✅ 权限 + SELinux 双重检查
chmod -R 777 storage bootstrap/cache
⚠️ 仅测试用,生产环境禁止
如果你愿意,可以告诉我:
我可以给你完全对应的命令。