VPS 极速优化脚本 (ZRAM + 2G Swap + 内核调优)
适用系统:Debian / Ubuntu / CentOS
使用方法:复制下方代码,保存为 optimize.sh,运行 sudo bash optimize.sh
#!/bin/bash
# ============================================================
# Description: Auto Setup ZRAM (Dynamic) + 2GB Disk Swap + Sysctl
# Author: Gemini Expert System
# ============================================================
# 检查 Root 权限
if [ "$(id -u)" != "0" ]; then
echo "Error: 必须使用 root 运行此脚本" 1>&2
exit 1
fi
echo ">> [1/5] 正在清理旧环境 (防止冲突)..."
# 1. 停止并移除现有的 ZRAM
if grep -q zram /proc/swaps; then
swapoff /dev/zram0 2>/dev/null
rmmod zram 2>/dev/null
fi
# 停止旧的服务
systemctl stop zram-config.service 2>/dev/null
systemctl disable zram-config.service 2>/dev/null
rm -f /etc/systemd/system/zram-config.service
rm -f /usr/local/bin/zram-start.sh
# 2. 停止并移除现有的磁盘 Swap
if grep -q '/swapfile' /proc/swaps; then
swapoff /swapfile 2>/dev/null
fi
# 从 fstab 中彻底移除 swapfile 配置 (防止重复)
sed -i '/\/swapfile/d' /etc/fstab
# 删除物理文件
rm -rf /swapfile
echo ">> [2/5] 配置 ZRAM (内存压缩)..."
# 创建 ZRAM 启动脚本
cat << 'EOF' > /usr/local/bin/zram-start.sh
#!/bin/bash
modprobe zram num_devices=1
# 动态获取物理内存大小
total_mem=$(grep MemTotal /proc/meminfo | awk '{print $2}')
# 设置 ZRAM 大小为物理内存的 100% (1:1)
zram_size=${total_mem}K
# 优先选择 zstd 算法
algo="lzo"
if grep -q zstd /sys/block/zram0/comp_algorithm; then
algo="zstd"
elif grep -q lz4 /sys/block/zram0/comp_algorithm; then
algo="lz4"
fi
echo $algo > /sys/block/zram0/comp_algorithm
echo $zram_size > /sys/block/zram0/disksize
mkswap /dev/zram0 > /dev/null
# 优先级设为 100 (最高)
swapon --priority 100 /dev/zram0
echo "ZRAM Active: Algo=$algo, Size=$zram_size"
EOF
chmod +x /usr/local/bin/zram-start.sh
# 创建 Systemd 持久化服务
cat << EOF > /etc/systemd/system/zram-config.service
[Unit]
Description=Configure ZRAM for Low Memory VPS
After=multi-user.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/zram-start.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable zram-config.service
systemctl restart zram-config.service
echo ">> [3/5] 创建 2GB 磁盘 Swap (保底保险)..."
# 创建 2048MB 文件
dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
chmod 600 /swapfile
mkswap /swapfile
# 启用磁盘 swap,默认优先级 (-2),让它排在 ZRAM 后面
swapon /swapfile
# 写入 fstab
echo '/swapfile none swap sw 0 0' >> /etc/fstab
echo ">> [4/5] 应用内核参数调优..."
cat << EOF > /etc/sysctl.d/99-vps-optimization.conf
# 积极使用 ZRAM (80)
vm.swappiness = 80
# 关闭 ZRAM 预读
vm.page-cluster = 0
# 保持文件系统缓存压力
vm.vfs_cache_pressure = 100
EOF
sysctl --system > /dev/null
echo ">> [5/5] ✅ 优化全部完成!状态检查:"
echo "------------------------------------------------"
swapon --show
echo "------------------------------------------------"
free -h
运行方法
nano optimize_vps.sh # 粘贴上面的代码并保存 (Ctrl+O, Enter, Ctrl+X)
chmod +x optimize_vps.sh
sudo ./optimize_vps.sh
最小化安装的ubuntu和debian可能得先更新内核
# 1. 更新软件源 apt update # 2. 尝试安装当前内核的额外模块包 # 注意:如果这一步提示 "Unable to locate package",请直接跳到下面的第 3 步 apt install linux-modules-extra-$(uname -r) # 3. 如果第 2 步失败,或者为了保险起见,直接安装标准的通用内核 apt install linux-generic # 4. 非常重要:安装完内核后必须重启才能生效 reboot
Debian 12 换源阿里云一键脚本
cat << 'EOF' > update_mirror.sh #!/bin/bash # 检查 root 权限 if [ "$EUID" -ne 0 ]; then echo "请使用 sudo 或 root 账号运行此脚本" exit 1 fi echo "开始备份原始源文件..." cp /etc/apt/sources.list /etc/apt/sources.list.bak_$(date +%Y%m%d%H%M%S) echo "正在安装 HTTPS 支持和证书..." apt-get update && apt-get install -y apt-transport-https ca-certificates echo "正在写入阿里云 Debian 12 (Bookworm) 源配置..." cat << 'SOURCES' > /etc/apt/sources.list # 阿里云 Debian 12 Bookworm 镜像源 deb https://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware deb-src https://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware deb https://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware deb-src https://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware deb https://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware deb-src https://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware deb https://mirrors.aliyun.com/debian/ bookworm-backports main contrib non-free non-free-firmware deb-src https://mirrors.aliyun.com/debian/ bookworm-backports main contrib non-free non-free-firmware SOURCES echo "正在刷新软件源缓存..." apt-get update echo "✅ 换源完成!系统现在可以使用阿里云镜像进行高速更新了。" EOF # 给脚本执行权限并运行 chmod +x update_mirror.sh sudo ./update_mirror.sh