在 CentOS 上提升 Laravel 运行速度,可以从 PHP 配置、Laravel 自身优化、服务器环境、缓存与架构 几个方面入手。下面按「见效快 → 进阶优化」说明。
在 生产环境 一定要执行:
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
php artisan optimize
⚠️ 修改配置或路由后,记得重新 cache
关闭调试:
APP_DEBUG=false
APP_ENV=production
Laravel 10/11 推荐 PHP 8.1+
php -v
升级示例(Remi 源):
yum install -y epel-release
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php82
yum update -y php*
编辑:
vi /etc/php.d/10-opcache.ini
推荐配置:
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.validate_timestamps=0
重启 PHP:
systemctl restart php-fpm
vi /etc/php-fpm.d/www.conf
关键参数:
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
根据内存调整(1GB ≈ 20–30 children)
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
}
开启 gzip:
gzip on;
gzip_types text/css application/javascript application/json;
避免 PHP 处理 css/js:
location ~* \.(js|css|png|jpg|svg|woff2)$ {
expires 30d;
access_log off;
}
Schema::table('users', function ($table) {
$table->index('email');
});
User::with('posts')->get();
.env
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
安装 Redis:
yum install redis
systemctl enable --now redis
耗时任务使用队列:
php artisan queue:work --daemon
用 supervisor 守护:
yum install supervisor
生产环境安装:
composer install --optimize-autoloader --no-dev
setenforce 0
✅ CentOS 7/8
✅ Nginx
✅ PHP 8.2 + OPcache
✅ Redis
✅ Laravel 优化命令
✅ 队列异步
如果你愿意,可以告诉我:
我可以直接给你一份可落地的配置方案。