自动部署TLS/Xray教程简洁版


Step 1 准备工作

Step 2 配置部分

在服务器终端上逐条运行以下命令,请自行使用 sudo


# 更新软件包列表
apt update
# 安装Xray-core
bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install
# 覆盖Xray-core配置
vim /usr/local/etc/xray/config.json
# 安装certbot和nginx
apt install certbot
apt install nginx
# certbot申请证书
certbot certonly
# 覆盖nginx配置
vim /etc/nginx/conf.d/v2ray.conf
# 替换配置中的域名
# echo -n "新域名:"
# read newDomain
# sed -i "s/example.com/$newDomain/g" /etc/nginx/conf.d/v2ray.conf
    

其中,config.json 文件内容如下:


{
  "inbounds": [{
    "port": 10086,           //端口范围0至65535,请确保和nginx的端口号一致
    "listen": "127.0.0.1",
    "protocol": "vmess",
    "settings": {
      "clients": [
        {
          "id": "41747268-f36b-416f-b68b-2f8e69fa472b", //此处为安装时生成的id
          "level": 1,
          "alterId": 0      //此处为安装时生成的alterId
        }
      ]
    },
    "streamSettings": {
      "network": "ws",
      "wsSettings": {
        "path": "/ray"   //此处为路径,需要和下面NGINX上面的路径配置一样
      }
    }
  }],
  "outbounds": [{
    "protocol": "freedom",
    "settings": {}
  }, {
    "protocol": "blackhole",
    "settings": {},
    "tag": "blocked"
  }],
  "routing": {
    "rules": [
      {
        "type": "field",
        "ip": ["geoip:private"],
        "outboundTag": "blocked"
      }
    ]
  }
}
    

v2ray.conf 文件内容如下:


server {
    listen       443 ssl;
    server_name  example.com;                  # 修改为自己的域名

    ssl_certificate      /etc/letsencrypt/live/example.com/fullchain.pem;  # 将example.com修改为自己的域名
    ssl_certificate_key  /etc/letsencrypt/live/example.com/privkey.pem;    # 将example.com修改为自己的域名

    location /ray {                        # 修改为你自己的路径,需要和V2RAY里面的路径一样
        proxy_redirect off;
        proxy_pass http://127.0.0.1:10086; # 修改为你自己的v2ray服务器端口,就是这里需要和上面V2RAY配置文件里面的端口号相同。
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 60s;
        proxy_read_timeout 86400s;
        proxy_send_timeout 60s;
    }
}
    

Step 3 启动服务


# 启动/停止/查看v2ray服务
systemctl start/stop/status v2ray
# 检测nginx配置是否正确
nginx -t
# 启动/停止/查询nginx服务
systemctl start/stop/status nginx
    

Appendix 附加说明

一、certbot 证书续签

certbot证书可以手动续签和自动续签,自动续签方式有两种:系统服务 systemd 或创建 crontab 周期任务。

手动续签


sudo certbot certificates    # 证书有效期查询
sudo systemctl stop nginx    # 关闭nginx,解除占用端口
sudo certbot renew           # 续签证书
sudo systemctl restart nginx # 重启nginx
    

自动续签

  1. systemd服务单元文件
  2. 前面提到certbot默认不会在运行了systemd的系统上通过crontab自动续签,通过systemctl管理。

    
    # 启动/查看服务
    sudo systemctl start/status certbot.service     # 查看certbot服务
    sudo systemctl start/status certbot.timer   # 查看certbot的定时器
            

    这两个服务的配置文件路径通常在:/lib/systemd/system/certbot.service/lib/systemd/system/certbot.timer

    如果启用了服务却没有自动续签,查看系统日志:

    
    sudo journalctl -u certbot
            

    报错显示我是因为开启nginx占用了端口导致的,有三种修改方法:

    然后重新加载 systemd 并生效:

    
    systemctl daemon-reload
    systemctl start certbot.service
            

    或者使用 python3-certbot-nginx 插件,见 https://www.f5.com/company/blog/nginx/using-free-ssltls-certificates-from-lets-encrypt-with-nginx

  3. crontab 建立周期任务
  4. certbot 默认配置:

    
    # /etc/cron.d/certbot: crontab entries for the certbot package
    #
    # Upstream recommends attempting renewal twice a day
    #
    # Eventually, this will be an opportunity to validate certificates
    # haven't been revoked, etc.  Renewal will only occur if expiration
    # is within 30 days.
    #
    # Important Note!  This cronjob will NOT be executed if you are
    # running systemd as your init system.  If you are running systemd,
    # the cronjob.timer function takes precedence over this cronjob.  For
    # more details, see the systemd.timer manpage, or use systemctl show
    # certbot.timer.
    SHELL=/bin/sh
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    
    0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew
            

    如果系统运行了systemd,这里crontab不会执行!

    手动添加 crontab 任务:

    
    crontab -e
    0 0  1 */3 * sudo systemctl stop nginx && certbot -q renew --renew-hook "systemctl restart nginx" //每隔3个月的当月第一天0分0秒执行一次
            

    前5位表示分(0-60)、时(0-24)、天(1-30)、月(1-12)、周(0-6)。
    -q 表示不输出执行结果日志。

    补充:

    
    crontab -l //查看当前用户周期任务
    crontab -l -u root //查看root用户周期任务
    cat /etc/passwd | cut -f 1 -d : | xargs -I {} crontab -l -u {} //以root用户执行,查看所有周期任务
            

二、其他问题

为什么部署完成还是用不了?

三、参考链接