> 家里有台电脑想当服务器用,但没有公网 IP,怎么办?或者公司内网的服务想在外面访问?frp 就是解决这个问题的神器——通过一台有公网 IP 的 VPS 做中转,把内网服务暴露到公网。
---
## frp 是什么?
frp(Fast Reverse Proxy)是一个高性能的内网穿透工具。架构很简单:
- **frps**:服务端,部署在有公网 IP 的 VPS 上
- **frpc**:客户端,部署在内网机器上
用户访问 VPS 的某个端口,frp 自动把流量转发到内网机器的对应端口。
---
## 第一步:在 VPS 上部署 frps(服务端)
```bash
cd /opt
wget https://github.com/fatedier/frp/releases/download/v0.52.0/frp_0.52.0_linux_amd64.tar.gz
tar -xzf frp_0.52.0_linux_amd64.tar.gz
mv frp_0.52.0_linux_amd64 frp
cd frp
```
编辑 frps.toml:
```toml
bindPort = 7000
auth.token = "一个复杂的随机字符串"
```
启动服务端:./frps -c frps.toml
配置成系统服务开机自启:
```bash
cat > /etc/systemd/system/frps.service << 'EOF'
[Unit]
Description=frp server
After=network.target
[Service]
Type=simple
ExecStart=/opt/frp/frps -c /opt/frp/frps.toml
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable frps --now
```
---
## 第二步:在内网机器上部署 frpc(客户端)
编辑 frpc.toml:
```toml
serverAddr = "你的VPS的IP"
serverPort = 7000
auth.token = "与服务器相同的token"
[[proxies]]
name = "web"
type = "tcp"
localIP = "127.0.0.1"
localPort = 80
remotePort = 8080
```
启动:./frpc -c frpc.toml
---
## 常见使用场景
| 场景 | localPort | remotePort |
|------|-----------|------------|
| 内网 Web 服务 | 80/8080 | 8080 |
| 远程桌面(RDP) | 3389 | 3389 |
| SSH 访问内网机器 | 22 | 2222 |
| 数据库连接 | 3306 | 3306 |
---
## 安全注意事项
1. token 要设复杂点,防止被恶意连接
2. 只映射必要的端口,不要全端口暴露
3. 配合防火墙使用,限制来源 IP
4. 使用 HTTPS,在 VPS 端配置 Nginx + SSL
---
frp 是内网穿透领域的明星项目,稳定、高效、配置简单。有了它,一台轻量云服务器 + 家里的电脑,就能搭建各种有意思的服务。