内网穿透新方案:基于 PVE + BT面板 + Cloudflare Tunnel 搭建高安全性个人博客
在没有公网 IP 的环境下搭建个人博客,传统的端口转发(Port Forwarding)往往面临安全风险。本文将分享一种利用 Cloudflare Tunnel (Argo Tunnel) 实现零信任(Zero Trust)架构的方案,让你的内网服务器在不暴露 IP 的情况下,安全地发布到公网。 ## 一、 核心准备工作
在开始部署前,请确保你已准备好以下资源: – 国际域名: 推荐注册 <code>.top</code> 或 <code>.com</code> 等主流后缀。建议选择提供免费 WHOIS 隐私保护 的注册商(如 Namesilo),以保护个人信息。
- Cloudflare 账号: 将你的域名 DNS 解析托管至 Cloudflare。
- 本地服务器环境: 本教程基于 Proxmox VE (PVE) 虚拟化平台,容器系统使用 Debian 12,并预装 宝塔面板 (BT Panel)。
- 终端工具: 熟练使用 SSH 工具(如 Termius、iTerm2 或 Warp)。
二、 PVE 环境优化与系统配置
为了提升在特定网络环境下的访问速度,我们需要对 Debian 容器进行初步优化。 ### 1. 替换 PVE 容器模板源
进入 PVE 宿主机后台,将模板源更换为国内镜像站(以清华源为例):
perl -i -pe 's|url\s*=>\s*".*?"|url => "https://mirrors.tuna.tsinghua.edu.cn/proxmox/images"| if $in_block; $in_block = 1 if /host\s*=>\s*"download\.proxmox\.com"/; $in_block = 0 if /\}/' /usr/share/perl5/PVE/APLInfo.pm && pveam update
2. Debian 12 系统初始化
创建 CT 虚拟机并登录后,首先开启 SSH 远程登录权限:
“`bash
sed -i 's/^#Port 22/Port 22/' /etc/ssh/sshd_config
sed -i 's/^#PermitRootLogin/PermitRootLogin/' /etc/ssh/sshd_config
sed -i 's/^#PasswordAuthentication/PasswordAuthentication/' /etc/ssh/sshd_config
sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
reboot</p>
<pre><code>
### 3. 更新系统软件包源
替换为 Debian 12 (Bookworm) 的清华镜像源:
```bash
cp /etc/apt/sources.list /etc/apt/sources.list.bak && \
echo "备份原有源为 /etc/apt/sources.list.bak"
cat > /etc/apt/sources.list <<EOF
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
EOF
apt update</code></pre>
<hr />
<h2>三、 部署 Web 服务环境</h2>
<h3>1. 安装宝塔面板</h3>
<p>使用宝塔官方提供的 LTS 稳定版安装脚本:</p>
<pre><code class="language-bash">wget -O install.sh https://download.bt.cn/install/install_lts.sh
bash install.sh</code></pre>
<h3>2. 构建 LNMP 架构</h3>
<p>登录宝塔面板后,选择 <strong>LNMP (Linux + Nginx + MySQL + PHP)</strong> 环境进行极速安装。</p>
<h3>3. 创建 WordPress 站点</h3>
<p>在面板中添加站点,填入你的域名。下载官方 WordPress 源码并解压至站点根目录。<strong>注意:此时只需完成文件放置,暂不进行数据库初始化配置。</strong></p>
<hr />
<h2>四、 核心环节:Cloudflare Tunnel 配置</h2>
<p>Cloudflare Tunnel 充当了内网与公网之间的“隐形电缆”。 ### 1. 安装 Cloudflared 客户端</p>
<p>根据你的架构下载对应的 `.deb` 包并安装: </p>
<p>```bash
wget <a href="https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb">https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb</a>
dpkg -i cloudflared-linux-amd64.deb cloudflared –version</p>
<pre><code>
### 2. 身份认证与 Tunnel 创建
执行登录命令,点击终端输出的 URL 跳转至浏览器完成授权:
```bash
cloudflared login</code></pre>
<p>授权成功后,创建一个新的 Tunnel(建议以域名命名):</p>
<pre><code class="language-bash"># 替换 <tunnel-name> 为你的自定义名称
cloudflared tunnel create <tunnel-name></code></pre>
<h3>3. 配置 Ingress 路由规则</h3>
<p>在 `/root/.cloudflared` 目录下创建 `config.yml` 文件,定义流量转发逻辑:
“`yaml
tunnel: <你的Tunnel-ID> credentials-file: /root/.cloudflared/<你的Tunnel-ID>.json
ingress: <ul> <li>hostname: www.yourdomain.com service: <a href="http://localhost:80">http://localhost:80</a></li> <li>hostname: yourdomain.com service: <a href="http://localhost:80">http://localhost:80</a></li> <li>service: http_status:404</li></ul>
### 4. 设置 DNS 解析与服务启动
通过以下命令自动在 Cloudflare 后台生成 CNAME 记录,并启动服务:
```bash
# 绑定域名
cloudflared tunnel route dns <tunnel-name> yourdomain.com
#安装并启动系统服务
cloudflared service install
systemctl enable cloudflared
systemctl start cloudflared
五、 安全增强与后记
开启全站 HTTPS
在 Cloudflare 后台的 SSL/TLS -> Edge Certificates 中,开启 Always Use HTTPS。由于 Tunnel 本身是加密的,你的源站甚至不需要配置 SSL 证书。 ### 为什么选择 Zero Trust(零信任)?
传统的内网穿透需要你手动打开路由器防火墙端口,这等于给黑客留了一扇窗。而 Cloudflare Tunnel 的逻辑是: 1. 主动出站: 由内网服务器主动发起连接,外部无法探测到你的真实 IP。
- 默认不信任: 只有经过 Cloudflare 节点验证的流量才能进入 Tunnel。
- 最小权限: 仅暴露 <code>config.yml</code> 中定义的特定服务端口。
小贴士: 博客安装完成后,请务必在 WordPress 后台中将“站点地址”和“WordPress 地址”修改为你的公网域名,否则可能会出现样式丢失或无法登录的问题。
祝你的博客运行愉快!
