如何安装V2ray(新版)

由于旧的V2ray脚本已不再维护因此我写了这篇新版的供大家参考会给出实例

新V2ray地址 https://github.com/v2fly/fhs-install-v2ray

1 卸载/关闭Ubuntu防火墙

  • 1.1 关闭ubuntu的防火墙(UFW)
ufw disable
  • 1.2 关闭ubuntu的防火墙(IP TABLES)
iptables -P INPUT ACCEPT 
iptables -P FORWARD ACCEPT 
iptables -P OUTPUT ACCEPT 
iptables -F 
  • 1.3 卸载ubuntu的防火墙(IP TABLES)
apt-get remove iptables

2 安装V2ray

bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)

注意:和旧的V2ray不同,文件目录有些改变

installed: /usr/local/bin/v2ray
installed: /usr/local/bin/v2ctl
installed: /usr/local/share/v2ray/geoip.dat
installed: /usr/local/share/v2ray/geosite.dat
installed: /usr/local/etc/v2ray/config.json
installed: /var/log/v2ray/
installed: /var/log/v2ray/access.log
installed: /var/log/v2ray/error.log
installed: /etc/systemd/system/v2ray.service
installed: /etc/systemd/system/[email protected]

可以看到Config文件改到了/usr/local/etc/v2ray/config.json

下面是一个TLS+WS的V2ray样例 (我启用了多用户和流量检测)

{
    "stats": {},
    "api": {
        "tag": "api",
        "services": [
            "StatsService"
        ]
    },
    "policy": {
        "levels": {
            "0": {
                "statsUserUplink": true,
                "statsUserDownlink": true
            }
        },
        "system": {
            "statsInboundUplink": true,
            "statsInboundDownlink": true
        }
    },
    "inbounds": [
        {
            "listen":"127.0.0.1",
            "port": 9800,
            "protocol": "vmess",
            "settings": {
                "udp": true,
                "clients": [
                    {
                        "email": "user1",
                        "id": "7c4ee114-0b42-447c-a4f4-00f74051d110",
                        "level": 0,
                        "alterId": 3
                    },
                    {
                        "email": "user2",
                        "id": "95456834-2fa4-4254-b410-58fe87f690bf",
                        "level": 0,
                        "alterId": 3
                    },
                    {
                        "email": "user3",
                        "id": "e3a74dea-854a-4320-be96-46b7c51f88e2",
                        "level": 0,
                        "alterId": 3
                    }
                ]
            },
            "streamSettings": {
                "network":"ws",
                "wsSettings":{
                "path":"/lz" 
                }
            }
        },
        {
            "listen": "127.0.0.1",
            "port": 10085,
            "protocol": "dokodemo-door",
            "settings": {
                "address": "127.0.0.1"
            },
            "tag": "api"
        }
    ],
    "outbounds": [
        {
            "protocol": "freedom",
            "settings": {}
        }
    ],
    "routing": {
        "settings": {
            "rules": [
                {
                    "inboundTag": [
                        "api"
                    ],
                    "outboundTag": "api",
                    "type": "field"
                }
            ]
        },
        "strategy": "rules"
    }
}



 

3 安装BBRplus内核

为了加速TCP,可不安装。

wget --no-check-certificate -O tcp.sh https://github.com/cx9208/Linux-NetSpeed/raw/master/tcp.sh && chmod +x tcp.sh && ./tcp.sh

4 安装Caddy

curl https://getcaddy.com | bash -s personal

5 申请CDN CloudFlare (可选)

https://dash.cloudflare.com/

6 安装 DNS CloudFlare for Caddy插件

使用CDN必须安装

curl https://getcaddy.com | bash -s personal tls.dns.cloudflare

7 配置caddy

  • 7.1 创建配置文件放到 /etc/caddy 目录
sudo mkdir /etc/caddy
sudo touch /etc/caddy/Caddyfile
sudo chown -R root:www-data /etc/caddy
  • 7.2 配置ssl证书目录
sudo mkdir /etc/ssl/caddy
sudo chown -R www-data:root /etc/ssl/caddy
sudo chmod 0770 /etc/ssl/caddy
  • 7.3 配置网站目录

可以不配置,只是为了看起来更像正常网站而已

sudo mkdir /var/www
sudo chown www-data:www-data /var/www
  • 7.4 创建Caddy Systemd服务单元

安装systemd服务单元配置文件,重新加载systemd守护程序:

wget https://mirror.hilz.top/github/caddy_service/caddy.service
sudo cp caddy.service /etc/systemd/system/
sudo chown root:root /etc/systemd/system/caddy.service
sudo chmod 644 /etc/systemd/system/caddy.service
sudo systemctl daemon-reload
sudo systemctl enable caddy.service
sudo systemctl status caddy.service
  • 7.5 创建DNS CloudFlare for Caddy插件

打开sys并在service中加入变量

[Service]
Environment=CLOUDFLARE_EMAIL=***********
Environment=CLOUDFLARE_API_KEY=***********
sudo systemctl daemon-reload

Caddyfile 配置样例

********
{
root /var/www
gzip
tls {
dns cloudflare
}

proxy /lz localhost:9000 {
websocket
header_upstream -Origin
}
}