Contents

从源码手动编译安装 Xray:最干净的跨境业务加速方式(VLESS + REALITY)

0. 前言

目前网络上关于 Xray 的安装教程大多数都是一键脚本,这虽然方便,但也带来许多冗余配置和潜在安全隐患。
如果你是一个对系统整洁度要求较高的极客,不妨试试 从源码手动编译安装 Xray,只安装自己真正需要的部分,稳定、快速、防封更胜一筹。

本文以 Debian 12.9 为例,使用最新版本的 Golang 编译 Xray-core,并部署 VLESS + REALITY 的最简服务端配置。


1. 实验环境

VPS 服务商 数据中心 操作系统 规格配置 流量及带宽 测评链接
搬瓦工 美国洛杉矶 DC9 Debian 12.9 2C/1G/20G 1T@2.5Gbps 更详细测评

搬瓦工 BandwagonHost USCA_9 线路简介

运营商 去程 回程
电信 CN2 GIA CN2 GIA
联通 9929 CN2 GIA
移动 CMIN2 CMIN2

2. 安装基础依赖包

sudo apt update -y && sudo apt upgrade -y && sudo apt install git vim curl nano -y

这些工具主要用于文件编辑、下载与基础调试,属于最小必要依赖。


3. 安装 Golang(Go 语言编译器)

进入官方文档查看安装说明:👉 https://go.dev/doc/install 官方 Golang 最新发行包: 👉 https://go.dev/dl/

cd /opt/
curl -LO https://go.dev/dl/go1.25.5.linux-amd64.tar.gz
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.25.5.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source ~/.bashrc

✅ 如果你的 VPS 是 ARM 架构,请使用 go1.25.5.linux-arm64.tar.gz


4. 编译 Xray-core

4.1 拉取源码

git clone https://github.com/XTLS/Xray-core.git
cd Xray-core && go mod download

4.2 构建可执行文件

CGO_ENABLED=0 go build -o xray -trimpath -ldflags "-s -w -buildid=" ./main
sudo mv xray /usr/local/bin

5. 配置 Xray 服务端(VLESS + REALITY)

5.1 创建配置文件目录与文件

sudo mkdir -p /etc/xray
sudo nano /etc/xray/config.json

5.2 推荐配置模板(请根据提示修改)

{
  "log": {
    "loglevel": "warning"
  },
  "inbounds": [
    {
      "port": 你的端口,
      "protocol": "vless",
      "settings": {
        "clients": [
          {
            "id": "你的UUID",
            "flow": "xtls-rprx-vision"
          }
        ],
        "decryption": "none"
      },
      "streamSettings": {
        "network": "tcp",
        "security": "reality",
        "realitySettings": {
          "dest": "www.bing.com:443",
          "serverNames": ["www.bing.com", "bing.com"],
          "privateKey": "你的PrivateKey",
          "shortIds": ["你的ShortID"]
        }
      },
      "sniffing": {
        "enabled": true,
        "destOverride": ["http", "tls", "quic"]
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "freedom",
      "tag": "direct"
    },
    {
      "protocol": "blackhole",
      "tag": "block"
    }
  ]
}

🔧 参数生成

  • port: 修改成你喜欢的端口,建议实验 443,伪装性更好
  • UUIDxray uuid
  • PrivateKeyxray x25519
  • ShortID(8 位 hex)openssl rand -hex 8

6. 创建 systemd 管理服务

sudo nano /etc/systemd/system/xray.service

填入以下内容:

[Unit]
Description=Xray Service
Documentation=https://github.com/xtls
After=network.target nss-lookup.target

[Service]
User=root
NoNewPrivileges=true
ExecStart=/usr/local/bin/xray run -config /etc/xray/config.json
Restart=on-failure
RestartPreventExitStatus=23

[Install]
WantedBy=multi-user.target

启动服务

sudo systemctl daemon-reexec
sudo systemctl enable --now xray

查看运行状态

sudo systemctl status xray --no-pager

若服务未启动成功,查看日志:

sudo journalctl -u xray -n 80 --no-pager

7. 常用命令汇总

动作 命令
启动 sudo systemctl start xray
停止 sudo systemctl stop xray
重启 sudo systemctl restart xray
状态查看 sudo systemctl status xray --no-pager
查看日志 sudo journalctl -u xray -n 80 --no-pager

8. 总结

你只需要三个文件,即可完成简洁干净的科学上网服务部署:

  • /usr/local/bin/xray — 编译后的主程序
  • /etc/xray/config.json — 配置文件
  • /etc/systemd/system/xray.service — 管理服务脚本

相比一键脚本安装方式,这种从源码编译的方式,更透明、更纯净,非常适合追求控制感和极致优化的玩家。


9. 拓展阅读