📝 AlmaLinux 9/10 分布式爬虫代理池部署指南
1. 系统约定
- 操作系统:AlmaLinux 9 / 10
- 主服务器公网 IP:自动获取(通过
curl) - 出口 IP 段:
9.9.9.2 - 9.9.9.252 - 代理协议:Socks5
- 端口范围:
10001 - 10252
2. 环境检查
2.1 更新系统
dnf update -y
2.2 安装常用工具
dnf install -y epel-release wget curl vim net-tools iproute iptables
2.3 检查 IP 是否绑定
ip addr show
确认 9.9.9.2-252 已绑定到网卡。
3. 防火墙配置
3.1 开放端口范围
firewall-cmd --permanent --add-port=10001-10252/tcp
firewall-cmd --reload
firewall-cmd --list-ports
4. 代理软件安装
推荐两款:
4.1 3proxy(轻量、灵活)
dnf install -y gcc make git
git clone https://github.com/z3APA3A/3proxy.git
cd 3proxy
make -f Makefile.Linux
mkdir -p /etc/3proxy
cp src/3proxy /usr/local/bin/
5. 配置文件生成脚本
5.1 生成 3proxy 配置 /usr/local/bin/gen_3proxy_cfg.sh
#!/bin/bash
# 自动生成 3proxy 配置文件
# 用法: ./gen_3proxy_cfg.sh 9.9.9.2-252
if [ -z "$1" ]; then
echo "用法: $0 <出口IP范围,例如 9.9.9.2-252>"
exit 1
fi
# 获取主机公网 IP
MAIN_IP=$(curl -s ifconfig.me)
if [ -z "$MAIN_IP" ]; then
echo "无法获取主机公网 IP"
exit 1
fi
# 解析参数
BASE=$(echo $1 | cut -d. -f1-3) # 9.9.9
START=$(echo $1 | cut -d. -f4 | cut -d- -f1) # 2
END=$(echo $1 | cut -d- -f2) # 252
CFG=/etc/3proxy/3proxy.cfg
BASE_PORT=10000
mkdir -p /etc/3proxy
cat > $CFG <<EOF
daemon
nscache 65536
auth none
internal = $MAIN_IP
EOF
for i in $(seq $START $END); do
ip="$BASE.$i"
port=$((BASE_PORT + i))
echo "external = $ip" >> $CFG
echo "socks -p$port" >> $CFG
done
echo "✅ 配置文件已生成: $CFG"
使用方法:
chmod +x /usr/local/bin/gen_3proxy_cfg.sh
/usr/local/bin/gen_3proxy_cfg.sh 9.9.9.2-252
6. 启动文件脚本
6.1 Systemd 服务文件 /etc/systemd/system/3proxy.service
[Unit]
Description=3proxy Proxy Server
After=network.target
[Service]
ExecStart=/usr/local/bin/3proxy /etc/3proxy/3proxy.cfg
Restart=always
[Install]
WantedBy=multi-user.target
启用服务:
systemctl daemon-reexec
systemctl enable --now 3proxy
systemctl status 3proxy
7. 测试代理出口脚本
7.1 Bash 版 /usr/local/bin/test_proxies.sh
#!/bin/bash
# 批量测试代理出口 IP
# 用法: ./test_proxies.sh 1.1.1.1 10001 10252
if [ $# -lt 3 ]; then
echo "用法: $0 <主机IP> <起始端口> <结束端口>"
exit 1
fi
SERVER_IP=$1
START_PORT=$2
END_PORT=$3
for port in $(seq $START_PORT $END_PORT); do
result=$(curl -s --max-time 5 --socks5 $SERVER_IP:$port https://api.ipify.org)
if [ -n "$result" ]; then
echo "[OK] $SERVER_IP:$port → 出口IP: $result"
else
echo "[FAIL] $SERVER_IP:$port"
fi
done
使用方法:
chmod +x /usr/local/bin/test_proxies.sh
/usr/local/bin/test_proxies.sh 1.1.1.1 10001 10252
✅ 总结
- 环境检查 → 确认 IP、更新系统
- 防火墙 → 开放端口范围
- 安装代理软件 → 推荐 3proxy / Dante
- 配置文件生成脚本 → 自动生成 252 个端口映射
- systemd 启动脚本 → 后台运行
- 测试脚本 → 验证出口 IP
IDC头条