配置代理服务器
把一台服务器变成一台可以使用的代理
安装命令:
!/usr/bin/env bash
set -euo pipefail
=========================================================
install3proxy.sh Ubuntu/Debian 稳定安装脚本
用途:把一台海外/普通 Ubuntu 服务器变成 HTTP 高匿 + SOCKS5 代理服务器
默认端口:HTTP 8400,SOCKS5 8500
默认账号:pdmy
默认密码:自动生成强密码,也可通过 PROXY_PASS 手动指定
使用:
sudo bash install_3proxy_ubuntu_stable_fixed.sh
指定账号密码:
PROXY_USER=pdmy PROXY_PASS='YourStrongPass123' sudo -E bash install_3proxy_ubuntu_stable_fixed.sh
指定端口:
HTTP_PORT=8400 SOCKS_PORT=8500 sudo -E bash install_3proxy_ubuntu_stable_fixed.sh
如需上级代理出口,例如本机 WARP/Clash/sing-box SOCKS5:
ENABLE_PARENT=1 PARENT_HOST=127.0.0.1 PARENT_PORT=40000 sudo -E bash install_3proxy_ubuntu_stable_fixed.sh
=========================================================
if [[ "${EUID}" -ne 0 ]]; then
echo "错误:请使用 root 权限运行,例如:sudo bash $0"
exit 1
fi
export DEBIAN_FRONTEND=noninteractive
PROXY_USER="${PROXY_USER:-pdmy}"
PROXY_PASS="${PROXY_PASS:-}"
HTTP_PORT="${HTTP_PORT:-8400}"
SOCKS_PORT="${SOCKS_PORT:-8500}"
ENABLE_PARENT="${ENABLE_PARENT:-0}"
PARENT_HOST="${PARENT_HOST:-127.0.0.1}"
PARENT_PORT="${PARENT_PORT:-40000}"
PARENT_TYPE="${PARENT_TYPE:-socks5}"
INSTALL_DIR="/usr/local/3proxy"
SRC_DIR="/usr/local/src/3proxy"
CONF_DIR="${INSTALL_DIR}/conf"
CONF_FILE="${CONF_DIR}/3proxy.cfg"
BIN_FILE="${INSTALL_DIR}/bin/3proxy"
SERVICE_FILE="/etc/systemd/system/3proxy.service"
LOG_DIR="/var/log/3proxy"
LOG_FILE="${LOG_DIR}/3proxy.log"
RUN_USER="proxy3"
RUN_GROUP="proxy3"
生成适合放在代理 URL 里的密码,避免 @ : / ? # & % 等字符导致 URL 解析问题
if [[ -z "${PROXY_PASS}" ]]; then
if command -v openssl >/dev/null 2>&1; then
PROXY_PASS="$(openssl rand -base64 24 | tr -dc 'A-Za-z0-9' | head -c 18)"
else
PROXY_PASS="Proxy$(date +%s)$(shuf -i 1000-9999 -n 1)"
fi
fi
get_public_ip() {
local ip=""
for url in \
"https://ip.sb" \
"https://ifconfig.me/ip" \
"https://icanhazip.com" \
"https://checkip.amazonaws.com" \
"https://api.ipify.org"
do
ip="$(curl -4 -fsSL --connect-timeout 5 --max-time 10 "$url" 2>/dev/null | tr -d '[:space:]' || true)"
if [[ "$ip" =~ ^([0-9]{1,3}.){3}[0-9]{1,3}$ ]]; then
echo "$ip"
return 0
fi
done
return 1
}
判断端口是否为数字
check_port() {
local name="$1"
local value="$2"
if ! [[ "$value" =~ ^[0-9]+$ ]] || (( value < 1 || value > 65535 )); then
echo "错误:${name} 必须是 1-65535 的端口号,当前值:${value}"
exit 1
fi
}
check_port "HTTP_PORT" "${HTTP_PORT}"
check_port "SOCKS_PORT" "${SOCKS_PORT}"
if [[ "${ENABLE_PARENT}" == "1" ]]; then
check_port "PARENT_PORT" "${PARENT_PORT}"
fi
SERVER_PUBLIC_IP="${SERVER_PUBLIC_IP:-$(get_public_ip || true)}"
if [[ -z "${SERVER_PUBLIC_IP}" ]]; then
SERVER_PUBLIC_IP="服务器公网IP"
fi
echo "[1/9] 安装依赖..."
apt-get update -y
apt-get install -y git build-essential make gcc ca-certificates curl libssl-dev pkg-config openssl ufw || true
apt-get install -y git build-essential make gcc ca-certificates curl libssl-dev pkg-config openssl
if [[ ! -f /usr/include/openssl/evp.h ]]; then
echo "错误:仍然找不到 /usr/include/openssl/evp.h,请检查 libssl-dev 是否安装成功。"
exit 1
fi
echo "[2/9] 创建运行用户..."
if ! id "${RUN_USER}" >/dev/null 2>&1; then
useradd --system --no-create-home --shell /usr/sbin/nologin "${RUN_USER}"
fi
echo "[3/9] 下载/更新 3proxy 源码..."
mkdir -p /usr/local/src
if [[ -d "${SRC_DIR}/.git" ]]; then
git -C "${SRC_DIR}" remote set-url origin https://github.com/3proxy/3proxy.git || true
git -C "${SRC_DIR}" fetch --depth=1 origin master || git -C "${SRC_DIR}" fetch origin master || true
git -C "${SRC_DIR}" reset --hard origin/master || git -C "${SRC_DIR}" pull --ff-only || true
else
rm -rf "${SRC_DIR}"
if ! git clone --depth=1 https://github.com/3proxy/3proxy.git "${SRC_DIR}"; then
echo "git clone 失败,尝试使用 GitHub 源码压缩包下载..."
TMP_TAR="/tmp/3proxy-master.tar.gz"
rm -f "${TMP_TAR}"
curl -L --connect-timeout 10 --max-time 60 -o "${TMP_TAR}" https://github.com/3proxy/3proxy/archive/refs/heads/master.tar.gz
rm -rf "${SRC_DIR}"
mkdir -p "${SRC_DIR}"
tar -xzf "${TMP_TAR}" --strip-components=1 -C "${SRC_DIR}"
fi
fi
echo "[4/9] 编译 3proxy..."
cd "${SRC_DIR}"
make -f Makefile.Linux clean || true
make -f Makefile.Linux
if [[ ! -x "${SRC_DIR}/bin/3proxy" ]]; then
echo "错误:编译后没有找到 ${SRC_DIR}/bin/3proxy"
exit 1
fi
echo "[5/9] 安装 3proxy 文件..."
install -d "${INSTALL_DIR}/bin" "${CONF_DIR}"
install -m 0755 "${SRC_DIR}/bin/3proxy" "${BIN_FILE}"
ln -sf "${BIN_FILE}" /usr/local/bin/3proxy.sh
echo "[6/9] 配置日志目录权限..."
mkdir -p "${LOG_DIR}"
touch "${LOG_FILE}"
chown -R "${RUN_USER}:${RUN_GROUP}" "${LOG_DIR}"
chmod 750 "${LOG_DIR}"
chmod 640 "${LOG_FILE}"
chown -R "${RUN_USER}:${RUN_GROUP}" "${INSTALL_DIR}"
echo "[7/9] 写入 3proxy 配置..."
PARENT_BLOCK="# parent disabled"
if [[ "${ENABLE_PARENT}" == "1" ]]; then
PARENT_BLOCK="parent 1000 ${PARENT_TYPE} ${PARENT_HOST} ${PARENT_PORT}"
fi
注意:不写 daemon,让 systemd 接管进程。
注意:不默认写 external。很多云服务器公网 IP 是 NAT,不在网卡上,写 external 公网 IP 会导致 502。
cat > "${CONF_FILE}" <<CFG
====================系统DNS配置====================
nserver 1.1.1.1
nserver 8.8.8.8
nserver 223.5.5.5
nscache 131072
====================监听IP====================
internal 0.0.0.0
external 默认不写,兼容云服务器 NAT。需要绑定网卡 IP 时再手动添加 external 内网网卡IP
====================全局超时====================
timeouts 3 10 40 90 180 1800 20 60
====================日志配置====================
log ${LOG_FILE} D
rotate 30
====================用户账号认证====================
users ${PROXY_USER}:CL:${PROXY_PASS}
auth strong
====================访问控制ACL====================
allow ${PROXY_USER}
${PARENT_BLOCK}
deny *
====================代理端口====================
HTTP/HTTPS CONNECT 高匿代理,-a 用于隐藏 HTTP 代理相关头
proxy -a -p${HTTP_PORT}
SOCKS5 代理不需要 -a,因为 SOCKS5 不会添加 X-Forwarded-For / Via 等 HTTP 头
socks -p${SOCKS_PORT}
CFG
mkdir -p /etc/3proxy.sh
ln -sf "${CONF_FILE}" /etc/3proxy.sh/3proxy.sh.cfg
echo "[8/9] 创建 systemd 服务..."
cat > "${SERVICE_FILE}" <<SERVICE
[Unit]
Description=3proxy Proxy Server
Documentation=https://github.com/3proxy/3proxy
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=${RUN_USER}
Group=${RUN_GROUP}
ExecStart=${BIN_FILE} ${CONF_FILE}
Restart=on-failure
RestartSec=3
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
SERVICE
systemctl daemon-reload
systemctl enable 3proxy.sh >/dev/null
systemctl stop 3proxy.sh >/dev/null 2>&1 || true
echo "[9/9] 启动 3proxy..."
systemctl restart 3proxy.sh
sleep 1
如果 ufw 已启用,自动放行端口;云厂商安全组仍需在控制台放行。
if command -v ufw >/dev/null 2>&1; then
if ufw status 2>/dev/null | grep -qi "Status: active"; then
ufw allow "${HTTP_PORT}/tcp" || true
ufw allow "${SOCKS_PORT}/tcp" || true
fi
fi
if ! systemctl is-active --quiet 3proxy.sh; then
echo "错误:3proxy 启动失败,下面是服务状态:"
systemctl status 3proxy.sh --no-pager || true
echo "你也可以执行:journalctl -u 3proxy -n 80 --no-pager"
exit 1
fi
echo ""
echo "========================================================="
echo "3proxy 安装完成,服务已运行"
echo "配置文件:${CONF_FILE}"
echo "日志目录:${LOG_DIR}"
echo "HTTP 高匿代理:http://${PROXY_USER}:${PROXY_PASS}@${SERVER_PUBLIC_IP}:${HTTP_PORT}"
echo "SOCKS5 代理:socks5://${PROXY_USER}:${PROXY_PASS}@${SERVER_PUBLIC_IP}:${SOCKS_PORT}"
if [[ "${ENABLE_PARENT}" == "1" ]]; then
echo "上级代理:${PARENT_TYPE}://${PARENT_HOST}:${PARENT_PORT}"
else
echo "上级代理:未启用,直接使用服务器出口 IP"
fi
echo ""
echo "查看状态:systemctl status 3proxy --no-pager"
echo "查看端口:ss -lntp | grep -E ':${HTTP_PORT}|:${SOCKS_PORT}'"
echo "查看日志:tail -f ${LOG_FILE}*"
echo ""
echo "本机测试 HTTP:curl -x http://${PROXY_USER}:${PROXY_PASS}@127.0.0.1:${HTTP_PORT} https://ip.sb"
echo "本机测试 SOCKS:curl --socks5-hostname ${PROXY_USER}:${PROXY_PASS}@127.0.0.1:${SOCKS_PORT} https://ip.sb"
echo "公网测试 HTTP:curl -x http://${PROXY_USER}:${PROXY_PASS}@${SERVER_PUBLIC_IP}:${HTTP_PORT} https://ip.sb"
echo "公网测试 SOCKS:curl --socks5-hostname ${PROXY_USER}:${PROXY_PASS}@${SERVER_PUBLIC_IP}:${SOCKS_PORT} https://ip.sb"
echo ""
echo "重要:请在云服务器安全组放行 TCP ${HTTP_PORT} 和 TCP ${SOCKS_PORT}。"
echo "========================================================="
评论交流
还没有公开评论。