要明确 CentOS 上 Laravel 报错怎么解决,需要先看具体报错信息。不过我可以给你一个常见报错分类 + 排查思路 + 解决办法的通用指南,基本能覆盖 90% 的情况。
Laravel 对 PHP 版本要求严格:
| Laravel | PHP |
|---|---|
| 10 | ≥ 8.1 |
| 9 | ≥ 8.0 |
| 8 | ≥ 7.3 |
检查
php -v
解决
yum install -y php81 php81-cli php81-fpm
常见缺失扩展:
mbstring, dom, xml, curl, zip, gd, bcmath, openssl
查看
php -m
安装
yum install php-mbstring php-xml php-curl php-zip php-gd php-bcmath
The stream or file "/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
如果你用
php artisan serve,用户可能是当前用户
查看日志:
tail -f /var/log/nginx/error.log
tail -f /var/log/php-fpm/error.log
server {
listen 80;
root /var/www/laravel/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
key:generate 报错No application encryption key has been set
cp .env.example .env
php artisan key:generate
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
allow_url_fopen=Off
vi /etc/php.ini
修改:
allow_url_fopen = On
临时关闭测试
setenforce 0
永久解决
setsebool -P httpd_read_user_content 1
setsebool -P httpd_unified 1
请把以下信息贴出来(越全越好):
例如:
CentOS 7 + PHP 7.4 + Laravel 8
报错:SQLSTATE[HY000] [2002] No such file or directory
我可以直接给你精确解决方案。