前提:
一台干净的vps,系统为debian12,无端口占用,ip正常
从开始到最后,请不要断开会话连接

安装Golang

下载安装包

wget https://golang.org/dl/go1.22.5.linux-amd64.tar.gz

解压到/usr/local目录

sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz

添加临时环境变量(仅当前会话)

export PATH=$PATH:/usr/local/go/bin

验证安装

 go version

拉取xray源代码

安装git

sudo apt install git

验证安装

git --version

克隆仓库及下载依赖

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

构建二进制

CGO_ENABLED=0 go build -o xray -trimpath -ldflags "-s -w -buildid=" ./main

下载geoip和geosite文件

拉取文件

sudo wget -O /root/Xray-core/geoip.dat https://github.com/v2fly/geoip/releases/latest/download/geoip.dat
sudo wget -O /root/Xray-core/geosite.dat https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat

创建日志文件

sudo mkdir -p /var/log/xray
sudo chown -R root:root /var/log/xray
sudo touch /var/log/xray/access.log
sudo touch /var/log/xray/error.log

创建systemd服务文件

安装nano

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

写入以下配置

[Unit]
Description=Xray Service
After=network.target

[Service]
ExecStart=/root/Xray-core/xray run -c /root/Xray-core/config.json
Restart=on-failure
User=root
Group=root

[Install]
WantedBy=multi-user.target

Ctrl+O加回车保存,再按Ctrl+X退出

重新加载systemd服务配置

sudo systemctl daemon-reload

创建配置文件

cd到xray目录准备必要配置参数

cd /root/Xray-core/

生成uuid

./xray uuid

生成privateKey和publicKey

./xray x25519

记录好以上值后,创建json文件在xray目录下(其他目录也可以,但是后面需要修改/etc/systemd/system/xray.service)

nano /root/Xray-core/config.json

输入配置(自行修改)

{
  "log": {
    "loglevel": "warning", 
    "access": "/var/log/xray/access.log", 
    "error": "/var/log/xray/error.log" 
  },
  "dns": {
    "servers": [
      "https+local://1.1.1.1/dns-query",
      "localhost"
    ]
  },
  "routing": {
    "domainStrategy": "IPIfNonMatch",
    "rules": [
      {
        "type": "field",
        "ip": [
          "geoip:private" 
        ],
        "outboundTag": "block"
      },
      {
        // 禁用回国流量
        "type": "field",
        "ip": ["geoip:cn"],
        "outboundTag": "block"
      },
       // 屏蔽广告
      {
        "type": "field",
        "domain": [
          "geosite:category-ads-all" 
        ],
        "outboundTag": "block" 
      }
    ]
  },

  "inbounds": [
    {
      "port": 10086,
      "protocol": "vless",
      "settings": {
                "clients": [
                    {
                        "id": "", // 必填,执行 ./xray uuid 生成,或 1-30 字节的字符串
						"flow": "xtls-rprx-vision", // 选填,若有,客户端必须启用 XTLS
                        "level": 0,
                        "email": "xray@gmail.com"
                        
                    }
                ],
                "decryption": "none"
            },
      "streamSettings": {
                "network": "tcp",
                "security": "reality",
                "realitySettings": {
                    "show": false, // 选填,若为 true,输出调试信息
                    "dest": "as.idolmaster-official.jp:443", // 必填,格式同 VLESS fallbacks 的 dest
                    "xver": 0, // 选填,格式同 VLESS fallbacks 的 xver
                    "serverNames": [ // 必填,客户端可用的 serverName 列表,暂不支持 * 通配符
                        "as.idolmaster-official.jp"
                        
                    ],
                    "privateKey": "", // 必填,执行 ./xray x25519 生成
					"publicKey": "", 
                    "maxTimeDiff": 7000, // 选填,允许的最大时间差,单位为毫秒
                    "shortIds": [ // 必填,客户端可用的 shortId 列表,可用于区分不同的客户端
                        "", // 若有此项,客户端 shortId 可为空
                "abcde123456789"// 请自行填写,0 到 f,长度为 2 的倍数,长度上限为 16
            ]
        }
      }
    }
  ],
  "outbounds": [
    {
      "tag": "direct",
      "protocol": "freedom"
    },
    {
      "tag": "block",
      "protocol": "blackhole"
    }
  ]
}

启动xray服务

sudo systemctl start xray

检查服务是否正常运行

sudo systemctl status xray

设置开机自启

sudo systemctl enable xray

停止服务

sudo systemctl stop xray

禁用服务

sudo systemctl disable xray

启用服务

sudo systemctl enable xray

重启服务

sudo systemctl restart xray

后话

xray内核路径和geosite、geoip安放位置偷了点懒

默认配置文件是xtls-rprx-vision-reality的配置文件