Ubuntu上 ThinkPHP 性能优化清单
一 基础运行环境与版本
php -v 检查版本,必要时通过 update-alternatives 切换版本。sudo systemctl status php8.2-fpm、sudo systemctl restart php8.2-fpm、sudo nginx -t && sudo systemctl restart nginx。二 PHP 运行时与 OPcache 优化
opcache.enable=1opcache.memory_consumption=128(按内存调大到 256 视负载而定)opcache.interned_strings_buffer=8opcache.max_accelerated_files=4000(大项目可调至 10000+)opcache.validate_timestamps=0(生产关闭;开发可设为 1 或 revalidate_freq=60)pm=dynamic、pm.max_children=50、pm.start_servers=5、pm.min_spare_servers=5、pm.max_spare_servers=35sudo systemctl restart php8.2-fpm。三 ThinkPHP 框架层优化
.env 或 config/app.php 中设置 APP_DEBUG=false,减少日志与错误输出带来的开销。php think optimize:configphp think optimize:route四 Web 服务器与网络层优化
try_files $uri $uri/ /index.php?$query_string;fastcgi_pass unix:/run/php/php8.2-fpm.sock;(路径与 PHP-FPM 一致)location ~ /\.env { deny all; }gzip on;gzip_types text/plain text/css application/javascript application/json image/svg+xml;五 数据库与缓存、监控与排错
fastcgi_pass 套接字路径是否一致。try_files $uri $uri/ /index.php?$query_string; 已配置。