在 CentOS 上优化 Laravel,一般可以从系统层、PHP 层、Laravel 自身、缓存与队列、数据库、Web 服务器几个方面来做。下面给你一套比较实用、可落地的优化方案。
systemctl disable --now firewalld # 如果用了云安全组
systemctl disable --now postfix
保留:nginx / php-fpm / redis / mysql
ulimit -n 65535
永久生效:
/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
Laravel 大量读文件(config、routes、views),IO 很关键。
php -v
Laravel 10/11 在 PHP 8.2+ 性能提升明显。
yum install php-opcache
/etc/php.d/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 = 100
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
⚠️ 改代码后必须重新 cache
.env
APP_DEBUG=false
APP_ENV=production
composer require laravel/octane
php artisan octane:install
使用 RoadRunner 或 Swoole
.env
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
php artisan queue:work --daemon
用 systemd 或 supervisor 守护
crontab -e
* * * * * php /path/artisan schedule:run
$table->index('user_id');
❌
User::all()->posts
✅
User::with('posts')->get();
SHOW VARIABLES LIKE 'slow_query_log';
gzip on;
gzip_types text/css application/javascript;
location ~* \.(js|css|png)$ {
expires 30d;
}
htopphp artisan telescope(开发)ab / wrk 压测ab -n 1000 -c 100 http://site/
✅ PHP 8.2 + OPcache
✅ config / route / view cache
✅ Redis 缓存 + 队列
✅ Nginx gzip
✅ supervisor 管理队列
如果你愿意,可以告诉我:
我可以给你一套精准配置。