Linux 服务器优化实战指南
适用于: Ubuntu / Debian / CentOS / Rocky / AlmaLinux 等常用服务器环境。
目录
- 一、基础环境准备
- 二、内核参数优化(sysctl)
- 三、文件句柄与进程限制
- 四、网络性能优化
- 五、SSH 优化
- 六、磁盘 I/O 优化
- 七、内存与缓存优化
- 八、安全增强
- 九、Nginx 性能优化示例
- 十、监控工具
- 十一、自动备份
- 结语
一、基础环境准备
sudo apt update && sudo apt upgrade -y
sudo yum update -y
sudo yum update -y
sudo apt install -y vim curl wget net-tools htop lsof
二、内核参数优化(sysctl)
编辑:/etc/sysctl.d/99-custom.conf
fs.file-max = 1000000
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_syn_backlog = 8096
net.ipv4.tcp_syncookies = 1
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_syn_backlog = 8096
net.ipv4.tcp_syncookies = 1
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
sudo sysctl –system
三、文件句柄与进程限制
* soft nofile 1000000
* hard nofile 1000000
* soft nproc 65535
* hard nproc 65535
* hard nofile 1000000
* soft nproc 65535
* hard nproc 65535
四、网络性能优化
lsmod | grep bbr
echo “net.ipv4.tcp_congestion_control = bbr” | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
echo “net.ipv4.tcp_congestion_control = bbr” | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
五、SSH 优化
UseDNS no
LoginGraceTime 20
PermitRootLogin prohibit-password
LoginGraceTime 20
PermitRootLogin prohibit-password
六、磁盘 I/O 优化
cat /sys/block/sda/queue/scheduler
echo mq-deadline | sudo tee /sys/block/sda/queue/scheduler
echo mq-deadline | sudo tee /sys/block/sda/queue/scheduler
七、内存与缓存优化
vm.swappiness = 10
vm.vfs_cache_pressure = 50
sudo sysctl -p
vm.vfs_cache_pressure = 50
sudo sysctl -p
⚠️ 清理缓存可能引发性能波动,请谨慎执行。
sync
echo 3 | sudo tee /proc/sys/vm/drop_caches
echo 3 | sudo tee /proc/sys/vm/drop_caches
八、安全增强
sudo ufw default deny incoming
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw allow OpenSSH
sudo ufw enable
sudo apt install fail2ban -y
sudo systemctl enable –now fail2ban
sudo systemctl enable –now fail2ban
九、Nginx 性能优化示例
worker_processes auto;
worker_rlimit_nofile 1000000;events {
worker_connections 20480;
multi_accept on;
}http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
client_max_body_size 50M;
gzip on;
}
worker_rlimit_nofile 1000000;events {
worker_connections 20480;
multi_accept on;
}http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
client_max_body_size 50M;
gzip on;
}
十、监控工具
| 工具 | 用途 |
|---|---|
| htop | CPU / 内存监控 |
| iotop | I/O 调试 |
| nload | 实时带宽监控 |
| dstat | 综合系统状态 |
| ss / netstat | 网络连接分析 |
十一、自动备份
0 3 * * * /usr/bin/rsync -a /data /backup/
结语
以上优化可显著提升服务器性能、稳定性与并发处理能力。如需更专业排版,我可继续为你制作更高级的 Notion 风版式。
评论(已关闭)
评论已关闭