在 CentOS 上运行 Laravel “卡”,常见原因和优化方向可以按下面几块来排查和处理。
php artisan 慢 → 看 PHP 本身、文件系统、权限top
htop
free -h
df -h
重点看:
CentOS 默认可能是 PHP 7.2/7.4,太老。
php -v
建议升级到 PHP 8.1 / 8.2 / 8.3
yum install php-opcache
php.ini 示例:
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
重启 PHP:
systemctl restart php-fpm
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan optimize
⚠️ 改了配置/路由后记得重新 cache
.env
APP_DEBUG=false
APP_ENV=production
php artisan serve只适合开发,生产用 Nginx + PHP-FPM
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
}
PHP-FPM 调整:
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
SHOW PROCESSLIST;
Schema::table('users', function ($table) {
$table->index('email');
});
.env
DB_QUERY_LOG=true
或用:
\DB::enableQueryLog();
查看:
getenforce
临时关闭测试:
setenforce 0
⚠️ 生产环境建议正确配置而不是直接关
chown -R nginx:nginx storage bootstrap/cache
chmod -R 755 storage
/storage/logs 无限增大php artisan log:clear
.env
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
安装:
yum install redis
systemctl enable redis
如果你愿意,可以告诉我:
我可以给你针对性配置方案。