TOP云提供高性价比云服务器租用,有中国内地/港澳台、海外等全球各地节点,TOP云国内云服务器只要有域名备案号就能直接用,无须重复备案;港澳台及海外云服务器不用备案,购买之后直接使用,省时省力省心。价格实惠,续费同价,2核2G5M仅需27元每月,8核8G50M仅需66元每月,更多配置套餐请进入下面网址了解:

TOP云总站云服务器:https://topyun.vip/server/buy.html

TOP云C站云服务器:https://c.topyun.vip/cart

在云服务器上搭建 VPN 或代理服务器 可以实现安全上网、绕过网络限制或访问海外资源。以下是详细指南,涵盖 OpenVPN、WireGuard、Shadowsocks、V2Ray 等主流方案的配置方法,并附安全加固建议。


1. 准备工作

(1) 云服务器要求

  • 操作系统:Linux(推荐 Ubuntu 20.04/22.04 或 Debian 10/11)。

  • 服务器配置

    • 最低配置:1核 CPU、1GB 内存(仅测试可用)。

    • 生产环境建议:2核 CPU、2GB 内存(WireGuard 可更低)。

  • 网络要求

    • 公网 IP(必须)。

    • 开放 VPN/代理端口(如 OpenVPN 默认 1194,WireGuard 51820,Shadowsocks 8388)。

  • 域名(可选):如需远程访问,可绑定域名(需 DNS 解析)。

(2) 法律合规性

  • 遵守当地法律:部分国家/地区限制 VPN 使用,需确保合法用途(如企业内网、科研访问)。

  • 避免滥用:禁止用于非法活动(如破解、攻击)。


2. 搭建 VPN 服务器

(1) OpenVPN(经典方案,兼容性好)

安装 OpenVPN

sudo apt update && sudo apt upgrade -y
sudo apt install openvpn easy-rsa -y

配置 PKI(证书颁发机构)

make-cadir ~/openvpn-ca
cd ~/openvpn-ca

编辑 vars 文件(设置证书信息):

nano vars

修改以下内容(按需调整):

set_var EASYRSA_REQ_COUNTRY    "CN"
set_var EASYRSA_REQ_PROVINCE   "Beijing"
set_var EASYRSA_REQ_CITY       "Beijing"
set_var EASYRSA_REQ_ORG        "YourOrg"
set_var EASYRSA_REQ_EMAIL      "admin@example.com"
set_var EASYRSA_REQ_OU         "IT"
set_var EASYRSA_ALGO           "ec"
set_var EASYRSA_CURVE          "secp384r1"
set_var EASYRSA_DIGEST         "sha512"

生成 CA 证书:

source vars
./clean-all
./build-ca

生成服务器证书和密钥

./build-key-server server
./build-dh
openvpn --genkey --secret keys/ta.key

配置 OpenVPN 服务器

sudo cp ~/openvpn-ca/keys/{server.crt,server.key,ca.crt,dh2048.pem,ta.key} /etc/openvpn/server/
sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/server/
sudo gzip -d /etc/openvpn/server/server.conf.gz

编辑配置文件:

sudo nano /etc/openvpn/server/server.conf

关键参数:

port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh2048.pem
server 10.8.0.0 255.255.255.0
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
keepalive 10 120
tls-auth ta.key 0
cipher AES-256-CBC
user nobody
group nogroup
persist-key
persist-tun
status /var/log/openvpn-status.log
verb 3

启动 OpenVPN:

sudo systemctl start openvpn@server
sudo systemctl enable openvpn@server

创建客户端证书

cd ~/openvpn-ca source vars
./build-key client1

生成客户端配置文件(client1.ovpn):

cat > client1.ovpn <<EOF
client
dev tun
proto udp
remote your-server-ip 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert client1.crt
key client1.key
remote-cert-tls server
cipher AES-256-CBC
auth SHA256
verb 3
EOF

将 client1.ovpn 和证书文件发给客户端使用。


(2) WireGuard(高性能,现代方案)

安装 WireGuard

sudo apt install wireguard -y

生成密钥

umask 077
wg genkey | tee privatekey | wg pubkey > publickey

记录 privatekey(服务器私钥)和 publickey(服务器公钥)。

配置 WireGuard

sudo nano /etc/wireguard/wg0.conf

写入以下内容(修改 Address 和 AllowedIPs):

[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <服务器私钥>
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
PublicKey = <客户端公钥>
AllowedIPs = 10.0.0.2/32

启动 WireGuard

sudo systemctl enable --now wg-quick@wg0

客户端配置示例:

[Interface]
PrivateKey = <客户端私钥>
Address = 10.0.0.2/24
DNS = 8.8.8.8

[Peer]
PublicKey = <服务器公钥>
Endpoint = your-server-ip:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

(3) Shadowsocks(轻量代理,适合绕过限制)

安装 Shadowsocks

sudo apt install python3-pip -y
pip3 install shadowsocks

配置 Shadowsocks

sudo nano /etc/shadowsocks.json

写入:

{
    "server": "your-server-ip",
    "server_port": 8388,
    "local_address": "127.0.0.1",
    "local_port": 1080,
    "password": "your-password",
    "timeout": 300,
    "method": "aes-256-gcm"
}

启动服务:

ssserver -c /etc/shadowsocks.json -d start

3. 搭建代理服务器(V2Ray/Clash)

(1) V2Ray(多功能代理)

安装 V2Ray

bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)

配置 V2Ray

sudo nano /usr/local/etc/v2ray/config.json

示例配置(VMess 协议):

{
    "inbounds": [
        {
            "port": 443,
            "protocol": "vmess",
            "settings": {
                "clients": [
                    {
                        "id": "your-uuid",  # 生成 UUID:`uuidgen`
                        "alterId": 64
                    }
                ]
            },
            "streamSettings": {
                "network": "tcp",
                "security": "tls"
            }
        }
    ],
    "outbounds": [
        {
            "protocol": "freedom",
            "settings": {}
        }
    ]
}

启动 V2Ray:

sudo systemctl enable --now v2ray

(2) Clash(规则代理,适合科学上网)

安装 Clash

wget https://github.com/Fndroid/clash_for_windows_pkg/releases/download/0.20.4/Clash.Meta.tar.gz
tar -xzf Clash.Meta.tar.gz -C /opt/

配置文件示例(/etc/clash/config.yaml):

port: 7890
socks-port: 7891
redir-port: 7892
allow-lan: false
mode: Rule
log-level: info
external-controller: 127.0.0.1:9090
proxies:
  - name: "Proxy"
    type: vmess
    server: your-server-ip
    port: 443
    uuid: your-uuid
    alterId: 64
    cipher: auto
    tls: true

启动 Clash:

/opt/clash/clash -d /etc/clash

4. 安全加固

(1) 防火墙配置

开放 VPN/代理端口:

sudo ufw allow 1194/udp   # OpenVPN
sudo ufw allow 51820/udp  # WireGuard
sudo ufw allow 8388/tcp   # Shadowsocks
sudo ufw enable

(2) 防暴力破解

安装 fail2ban:

sudo apt install fail2ban -y
sudo systemctl enable fail2ban

创建自定义规则(如针对 OpenVPN 的 1194 端口):

sudo nano /etc/fail2ban/jail.local

添加:

[openvpn]
enabled = true
port = 1194
filter = openvpn
logpath = /var/log/openvpn.log
maxretry = 3
bantime = 1h

(3) 定期更新

sudo apt update && sudo apt upgrade -y

5. 常见问题

Q1: 客户端无法连接 VPN?

  • 检查防火墙是否开放端口(sudo ufw status)。

  • 检查服务器日志(如 OpenVPN 的 /var/log/openvpn.log)。

  • 确保客户端配置正确(如证书、IP、端口)。

Q2: 如何支持多设备连接?

  • OpenVPN/WireGuard 可生成多个客户端证书。

  • Shadowsocks/V2Ray 可配置多个用户或规则。

Q3: 如何隐藏 VPN 流量?

  • 使用 混淆插件(如 V2Ray 的 freedom + tls)。

  • 选择非标准端口(如 443)。


6. 推荐方案

需求推荐方案特点
高安全性OpenVPN兼容性好,支持 TLS
高性能WireGuard低延迟,现代加密
绕过限制Shadowsocks轻量,易配置
多协议支持V2Ray支持 VMess/Shadowsocks

总结

  1. 选择方案:根据需求选择 OpenVPN、WireGuard、Shadowsocks 或 V2Ray。

  2. 安装配置:按步骤生成证书/密钥并启动服务。

  3. 安全加固:防火墙、防暴力破解、定期更新。

  4. 客户端连接:分发配置文件或提供订阅链接。

完成后,你的云服务器即可提供 安全 VPN 或代理服务! 🌐


不容错过
Powered By TOPYUN 云产品资讯