Laravel 在 CentOS 上报错的原因通常和环境、权限、PHP 版本或配置有关。下面我按最常见原因 + 排查方法给你梳理,基本能覆盖 90% 的情况。
Laravel 对 PHP 版本要求很严格:
| Laravel 版本 | PHP 最低版本 |
|---|---|
| Laravel 8 | 7.3 |
| Laravel 9 | 8.0 |
| Laravel 10 | 8.1 |
| Laravel 11 | 8.2 |
✅ 检查:
php -v
❌ CentOS 默认仓库 PHP 很低(如 5.4 / 7.2)
✅ 解决:
yum install epel-release
yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php82
yum install php php-cli php-fpm php-mbstring php-xml php-json php-zip
Laravel 需要写:
storagebootstrap/cache❌ 报错示例:
The stream or file "storage/logs/laravel.log" could not be opened
✅ 解决:
chown -R nginx:nginx /var/www/laravel
chmod -R 755 storage bootstrap/cache
如果你用 Apache:
chown -R apache:apache /var/www/laravel
SELinux 会阻止 web 服务写文件
✅ 检查:
getenforce
如果是 Enforcing
✅ 临时关闭测试:
setenforce 0
✅ 永久允许(推荐):
setsebool -P httpd_unified 1
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/laravel/storage(/.*)?"
restorecon -Rv /var/www/laravel
Laravel 必需扩展:
✅ 检查:
php -m
✅ 安装:
yum install php-mbstring php-xml php-json php-zip
❌ 报错:
composer: command not found
✅ 安装:
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
安装依赖:
composer install --no-dev --optimize-autoloader
常见错误:
✅ Nginx 示例:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
.env✅ 解决:
cp .env.example .env
php artisan key:generate
tail -f storage/logs/laravel.log
或打开调试:
APP_DEBUG=true
你可以把 具体报错内容 贴出来,比如:
我可以帮你 逐条分析 + 给出 CentOS 精确命令。