折腾博客

学习不止 · 技术与软件分享

Debian/Ubuntu Docker 部署 Mihomo + Web 面板教程

发布时间:

发布者:

分类:

阅读数:

82

点赞数:

1

评论数:

0

阅读时长:

22–32 分钟

阅读字数:

5,098字

最后编辑于:

前言:

在 PVE 环境里刚好有一台 Debian Server,一直作为备用系统放在那里。

现在的架构是 iKuai 负责主路由与流量调度, OpenWrt 作为辅助出口。但问题也很明显,一旦 OpenWrt 出现异常崩掉,整个网络就会瘫痪。

所以就想到把这台 Debian 利用起来,作为一个独立的兜底节点,用来在 OpenWrt 异常时切换到 Debian 提供备用上网能力,避免整个网络完全瘫痪。

本文基于 Debian 环境,通过 Docker 部署 Mihomo(MetaCubeX),配置机场订阅、安装 Web 面板,并通过 TUN 模式接管系统流量,实现宿主机透明代理。

适用环境:

  • Debian 11
  • Debian 12
  • Ubuntu 22.04
  • Ubuntu 24.04

部署方式:

  • Docker
  • Docker Compose

用途:

  • 给 Debian 宿主机做代理
  • 给 Docker 容器做代理
  • 给局域网设备做代理

Debian 与 Ubuntu 网络结构、Docker 与 systemd 基本一致,因此本文步骤通用。

前提:

系统已安装:

  • Docker
  • Docker Compose

Docker 安装可直接在本站搜索关键字“Docker”搜索相关教程。

一、创建目录

创建工作目录:

mkdir -p /container/mihomo/config
cd /container/mihomo

目录结构:

  • /container/mihomo/
  • ├── docker-compose.yml
  • └── config

二、创建 docker-compose.yml

创建:

nano docker-compose.yml

写入:

services:

  mihomo:

    image: metacubex/mihomo:latest

    container_name: mihomo

    restart: unless-stopped


    network_mode: host


    privileged: true

    cap_add:
      - NET_ADMIN

    devices:
      - /dev/net/tun:/dev/net/tun


    volumes:
      - ./config:/root/.config/mihomo


    command:
      - -d
      - /root/.config/mihomo

说明:

使用:
network_mode: host
表示:
Mihomo 与宿主机共用网络。

无需:
ports:
端口映射。

开启:
privileged: true
NET_ADMIN
/dev/net/tun
用于支持:
tun:
enable: true
否则透明代理无法正常工作。

保存:

Ctrl + O

回车

Ctrl + X

三、启动容器

启动:

docker compose up -d

查看:

docker ps

metacubex/mihomo:latest → Up

日志:

docker logs mihomo --tail 20

如果有报错,查看步骤是否有错误,或是搜索引擎查询。

首次启动后:

ls /container/mihomo/config

会自动生成:

类似:

  • config.yaml
  • Country.mmdb
  • geoip.metadb

说明容器初始化成功。

四、编辑 config.yaml

打开:

nano /container/mihomo/config/config.yaml

替换为以下配置。

以下为个人运行配置,保持原样,仅需修改订阅地址与面板路径,熟悉了可以自行修改。

# ====================
# 代理端口
# ====================

port: 7890
socks-port: 7891
mixed-port: 7892

redir-port: 7893
tproxy-port: 9898


# ====================
# 基础配置
# ====================

allow-lan: true
bind-address: "*"

mode: rule
log-level: info

ipv6: false

unified-delay: true
tcp-concurrent: true

keep-alive-idle: 600
keep-alive-interval: 15

global-client-fingerprint: chrome


# ====================
# Web 控制器
# ====================

external-controller: 0.0.0.0:9090

# 注意修改
external-ui: /root/.config/mihomo/ui/zd

secret: ""


# ====================
# Profile
# ====================

profile:
  store-selected: true
  store-fake-ip: true


# ====================
# 机场订阅
# ====================

proxy-providers:

  airport1:

    type: http

    url: "填写自己的订阅"

    interval: 86400

    health-check:

      enable: true

      url: https://www.gstatic.com/generate_204

      interval: 300


# ====================
# 本地节点
# ====================

proxies:

#  - name: DIRECT
#    type: direct
#    udp: true


# ====================
# 策略组
# ====================

proxy-groups:

  - name: PROXY

    type: select

    proxies:

      - AUTO
      - FALLBACK
      - DIRECT


  - name: AUTO

    type: url-test

    url: https://www.gstatic.com/generate_204

    interval: 300

    tolerance: 150

    include-all: true


  - name: FALLBACK

    type: fallback

    url: https://www.gstatic.com/generate_204

    interval: 300

    include-all: true


  - name: LOAD-BALANCE

    type: load-balance

    strategy: round-robin

    url: https://www.gstatic.com/generate_204

    interval: 300

    include-all: true


# ====================
# 规则
# ====================

rules:

  - DOMAIN-SUFFIX,cn,DIRECT

  - GEOIP,CN,DIRECT

  - DOMAIN-SUFFIX,google.com,PROXY
  - DOMAIN-SUFFIX,youtube.com,PROXY
  - DOMAIN-SUFFIX,github.com,PROXY

  - MATCH,PROXY


# ====================
# TUN
# ====================

tun:

  enable: true

  stack: mixed

  dns-hijack:

    - any:53

  auto-route: true

  auto-redirect: true

  auto-detect-interface: true


# ====================
# DNS
# ====================

dns:

  enable: true

  listen: 0.0.0.0:53

  enhanced-mode: fake-ip

  fake-ip-range: 198.18.0.1/16


  default-nameserver:

    - 223.5.5.5
    - 119.29.29.29


  nameserver:

    - https://dns.alidns.com/dns-query
    - https://doh.pub/dns-query


  fallback:

    - 1.1.1.1
    - 8.8.8.8


# ====================
# Sniffer
# ====================

sniffer:

  enable: true

  sniff:

    TLS:
      ports: [443,8443]

    HTTP:
      ports: [80,8080-8880]

      override-destination: true


# ====================
# iptables
# ====================

iptables:

  enable: false

  inbound-interface: eth0

仅需修改:

  • proxy-providers:
  • 订阅地址
  • external-ui:
  • 面板路径

保存:

Ctrl + O

回车

Ctrl + X

五、安装 Web 面板

由于:
volumes:
./config:/root/.config/mihomo

因此:
宿主机:
/container/mihomo/config

等于容器:
/root/.config/mihomo

不用进入容器安装面板。

创建目录:

mkdir -p /container/mihomo/config/ui/zd
mkdir -p /container/mihomo/config/ui/mc

安装 MC 面板(MetaCubeXD)

MetaCubeXD

特点:

  • 官方维护
  • 功能最完整
  • 日志/连接显示丰富
  • 目前最常见

安装最新版:

wget -O /tmp/mc.tgz \
https://github.com/MetaCubeX/metacubexd/releases/latest/download/compressed-dist.tgz

tar -zxvf /tmp/mc.tgz \
-C /container/mihomo/config/ui/mc

查看:

ls /container/mihomo/config/ui/mc

出现:

  • _nuxt/
  • _fonts/
  • index.html
  • 200.html
  • config.js

说明安装成功。

安装 ZD 面板(Zashboard)

Zashboard

特点:

  • 界面现代
  • 布局简洁
  • 移动端体验好

下载最新版:

wget -O /tmp/zd.zip \
https://github.com/Zephyruso/zashboard/releases/latest/download/dist.zip

解压:

apt install unzip -y

unzip /tmp/zd.zip \
-d /container/mihomo/config/ui/zd

查看:

ls /container/mihomo/config/ui/zd

出现:

  • assets/
  • index.html
  • favicon.ico
  • manifest.webmanifest

说明安装成功。

六、多面板切换

查看:

nano /container/mihomo/config/config.yaml

当前:
external-ui:
/root/.config/mihomo/ui/zd
表示启用:

zd
若切换:
external-ui:
/root/.config/mihomo/ui/mc

重启:

docker restart mihomo

即可切换面板。

查看日志:

docker logs mihomo --tail 20

若出现:
RESTful API listening at: [::]:9090
Mixed(http+socks) proxy listening at:
说明正常。

七、访问面板

假设服务器 IP:
192.168.1.100

打开:
http://192.168.1.100:9090/ui/

注意:

不是:
http://192.168.1.100:9090

首次连接:

后端:
http://192.168.1.100:9090
密钥:
留空
点击:
添加
连接成功。

八、验证代理是否正常

由于本文使用:

  • network_mode: host
  • tun:
  • enable: true

Mihomo 会接管宿主机网络。

测试访问 Google:

curl -I https://google.com

正常返回:
类似:
HTTP/1.1 301 Moved Permanently
或:
HTTP/2 200
说明系统已具备访问能力。

继续检查出口:

curl ip.sb

返回国外 IP:
例如:
74.xx.xx.xx

说明:宿主机

Mihomo TUN

机场节点

国外出口
代理已经生效。

同时可观察面板:

  • 连接数
  • 上传
  • 下载
  • 日志

执行 curl 时出现变化,也说明流量已经过 Mihomo。

九、总结

至此,Mihomo 已部署完成。

以后仅需维护订阅或调整 config.yaml 配置,重启容器即可生效。

节点切换、规则查看、流量监控等操作,可直接通过 Web 面板完成。

部署完成后,基本无需频繁维护,保持订阅有效即可长期使用。

赞助我们
微信赞助码
支付宝赞助码
分享到:

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注