1. 创建 Mihomo 目录
mkdir -p /etc/mihomo
cd /etc/mihomo
2. 下载 Mihomo
你的服务器是 x86_64,下载对应 amd64 版本:
wget -O mihomo.gz https://github.com/MetaCubeX/mihomo/releases/download/v1.19.30/mihomo-linux-amd64-v1-v1.19.30.gz
解压:
gzip -d mihomo.gz
chmod +x mihomo
ln -sf /etc/mihomo/mihomo /usr/local/bin/mihomo
检查:
mihomo -v
应该显示类似:
Mihomo Meta v1.19.30 linux amd64
3. 下载 Clash 订阅
不要把真实订阅链接公开,自己替换:
curl -L "你的订阅链接" -o /etc/mihomo/config.yaml
测试配置:
mihomo -t -d /etc/mihomo
第一次运行可能会自动下载 GeoIP/MMDB 等文件,属于正常情况。
4. 安装 MetaCubeXD WebUI
git clone https://github.com/MetaCubeX/metacubexd.git -b gh-pages /etc/mihomo/ui
然后编辑:
vi /etc/mihomo/config.yaml
确保有:
external-controller: 127.0.0.1:9090
external-ui: /etc/mihomo/ui
建议同时设置一个强密码:
secret: "你自己的强密码"
这里保持:
127.0.0.1:9090
不要为了 WebUI 改成 0.0.0.0:9090。
5. 创建 systemd 服务
cat >/etc/systemd/system/mihomo.service <<'EOF'
[Unit]
Description=Mihomo Proxy Service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=root
WorkingDirectory=/etc/mihomo
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo
Restart=on-failure
RestartSec=5
LimitNOFILE=1048576
[Install]
WantedBy=multi-user.target
EOF
启动并设置开机启动:
systemctl daemon-reload
systemctl enable --now mihomo
检查:
systemctl status mihomo
6. 检查端口
ss -lntup | grep -E ':(7890|7891|9090)\b'
正常可以看到类似:
127.0.0.1:7890
127.0.0.1:7891
127.0.0.1:9090
测试 WebUI:
curl -s http://127.0.0.1:9090/ui/ | head
出现:
<title>MetaCubeXD</title>
说明 Mihomo + WebUI 已经正常。
7. 宝塔创建反向代理站点
例如域名:
clash2.noyoru.top
反向代理目标:
http://127.0.0.1:9090
核心 Nginx 配置实际上就是:
location / {
proxy_pass http://127.0.0.1:9090;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
测试:
nginx -t
然后重载:
systemctl reload nginx
如果宝塔自己的 Nginx 服务管理方式不同,也可以直接在宝塔面板重载。
8. 阿里云安全组放行
这是我们这次实际遇到的问题。
至少放行:
TCP 80
TCP 443
不要开放:
7890
7891
9090
因为 9090 通过 Nginx 反代即可,没有必要直接暴露公网。
此时:
http://clash2.noyoru.top/ui/
应该可以访问。
9. 配置 HTTPS
在宝塔:
网站 → clash2.noyoru.top → SSL
部署你的证书。
然后确认 Nginx 已经有类似:
listen 443 ssl;
server_name clash2.noyoru.top;
ssl_certificate ...
ssl_certificate_key ...
检查:
ss -lntp | grep ':443'
确认有 Nginx 监听 443。
同时再次确认阿里云安全组开放 TCP 443。
然后访问:
https://clash2.noyoru.top/ui/
10. 配置 MetaCubeXD 后端
如果 WebUI 能打开,但是提示:
后端无法连接
MetaCubeXD 后端地址不要填写:
http://127.0.0.1:9090
因为浏览器中的 127.0.0.1 是你的电脑。
使用 Nginx 反代后的域名:
https://clash2.noyoru.top
然后 Secret 填:
config.yaml 中 secret 的值
可以先测试 API:
curl https://clash2.noyoru.top/version
如果返回:
{"message":"Unauthorized"}
说明反代其实已经成功,只是需要 Secret。
带 Secret 测试:
curl \
-H "Authorization: Bearer 你的Secret" \
https://clash2.noyoru.top/version
能返回 Mihomo 版本信息,就说明整条链路完全正常。
最终架构就是:
┌─ 7890 代理
订阅 → Mihomo ───────┼─ 7891 SOCKS
│
└─ 127.0.0.1:9090
↑
Nginx 反代
↑
HTTPS :443
↑
clash2.noyoru.top
↑
浏览器
↑
MetaCubeXD
最关键的三个安全原则就是:公网只开放 80/443;9090 保持 127.0.0.1;Mihomo API 设置强 secret。