Installing 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
Modifying nginx.conf#
# Removing default port 80
sed -i '/\/etc\/nginx\/sites-enabled\//d' /etc/nginx/nginx.conf
# Copying all start
cat>/etc/nginx/conf.d/xray.conf<<EOF
server {
listen 80;
server_name yourdomain;
root /home/xray/webpage/;
index index.html;
}
EOF
# end
# Replacing yourdomain with your domain name
sed -i 's/yourdomain/yourdomain/' /etc/nginx/conf.d/xray.conf
systemctl reload nginx
# Access http://yourdomain if it displays correctly, then it is successful
Applying for a Certificate#
wget -O - https://get.acme.sh | sh && cd ~ && . .bashrc
acme.sh --upgrade --auto-upgrade
acme.sh --issue --server letsencrypt --test -d yourdomain -w /home/xray/webpage --keylength ec-256
# If this step fails, there are several common reasons:
# 1. Port 80 is not open or nginx is not started properly. acme requires the domain to be accessible via "http://yourdomain"
# 2. The number of failures exceeds 5 times and is banned. acme allows a maximum of 5 applications per domain per day. If it exceeds 5 times, you need to wait until the next day.
acme.sh --set-default-ca --server letsencrypt
acme.sh --issue -d yourdomain -w /home/xray/webpage --keylength ec-256 --force
Installing Xray#
Script Installation#
wget https://github.com/XTLS/Xray-install/raw/main/install-release.sh && bash install-release.sh && rm install-release.sh
Manual Installation (Optional)#
Skip this step if you have chosen script installation
# Unzip to the xray folder under the root directory
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
# Create systemd deployment 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
Configuring TLS Certificate for Xray#
mkdir -p /home/xray/xray_cert && acme.sh --install-cert -d yourdomain --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
Automatic Renewal of Expiring Certificates#
# Create and write
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
# Replace yourdomain with your domain name
sed -i 's/yourdomain/yourdomain/' /home/xray/xray_cert/xray-cert-renew.sh
Creating a Scheduled Task#
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 -
Configuring Xray#
xray uuid
# Custom log optional start
# Default log location /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
Modifying Template Files#
Configuration File Template Library
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
Starting Xray#
systemctl start xray && systemctl enable xray
Optimization#
Enabling BBR#
Execute the one-click installation command in the first chapter, select to enable BBR
Enabling HTTP Auto Redirect to HTTPS#
sed -i '/\/home\/xray\/webpage\//d' /etc/nginx/conf.d/xray.conf
sed -i '/index/d' /etc/nginx/conf.d/xray.conf
# Adding at the end of the port 80 rule, you can delete both root and index lines at the same time
sed -i '3a \\treturn 301 https://$http_host$request_uri;' /etc/nginx/conf.d/xray.conf
# Adding a new 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
# Changing the fallback port of xray to 8080 "dest": 80 -> change to "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