Hysteria 2 服务器搭建教程(基于 Ubuntu/Debian,使用 Let's Encrypt 证书)
Hysteria 2 服务器搭建教程(基于 Ubuntu/Debian,使用 Let's Encrypt 证书)
1. 准备工作
1.1 服务器要求
- 操作系统:Ubuntu 20.04+/Debian 11+
- 开放端口:UDP 443(用于 Hysteria 2)
- 域名:准备一个已解析到服务器 IP 的域名(如
yourdomain.com)。
1.2 依赖安装
sudo apt update && sudo apt install -y wget certbot
2. 安装 Hysteria 2
2.1 下载并安装二进制文件
wget https://github.com/apernet/hysteria/releases/latest/download/hysteria-linux-amd64 -O /usr/local/bin/hysteria
chmod +x /usr/local/bin/hysteria
2.2 创建配置文件
编辑 /etc/hysteria/config.yaml:
listen: :443 # 监听 UDP 443
tls:
cert: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
key: /etc/letsencrypt/live/yourdomain.com/privkey.pem
auth:
type: password
password: yourpassword # 替换为你的密码
2.3 申请 Let's Encrypt 证书
sudo certbot certonly --standalone -d yourdomain.com
注:如果 80/443 端口被占用(如 Nginx),需先停止服务再运行 Certbot。
3. 配置系统服务
3.1 创建 Systemd 服务文件
编辑 /etc/systemd/system/hysteria.service:
[Unit]
Description=Hysteria 2 Proxy Server
After=network.target
[Service]
User=root
ExecStart=/usr/local/bin/hysteria server -c /etc/hysteria/config.yaml
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
3.2 启动并设置开机自启
sudo systemctl daemon-reload
sudo systemctl start hysteria
sudo systemctl enable hysteria
3.3 检查运行状态
sudo systemctl status hysteria
sudo ss -tulnp | grep hysteria # 确认 UDP 443 监听
4. 防火墙设置
4.1 放行 UDP 443
sudo ufw allow 443/udp
sudo ufw reload
4.2 验证防火墙规则
sudo ufw status
5. 客户端配置示例
5.1 Hysteria 客户端配置(JSON 格式)
{
"server": "yourdomain.com:443",
"auth": "yourpassword",
"tls": {
"sni": "yourdomain.com",
"alpn": "h3"
}
}
5.2 客户端支持
- Windows/macOS:使用 v2rayN 或 Hysteria GUI。
- Linux/Android:使用官方 CLI 客户端或兼容的代理工具。
6. 常见问题排查
6.1 证书权限问题
sudo chmod 644 /etc/letsencrypt/live/yourdomain.com/fullchain.pem
sudo chmod 640 /etc/letsencrypt/live/yourdomain.com/privkey.pem
6.2 端口冲突
- Nginx/Apache:确保它们仅占用 TCP 443,不与 UDP 443 冲突。
- 其他代理工具:检查
ss -tulnp并关闭冲突服务。
6.3 查看日志
journalctl -u hysteria -n 50 --no-pager
7. 优化建议
- 启用端口跳跃(减少 QoS 干扰):
# 在服务器配置中添加
bandwidth:
up: "100 mbps"
down: "100 mbps"
quic:
initStreamReceiveWindow: 8388608
maxStreamReceiveWindow: 8388608
- 定期更新证书:
sudo certbot renew --dry-run
总结
- ✅ 成功标志:客户端能通过 UDP 443 连接服务器,且速度稳定。
- ⚙️ 关键点:证书路径、密码匹配、防火墙放行 UDP 443。
- 🚀 进阶:可结合 Nginx 反代实现 Web 伪装(需 TCP 443 与 UDP 443 分开)。
按此教程操作,即可快速搭建高性能的 Hysteria 2 代理服务! 🚀
本文由萧兮的博客原创发布,欢迎转载,转载务必保留原文链接。
萧兮的博客:https://www.20010515.xyz · 原文:https://www.20010515.xyz/posts/8f585383-473b-4071-a543-238d867916ec