Debian系统下优化 ThinkPHP 运行的实用方案
一 基础运行与重写配置
server {
listen 80;
server_name your_domain.com;
root /var/www/your-app/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000; # 或 unix:/var/run/php/php-fpm.sock;
}
# 屏蔽敏感文件
location ~ /\.(env|git|htaccess)$ { deny all; }
# 静态资源缓存
location ~* \.(css|js|png|jpe?g|gif|ico|svg|woff2?|ttf)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
如使用 Apache,需启用 mod_rewrite,并在项目目录配置 .htaccess 以隐藏 index.php,同时确保静态资源路径可正常访问。二 PHP 与 PHP-FPM 优化
; /etc/php/8.2/fpm/php.ini 或相应版本
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0 ; 生产建议关闭,用部署脚本触发清除
opcache.revalidate_freq=0
opcache.fast_shutdown=1
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 500 ; 防止内存泄漏累积
request_terminate_timeout = 30
memory_limit = 256M
max_execution_time = 30
post_max_size = 32M
upload_max_filesize = 32M
systemctl reload php8.2-fpm(按实际版本替换)。三 数据库与缓存策略
php think optimize:route,降低路由注册开销。四 系统与网络优化
# limits.conf
* soft nofile 65535
* hard nofile 65535
# sysctl.conf
net.core.somaxconn = 65535
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_syncookies = 1
net.core.netdev_max_backlog = 65535
五 上线检查与渐进式调优