在 CentOS 上运行 Laravel 报错 是很常见的现象,但错误原因很多,需要看具体报错信息才能准确判断。下面我先给你总结 CentOS 上运行 Laravel 最常见的几类原因和排查方法,你可以对照看看。
Laravel 对 PHP 版本要求严格:
| Laravel | PHP |
|---|---|
| 8.x | ≥ 8.0 |
| 9.x | ≥ 8.0 |
| 10.x | ≥ 8.1 |
| 11.x | ≥ 8.2 |
CentOS 默认 PHP 通常是 7.2 / 7.4
检查:
php -v
✅ 解决:
yum install -y epel-release
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php82
yum install php php-cli php-fpm php-mysqlnd php-mbstring php-xml php-json
Laravel 必须依赖的扩展:
mbstringxmlcurljsontokenizerfileinfoopenssl检查:
php -m | grep -E 'mbstring|xml|curl|json'
安装:
yum install php-mbstring php-xml php-curl
报错类似:
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
报错经常“奇怪”,但其实是 SELinux 拦了。
临时关闭测试:
setenforce 0
如果正常了:
setsebool -P httpd_read_user_content 1
setsebool -P httpd_unified 1
Laravel 必须 重写到 public/index.php
✅ Nginx 示例:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
cp .env.example .env
composer install
php artisan key:generate
php artisan serve
或
cat storage/logs/laravel.log
.env
APP_DEBUG=true
请把以下信息发我:
cat /etc/redhat-release)php -v)php artisan --version)我可以直接帮你 一步步修到能跑。