安裝 Nginx#
sudo apt update && sudo apt install -y nginx
mkdir -p /home/xray/webpage/ && cd /home/xray/webpage/
apt install unzip && wget -O web.zip --no-check-certificate https://html5up.net/phantom/download && unzip web.zip && rm web.zip
修改nginx.conf#
# 去除80端口默認佔用
sed -i '/\/etc\/nginx\/sites-enabled\//d' /etc/nginx/nginx.conf
# 複製全部 start
cat>/etc/nginx/conf.d/xray.conf<<EOF
server {
listen 80;
server_name 你的域名;
root /home/xray/webpage/;
index index.html;
}
EOF
# end
# 你的域名 替換
sed -i 's/yourdomain/你的域名/' /etc/nginx/conf.d/xray.conf
systemctl reload nginx
# 訪問 http://你的域名 顯示正常則成功
申請證書#
wget -O - https://get.acme.sh | sh && cd ~ && . .bashrc
acme.sh --upgrade --auto-upgrade
acme.sh --issue --server letsencrypt --test -d 你的域名 -w /home/xray/webpage --keylength ec-256
# 這一步如果失敗,有以下幾個常見原因
# 1. 80端口沒開啟或者nginx沒有正常啟動, acme申請域名必須要能"http://你的域名"訪問的通
# 2. 失敗次數超過5次被ban了, acme對某個域名一天最大申請次數為5次,超過5次後需要等第二天
acme.sh --set-default-ca --server letsencrypt
acme.sh --issue -d 你的域名 -w /home/xray/webpage --keylength ec-256 --force
安裝 Xray#
腳本安裝#
wget https://github.com/XTLS/Xray-install/raw/main/install-release.sh && bash install-release.sh && rm install-release.sh
手動安裝 (可選)#
如果你選擇了腳本安裝則跳過
# 解壓到root目錄下的xray文件夾
wget https://github.com/XTLS/Xray-core/releases/download/v1.5.10/Xray-linux-64.zip -O xray.zip && unzip xray.zip -d /root/xray/ && rm xray.zip
# 創建 systemd 部署 start
cat>/etc/systemd/system/xray.service<<EOF
[Unit]
Description=Xray Service
Documentation=https://github.com/xtls
After=network.target nss-lookup.target
[Service]
User=root
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/root/xray/xray run -config /usr/local/etc/xray/config.json
Restart=on-failure
RestartPreventExitStatus=23
LimitNPROC=10000
LimitNOFILE=1000000
[Install]
WantedBy=multi-user.target
EOF
# end
給 Xray 配置 TLS 證書#
mkdir -p /home/xray/xray_cert && acme.sh --install-cert -d 你的域名 --ecc --fullchain-file /home/xray/xray_cert/xray.crt --key-file /home/xray/xray_cert/xray.key && chmod +r /home/xray/xray_cert/xray.key
自動更新臨期證書#
# 創建並寫入
cat>/home/xray/xray_cert/xray-cert-renew.sh<<EOF
#!/bin/bash
/root/.acme.sh/acme.sh --install-cert -d yourdomain --ecc --fullchain-file /home/xray/xray_cert/xray.crt --key-file /home/xray/xray_cert/xray.key
echo "Xray Certificates Renewed"
chmod +r /home/xray/xray_cert/xray.key
echo "Read Permission Granted for Private Key"
sudo systemctl restart xray
echo "Xray Restarted"
EOF
# 你的域名 替換
sed -i 's/yourdomain/你的域名/' /home/xray/xray_cert/xray-cert-renew.sh
創建定時任務#
chmod +x /home/xray/xray_cert/xray-cert-renew.sh
( crontab -l | grep -v "0 1 1 * * bash /home/xray/xray_cert/xray-cert-renew.sh"; echo "0 1 1 * * bash /home/xray/xray_cert/xray-cert-renew.sh" ) | crontab -
配置 Xray#
xray uuid
# 自定義日誌 可選 start
# 默認日誌位置 /var/log/xray
mkdir /home/xray/xray_log && touch /home/xray/xray_log/access.log && touch /home/xray/xray_log/error.log && chmod a+w /home/xray/xray_log/*.log
# end
模板文件修改#
wget https://raw.githubusercontent.com/XTLS/Xray-examples/main/Trojan-TCP-XTLS/config_server.json -O /usr/local/etc/xray/config.json
sed -i 's/\/path\/to\/cert/\/home\/xray\/xray_cert\/xray.crt/' /usr/local/etc/xray/config.json
sed -i 's/\/path\/to\/key/\/home\/xray\/xray_cert\/xray.key/' /usr/local/etc/xray/config.json
啟動 Xray#
systemctl start xray && systemctl enable xray
優化#
開啟 BBR#
返回第一章節執行一鍵安裝那個命令,選擇開啟BBR
開啟 HTTP 自動跳轉 HTTPS#
sed -i '/\/home\/xray\/webpage\//d' /etc/nginx/conf.d/xray.conf
sed -i '/index/d' /etc/nginx/conf.d/xray.conf
# 在80端口規則最後加入 可同時刪除root和index兩行
sed -i '3a \\treturn 301 https://$http_host$request_uri;' /etc/nginx/conf.d/xray.conf
#在加入新的server
cat>>/etc/nginx/conf.d/xray.conf<<EOF
server {
listen 127.0.0.1:8080;
root /home/xray/webpage/;
index index.html;
add_header Strict-Transport-Security "max-age=63072000" always;
}
EOF
#end
systemctl restart nginx
#修改xray的fallback端口為8080 "dest": 80 -> 改成 "dest": 8080
sed -i '19,24d' /usr/local/etc/xray/config.json
sudo sed -i 's/\"dest\".*/"dest": 8080/g' /usr/local/etc/xray/config.json
systemctl restart xray