在 CentOS 下运行 Laravel 报错的原因有很多,不能只凭“报错”两个字定位问题。下面我按最常见的原因 + 现象 + 解决办法给你系统梳理一下,你可以对照看看是哪一种。
The stream or file "/storage/logs/laravel.log" could not be openedPermission deniedCentOS 默认使用 apache 或 nginx 用户,而目录属主可能是 root。
chown -R nginx:nginx /path/to/laravel
# 或 apache
chown -R apache:apache /path/to/laravel
chmod -R 755 storage bootstrap/cache
⚠️ 不要用 777,不安全。
Class 'XXX' not foundLaravel 新版需要 PHP 8.1+
php -v
CentOS 默认 PHP 可能是 7.2/7.4,需要:
yum install epel-release
yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install php81 php81-php-fpm php81-php-mysqlnd
Class 'PDO' not foundmbstring not loadedphp -m | grep -E "mbstring|pdo|openssl|tokenizer"
缺什么装什么:
yum install php-mbstring php-pdo php-openssl php-tokenizer
getenforce
setenforce 0
setsebool -P httpd_read_user_content 1
setsebool -P httpd_unified 1
Vendor autoload not foundClass not foundcomposer install
composer dump-autoload
File not foundNo input file specifiedroot /path/to/laravel/public;
key not foundcp .env.example .env
php artisan key:generate
php artisan storage:link
tail -f storage/logs/laravel.log
或打开调试:
APP_DEBUG=true
如果你愿意,把具体报错贴出来,我可以直接告诉你怎么改。