在 CentOS 上运行 Laravel 报错 的原因非常多,通常会和 环境配置、权限、PHP 版本、依赖、SELinux 等有关。下面我按最常见原因 + 排查方法给你一份清单,你可以对照看。
Laravel 对 PHP 版本要求较新:
| Laravel | PHP |
|---|---|
| 10 | ≥ 8.1 |
| 11 | ≥ 8.2 |
检查:
php -v
常见错误:
Your requirements could not be resolved to an installable set of packages.
解决:
yum install centos-release-scl
yum install php81 php81-php-cli
Laravel 需要写:
storagebootstrap/cache错误示例:
The stream or file "/var/www/laravel/storage/logs/laravel.log" could not be opened
解决:
chown -R nginx:nginx /var/www/laravel
chmod -R 755 /var/www/laravel
chmod -R 777 storage bootstrap/cache
典型报错:
Permission denied
快速验证:
setenforce 0
如果好了,就是 SELinux 问题。
正确解决(不关 SELinux):
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/laravel/storage(/.*)?"
restorecon -Rv /var/www/laravel
Laravel 必需扩展:
bcmath
ctype
json
mbstring
openssl
pdo
tokenizer
xml
检查:
php -m
安装:
yum install php-mbstring php-xml php-bcmath
典型错误:
404 / 500 / No input file specified
Nginx 示例(必须):
location / {
try_files $uri $uri/ /index.php?$query_string;
}
报错:
Class 'App\Http\Controllers\Controller' not found
解决:
composer install
composer dump-autoload
tail -f storage/logs/laravel.log
.env
APP_DEBUG=true
# Nginx
tail -f /var/log/nginx/error.log
# Apache
tail -f /var/log/httpd/error_log
你可以直接把 报错内容贴出来(完整的,不用删),例如:
laravel.logphp artisan 报错我可以一步一步告诉你改哪里、怎么改。
你现在的 Laravel 版本、PHP 版本、Web 服务(nginx/apache)是什么?