在 CentOS 上运行 Laravel 报错,原因通常集中在环境配置 / 权限 / PHP 版本 / 依赖这几类。下面我按「最常见原因 → 排查方法 → 解决方案」给你一个完整清单。
Laravel 对 PHP 版本要求很严格:
| Laravel | PHP 最低版本 |
|---|---|
| 8.x | 7.3 |
| 9.x | 8.0 |
| 10.x | 8.1 |
| 11.x | 8.2 |
✅ 检查:
php -v
❌ 常见报错:
Your requirements could not be resolved to an installable set of packages.
✅ 解决:
yum install php81 php81-php-cli
# 或使用 remi 源
Laravel 必须扩展:
mbstringopensslpdotokenizerxmlctypejsonbcmath(财务类必装)✅ 检查:
php -m
✅ 安装:
yum install php-mbstring php-xml php-bcmath php-json
报错示例:
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 storage bootstrap/cache
或开发环境:
chmod -R 777 storage
很多人忽略了这个!
✅ 检查:
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
报错:
No application encryption key has been specified.
✅ 解决:
cp .env.example .env
php artisan key:generate
✅ 检查:
composer -V
✅ 安装:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
mv composer.phar /usr/local/bin/composer
File not found.
✅ 正确 root:
root /var/www/laravel/public;
✅ 重写规则:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
php -v
php -m | grep -E "mbstring|openssl|pdo|tokenizer|xml"
ls -la storage
getenforce
你可以直接贴:
cat /etc/redhat-release)我可以直接告诉你 是哪一行导致的、怎么改。