針對記憶體受限、IO 瓶頸明顯的 VPS 環境,本文匯總從交換空間擴充、時區校準、核心網絡參數深度調優、TTY 精簡、IPv6 關閉到啟動服務瘦身的完整優化清單,並附贈 Nginx 高併發場景的 sysctl.conf 參考配置,助您榨乾每一分硬體效能。
許多 VPS 預設無 Swap 分區,記憶體耗盡易觸發 OOM Killer 終止關鍵進程。建議建立 1-2 倍實體記憶體的 Swap 檔(以 256MB 為例):
cd /var
dd if=/dev/zero of=swapfile bs=1024 count=262144 # 256MB
/sbin/mkswap swapfile
/sbin/swapon swapfile
/sbin/swapon -s # 驗證
# 開機自動掛載
echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab
進階:調整 vm.swappiness=10 降低 Swap 激進度,優先使用實體記憶體。
cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
sysctl 僅臨時生效,永久配置請編輯 /etc/sysctl.conf 後執行 sysctl -p。
| 參數 | 說明 | 建議值 |
|---|---|---|
| net.core.somaxconn | listen() 佇列長度,影響高併發連線接受率 | 262144 |
| net.core.netdev_max_backlog | 網卡封包處理佇列 | 262144 |
| net.ipv4.tcp_max_syn_backlog | SYN 請求佇列長度,防禦 SYN Flood | 262144 |
| net.ipv4.tcp_syncookies | 啟用 SYN Cookie | 1 |
| net.ipv4.tcp_tw_reuse | 重用 TIME-WAIT Socket | 1 |
| net.ipv4.tcp_tw_recycle | 快速回收 TIME-WAIT(NAT 環境慎用) | 1 |
| net.ipv4.tcp_fin_timeout | FIN-WAIT-2 超時時間 | 30 |
| net.ipv4.tcp_keepalive_time | Keepalive 偵測間隔 | 1200 |
| net.ipv4.ip_local_port_range | 本地端口範圍 | 1024 65000 |
| net.ipv4.tcp_max_tw_buckets | 系統最大 TIME-WAIT 數量 | 5000 |
| net.core.rmem_max / wmem_max | Socket 讀寫緩衝區上限 | 16777216 |
| net.ipv4.tcp_rmem / tcp_wmem | TCP 讀寫緩衝區自動調整範圍 | 4096 87380 4194304 |
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
套用:sysctl -p
每個 TTY 常駐佔用約 4MB,個人 VPS 僅需 1-2 個。
sed -i 's/tty\[1-6\]/tty[1-2]/' /etc/inittab
sed -i 's/tty\[1-6\]/tty[1-2]/' /etc/sysconfig/init
sed -i 's/tty\[1-6\]/tty[1-2]/' /etc/init/start-ttys.conf
sed -i 's/tty\[1-6\]/tty[1-2]/' /etc/sysconfig/init
重啟生效。
cat >> /etc/modprobe.d/dist.conf <<'EOF'
alias net-pf-10 off
alias ipv6 off
EOF
# 部分發行版為 /etc/modprobe.conf
重啟系統。
# 列出所有開機自啟服務
chkconfig --list | grep :on
# 典型可關閉項目(視實際業務而定)
chkconfig ip6tables off # IPv6 防火牆
chkconfig postfix off # 郵件服務(非郵件伺服器可關)
chkconfig bluetooth off # 藍牙
# 關閉後立即停止
service ip6tables stop
free -h 確認 Swap 生效ss -ant | awk '{print $1}' | sort | uniq -c | sort -rn 觀察連線狀態分佈cat /proc/net/sockstat 監控 Socket 使用量優化無絕對標準,需依據業務負載(Web、DB、Cache)與硬體資源持續觀測微調。建議建立基準線,變更單一參數後進行 A/B 測試。