📢 订阅 Telegram Linux Server 频道 第一时间获取最新 VPS 和科学上网技术资讯。

前言

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

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


一、系统环境

  • VPS 服务商BandwagonHost
  • 产品方案: The DC9 Plan
  • 系统版本:Debian 12.9
  • 服务器配置:1 核 / 768MB RAM / 15GB SSD / 750GB 月流量 / 1.5Gbps 带宽
  • 线路:电信 CN2/CN2、移动 CMIN2/CMIN2、联通 9929/CN2
  • 服务器位置:美国洛杉矶

二、安装基础工具

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

三、安装 Golang(Go 语言编译器)

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

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

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


四、编译 Xray-core

1. 拉取源码

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

2. 构建可执行文件

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

五、配置 Xray 服务端(VLESS + REALITY)

1. 创建配置文件目录与文件

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

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: 修改成你喜欢的端口,建议五位数以上,比如 11983
  • UUID: xray uuid
  • PrivateKey: xray x25519
  • ShortID(8位 hex): openssl rand -hex 8

六、创建 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

七、常用命令汇总

动作命令
启动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

八、总结

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

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

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

如果你觉得这篇文章对你有帮助,欢迎留言、点赞、转发,这将是我继续写作最大的动力 ❤️
迎关注并订阅 Telegram Linux Server 频道