因为最近想在服务器上部署需要调用到 chatgpt api 的项目,所以就有了服务器走代理的需求。下面记录部署过程,以便后续忘记。

相信这个教程对想 在无GUI Linux 服务器上实现走代理 的同学会有所帮助~

准备

部署

  1. 打开Ubuntu服务器终端

  2. 切换至root用户

    1
    sudo su
  3. 新建文件夹(clash软件默认访问的路径)

    1
    mkdir -p /root/.config/clash && cd /root/.config/clash
  4. 上传订阅链接配置文件

    1
    wget "机场给你的订阅链接" -O config.yaml
  5. 上传上述两个压缩包

  6. 解压压缩包,赋予权限,重命名

    1
    2
    3
    4
    5
    6
    gzip -d clash-linux-amd64-v1.11.12.gz
    tar -xf yacd.tar.xz

    mv clash-linux-amd64-v1.11.12 clash
    chmod +x clash
    mv public dashboard
  7. 更改配置文件使其加载dashboard

    1
    2
    3
    4
    5
    6
    7
    8
    9
    port: 7890
    socks-port: 7891
    allow-lan: true
    mode: Rule
    log-level: debug //输出更多的日志信息,以便观察clash的运行状况
    secert: 123456 // 增加这一行, 如果你希望你的clash
    web要密码访问可以在这块配置密码, 如果不需要直接注释掉即可
    external-ui: dashboard // 增加这一行
    external-controller: 0.0.0.0:9090
  8. 验证是否可以有效执行

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ./clash -d .
    ------------Output------------
    INFO[0000] Start initial compatible provider Proxies
    INFO[0000] Start initial compatible provider Spotify
    INFO[0000] Start initial compatible provider Microsoft
    INFO[0000] Start initial compatible provider GlobalTV
    INFO[0000] Start initial compatible provider Apple
    INFO[0000] Start initial compatible provider Steam
    INFO[0000] RESTful API listening at: [::]:9090
    INFO[0000] HTTP proxy listening at: [::]:7890
    INFO[0000] SOCKS proxy listening at: [::]:7891
  9. 开放相关端口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ufw allow 9090
    ufw enable
    ufw reload
    ufw status

    #7890端口是代理端口,不开放也可以?不确定
    ufw allow 7890
    ufw enable
    ufw reload
    ufw status
  10. 在外网通过服务器ip:9090/ui可以访问dashboard,选择线路

  11. 设置开机自启动

    1
    2
    3
    mv clash /usr/local/bin

    vim /etc/systemd/system/clash.service
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [Unit]
    Description=clash service
    After=network.target

    [Service]
    Type=simple
    User=root
    ExecStart=/usr/local/bin/clash
    Restart=on-failure # or always, on-abort, etc

    [Install]
    WantedBy=multi-user.target

    然后使其生效

    1
    2
    3
    4
    systemctl daemon-reload
    systemctl enable clash
    systemctl start clash
    systemctl status clash
  12. 配置bash

    1
    vim ~/.bashrc
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    # Proxy auto start
    export https_proxy=http://127.0.0.1:7890
    export http_proxy=http://127.0.0.1:7890
    export all_proxy=socks5://127.0.0.1:7890
    # Open proxy
    on() {
    # export https_proxy=http://127.0.0.1:1087
    # export http_proxy=http://127.0.0.1:1087
    # export all_proxy=socks5://127.0.0.1:1086
    export https_proxy=http://127.0.0.1:7890
    export http_proxy=http://127.0.0.1:7890
    export all_proxy=socks5://127.0.0.1:7890
    echo "HTTP/HTTPS Proxy on"
    }

    # Close proxy
    off() {
    unset http_proxy
    unset https_proxy
    unset all_proxy
    echo "HTTP/HTTPS Proxy off"
    }
    1
    source ~/.bashrc
    1
    2
    3
    4
    # 启动代理
    on
    # 关闭代理
    off
  13. 测试

    1
    curl youtube.com

注意

  1. 走代理的是https和http,ping是无法ping通的,所以用curl和wget测试是否部署成功
  2. 按上述过程部署后,需要在root用户下启动代理
  3. 启动代理后,只是当前这个命令行操作可以走代理,运行在这个命令行下的程序也可以走代理

参考

https://parrotsec-cn.org/t/linux-clash-dashboard/5169

https://uestc.feishu.cn/wiki/LOZVwBcNciHdJIkeLDlcwSWdnMf

https://www.hengy1.top/article/3dadfa74.html