在 CentOS 上优化 Laravel 主要从系统层、PHP 层、Laravel 层、数据库层、缓存与队列、Web 服务几个方面入手。下面是一套比较实用、可落地的优化方案。
yum update -y
ulimit -n 65535
在 /etc/security/limits.conf 中:
* soft nofile 65535
* hard nofile 65535
/etc/sysctl.conf 示例:
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
vm.swappiness = 10
sysctl -p
yum install php php-cli php-fpm php-mysqlnd php-mbstring php-xml php-curl php-zip php-gd php-opcache
/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 ; 生产环境
/etc/php-fpm.d/www.conf
pm = dynamic
pm.max_children = 120
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 500
APP_ENV=production
APP_DEBUG=false
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan optimize (Laravel 8+ 可省略)
composer install --optimize-autoloader --no-dev
telescope、debugbar(生产)laravel ide helper(生产)worker_processes auto;
worker_connections 10240;
keepalive_timeout 30;
gzip on;
gzip_types text/css application/javascript application/json;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 60;
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
query_cache_type = 0
max_connections = 300
User::with('posts')->get();
chunk() / lazy() 处理大数据.env
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
php artisan queue:work --daemon
配合 supervisor 管理进程。
适合高并发:
composer require laravel/octane
php artisan octane:install
使用 Swoole / RoadRunner,性能可提升数倍。
supervisor 管理进程htopnetdataprometheus + grafana✅ PHP 8 + OPcache
✅ Laravel config/route/view cache
✅ Nginx + PHP-FPM 调优
✅ Redis 缓存 & 队列
✅ MySQL 索引 & 连接优化
✅ 生产环境关闭 debug
如果你愿意,我可以: