前言
最近需要在本地搭建的 DVWA(Damn Vulnerable Web Application)测试环境暴露到公网,方便外部访问测试。由于本地网络没有公网 IP,选择使用 FRP(Fast Reverse Proxy)这款优秀的内网穿透工具来实现。
本文将完整记录从配置到部署的全过程,以及踩过的坑和解决方案。
环境说明
公网服务器:阿里云 ECS,系统 Ubuntu
本地客户端:Windows 10,运行 DVWA 的 Docker 容器
FRP 版本:v0.69.1
目标:本地 8080 端口的 DVWA 映射到公网服务器的 8080 端口
一、服务端配置
1.1 下载与安装
# 下载 FRP
wget https://github.com/fatedier/frp/releases/download/v0.69.1/frp_0.69.1_linux_amd64.tar.gz
tar -zxvf frp_0.69.1_linux_amd64.tar.gz
cd frp_0.69.1_linux_amd641.2 配置 frps.toml
FRP 0.69.1 使用 TOML 格式配置,服务端配置如下:
# frps.toml
bindAddr = "0.0.0.0"
bindPort = 7000
# 管理页面仅本地访问
webServer.addr = "127.0.0.1"
webServer.port = 7500
webServer.user = "admin"
webServer.password = "your_secure_password"
# 开启 Prometheus 监控
enablePrometheus = true
# Token 认证,防止未授权访问
auth.token = "your_secure_token"配置说明:
bindAddr = "0.0.0.0":监听所有 IPv4 地址,这一步很重要,后面会提到踩坑经历
webServer.addr = "127.0.0.1":Dashboard 仅监听本地,确保管理页面只能从服务器本地访问
auth.token:客户端与服务端通信的认证令牌
二、服务端进程守护
2.1 使用 Systemd 管理 FRP 服务
创建 systemd 服务文件,实现开机自启和进程守护:
bash
sudo vim /etc/systemd/system/frps.service
ini
[Unit]
Description=Frp Server
After=network.target
[Service]
Type=simple
User=root
ExecStart=/root/frp/frps -c /root/frp/frps.toml
Restart=on-failure
RestartSec=5s
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target2.2 启动与管理命令
# 重载 systemd 配置
sudo systemctl daemon-reload
# 启动 frps
sudo systemctl start frps
# 设置开机自启
sudo systemctl enable frps
# 查看运行状态
sudo systemctl status frps
# 查看实时日志
sudo journalctl -u frps -f
# 重启服务
sudo systemctl restart frps2.3 遇到的坑:Systemd 启动失败
问题 1:status=203/EXEC
错误日志显示无法执行文件,通常是权限或路径问题。
解决方法:
确认 frps 文件有执行权限:chmod +x /root/frp/frps
确保 systemd 中 User=root,不要使用 nobody
简化 systemd 配置,去掉不必要的 WorkingDirectory 和 ExecReload 字段
问题 2:status=200/CHDIR
表示 systemd 无法切换到指定的工作目录。
解决方法:
去掉 WorkingDirectory 配置项,或确认目录路径正确存在
三、客户端配置
3.1 Windows 客户端
下载 Windows 版 FRP,创建 frpc.toml:
toml
serverAddr = "your_server_ip"
serverPort = 7000
auth.token = "your_secure_token"
[[proxies]]
name = "dvwa-web"
type = "tcp"
localIP = "127.0.0.1"
localPort = 8080
remotePort = 80803.2 运行客户端
# 直接运行
frpc.exe -c frpc.toml
# 安装为 Windows 服务(以管理员身份运行)
frpc.exe install -c frpc.toml
frpc.exe start
# 卸载服务
frpc.exe stop
frpc.exe uninstall四、踩坑记录与解决方案
坑 1:客户端连接超时(i/o timeout)
现象:
connect to server error: dial tcp x.x.x.x:7000: i/o timeout排查过程:
检查服务端监听状态
bash
sudo ss -tlnp | grep 7000发现输出显示 tcp6,只监听了 IPv6,而客户端使用 IPv4 连接。
添加 bindAddr = "0.0.0.0"
在 frps.toml 中显式指定绑定到 0.0.0.0,确保同时监听 IPv4。
检查云服务商安全组
即使修改了监听地址,仍然超时。最终确认是阿里云安全组未放行对应端口。
最终解决方案:
登录云服务商控制台
进入安全组配置
添加入方向规则:TCP 协议,端口 7000 和 8080,授权对象 0.0.0.0/0
保存后立即生效,无需重启服务器
坑 2:Systemd 服务启动失败
错误:status=203/EXEC
这个错误表示 systemd 无法执行 ExecStart 指定的命令。
排查步骤:
bash
# 1. 确认文件存在且有执行权限
ls -la /root/frp/frps
chmod +x /root/frp/frps
# 2. 手动测试命令是否能运行
/root/frp/frps -c /root/frp/frps.toml
# 3. 检查 systemd 文件配置
# User 不能使用 nobody,需要 root 权限
错误:status=200/CHDIRbash
# 确认工作目录是否存在
ls -ld /root/frp
# 去掉或修正 WorkingDirectory 配置坑 3:防火墙与安全组的区别
很多新手(包括当时的我)容易混淆:
系统防火墙(iptables/ufw):服务器操作系统层面的防火墙
云安全组:云服务商提供的虚拟防火墙,在操作系统之外
即使系统防火墙放行了端口,云安全组未配置同样无法访问。务必两者都检查。
五、验证连接
5.1 服务端验证
# 查看 frps 日志,确认客户端连接
sudo journalctl -u frps -f
# 查看端口监听
sudo ss -tlnp | grep -E "7000|8080"5.2 客户端验证
# Windows PowerShell 测试端口
Test-NetConnection your_server_ip -Port 7000
Test-NetConnection your_server_ip -Port 80805.3 功能验证
浏览器访问 http://your_server_ip:8080,应该能看到 DVWA 登录页面。
六、安全建议
使用强 Token:auth.token 使用复杂随机字符串
Dashboard 仅本地访问:webServer.addr = "127.0.0.1"
最小化端口暴露:安全组只放行必要的端口
定期更新:关注 FRP 版本更新,及时升级
日志监控:定期查看日志,发现异常连接及时处理
七、常用命令速查
# 服务端
sudo systemctl start frps # 启动
sudo systemctl stop frps # 停止
sudo systemctl restart frps # 重启
sudo systemctl status frps # 状态
sudo journalctl -u frps -f # 日志
# 客户端(Windows CMD)
frpc.exe -c frpc.toml # 前台运行
frpc.exe install -c frpc.toml # 安装服务
frpc.exe start # 启动服务
frpc.exe stop # 停止服务
frpc.exe uninstall # 卸载服务总结
FRP 是一款非常强大的内网穿透工具,配置相对简单。在实际部署中,大部分问题都集中在网络层面(安全组、防火墙、IPv4/IPv6),而非 FRP 本身。
遇到连接问题时,建议按以下顺序排查:
确认服务端正常运行
检查端口监听状态(IPv4/IPv6)
检查系统防火墙
检查云服务商安全组
使用 telnet 或 Test-NetConnection 逐段测试连通性
评论0
暂时没有评论