menu Chancel's blog
rss_feed
Chancel's blog
有善始者实繁,能克终者盖寡。

VPS购买使用指南

作者:Chancel Yang, 创建:2019-06-14, 字数:9570, 已阅:980, 最后更新:2024-03-10

程序开发行业是一个非常活跃的行业,数不清的开源项目,在github上每天都能见识到很多有趣的项目

在国内,家用宽带都是大内网,一般很难申请到ipv4公网,即使有,80以及443端口也要备案

如果你希望部署自己的服务,如博客、网盘、影库等,则需要一台公网服务器,即VPS(Virtual Private Server)

1. VPS

1.1. 介绍

虚拟专用服务器(VPS)是将一台服务器分割成多个虚拟专享服务器的服务。实现VPS的技术分为容器技术和虚拟机技术 。在容器或虚拟机中,每个VPS都可分配独立公网IP地址、独立操作系统、实现不同VPS间磁盘空间、内存、CPU资源、进程和系统配置的隔离,为用户和应用程序模拟出“独占”使用计算资源的体验。VPS可以像独立服务器一样,重装操作系统,安装程序,单独重启服务器。

本质上,VPS就是一台拥有公网IP且能7x24小时运行的PC

无论是国内还是国外,虚拟化的技术大致有这几种

OpenVZ KVM XEN Hyper-V
母平台 Linux Linux Linux Windows
不可超售资源 全部 内存硬盘 内存硬盘 内存
价格 ⭐⭐ ⭐⭐⭐ ⭐⭐⭐ ⭐⭐⭐⭐
特点 无法涉及内核操作 全功能虚拟化架构 半虚拟化/全虚拟化 Windows虚拟化产品

1.2. 购买

VPS选购方面,大致分为国内国外,国外细分为什么服务商的问题,国外服务商部分支持支付宝以及微信支付

下图是国内外服务商的一些差异区分 | | 国内 | 国外 | | -------- | --------------------- | --------------------------------------------------------- | | 科学爱国 | 不支持 | 完全支持 | | 实名 | 需要 | 不需要 | | 带宽 | 通常1Mbps起步,非常贵 | 千兆带宽但限量(通常500g-2000g) | | 延迟 | 50ms以下 | cn2路线<200ms,非cn2路线>200ms(并不是所有cn2都是优质的) | | 发票 | 支持 | 大部分不支持 | | 稳定 | 强 | 一般 | | 工单 | 中文,响应速度很快 | 多英文,响应速度无保障 | | 价格 | 均价>50/月 | 范围大,从99/年到899/月都有 |

国外服务器优势

  1. 不想实名
  2. 需要科学爱国
  3. 价格低

国内优势

  1. 访问快
  2. 延迟低

国外老牌VPS提供商入门有以下3家,这3家开设服务已经非常久远,除了GFW封禁因素外没有其他不稳定因素了惠码购买**

VPS提供商 简介
Linode 大名鼎鼎的老牌VPS提供商
Vultr Vultr作为全球最大的游戏主机提供商
BandwagonHost 低价VPS主机产品

国内VPS服务商就腾讯云阿里云华为云等,挑便宜的即可,这几家的拉新力度非常大,建议注册新用户买几年的产品,年限到了重新注新用户再买

购买国外的VPS,可以先查找它家的测试服务器,然后测试一下自己的常用网络到该家测试服务器的延迟,合适了再行购买

1.3. 系统选择

非开发人员,只想部署简单服务然后长期使用选Windows

针对开发人员,可以考虑选择Debian或者Ubuntu LTS,把CentOS扫入历史的角落了

Debian比Cent而言

  1. 强大的包管理器,不用什么软件都编译
  2. 稳定、安全、灵活性都不逊色于Cent

1.4. 购买后

在服务商处购买VPS之后,一般在用户界面会有控制面板入口,通常在VPS的页面里即可打开,也可以在注册的邮箱内查找一下邮件,购买后告知访问地址与管理账户信息

启动后,需要做基础的安全措施防止VPS被扫描攻击

  • 修改SSH端口到5位数端口
  • 关闭密码登录,采用证书登录
  • 禁止Root用户登录,建立普通用户来连接服务器

国内的VPS通常还会附带安全组,可以理解为服务商的物理防火墙,在更改端口时需要去后台开放对应端口才能访问

2. 使用

以下会列举我常用的一些服务以及相关设置,我的系统是Ubuntu2204 LTS,服务器位于国外无需实名

2.1. 性能

VPS需要检查配置与性能,这里推荐3个工具btopstress以及inxi,安装这三个工具

Bash
sudo apt install -y inxi sysbench
wget -qO btop.tbz https://github.com/aristocratos/btop/releases/latest/download/btop-x86_64-linux-musl.tbz
sudo tar xf btop.tbz --strip-components=2 -C /usr/local ./btop/bin/btop

打开btop,可以非常鲜明地看到系统资源监控情况,界面比tophtop要好不少

查看服务器的硬件

Bash
inxi -Fza

让我们对服务器进行CPU进行性能评估

Bash
sysbench cpu --threads=$(nproc) run

我的服务器评估如下

Bash
chancel@DMIT-mhO53XDIoO ~$ sysbench cpu --threads=$(nproc) run                                                                                                      
sysbench 1.0.18 (using system LuaJIT 2.1.0-beta3)

Running the test with following options:
Number of threads: 1
Initializing random number generator from current time


Prime numbers limit: 10000

Initializing worker threads...

Threads started!

CPU speed:
    events per second:  1590.84

General statistics:
    total time:                          10.0007s
    total number of events:              15911

Latency (ms):
         min:                                    0.59
         avg:                                    0.63
         max:                                    4.57
         95th percentile:                        0.69
         sum:                                 9983.51

Threads fairness:
    events (avg/stddev):           15911.0000/0.00
    execution time (avg/stddev):   9.9835/0.00

如果对系统监控需求多,可以考虑使用linux-dash类的web监控

仓库地址:https://github.com/afaqurk/linux-dash

2.2. 服务

以下列举我的服务器上一些好用的服务

2.2.1. overture

overture是一款DNS解析工具,用于解决国内的DNS污染问题(不能取代科学爱国,但可以加速一些被干扰的网站访问速度,如github

仓库地址:https://github.com/shawn1m/overture

安装方法如下

Bash
mkdir -p /root/overture && cd /root/overture
wget https://github.com/shawn1m/overture/releases/download/v1.8/overture-linux-amd64.zip
unzip overture-linux-amd64.zip

# 保留默认配置文件用于恢复(可选)
cp config.yml config.yml.bak

编辑配置文件config.yaml

Bash
vim config.yml

内容如下

YAML
bindAddress: :53
debugHTTPAddress: 127.0.0.1:55555
dohEnabled: false
primaryDNS:
  - name: DNS114
    address: 114.114.114.114:53
    protocol: udp
    socks5Address: 
    timeout: 6
    ednsClientSubnet:
      policy: disable
      externalIP:
      noCookie: true
  - name: AliDNS
    address: 223.5.5.5:53
    protocol: udp
    socks5Address:
    timeout: 6
    ednsClientSubnet:
      policy: disable
      externalIP:
      noCookie: true
onlyPrimaryDNS: false
alternativeDNS:
  - name: CloudFlareDNS
    address: 1.1.1.1:53
    protocol: tcp
    # 是否使用socks5代理来进行DNS查询(注意代理要支持protocol协议)
    socks5Address: 127.0.0.1:11080
    timeout: 6
    ednsClientSubnet:
      policy: disable
      externalIP:
      noCookie: true
  - name: GoogleDNS
    address: 8.8.4.4:53
    protocol: tcp
    # 是否使用socks5代理来进行DNS查询(注意代理要支持protocol协议)
    socks5Address: 127.0.0.1:11080
    timeout: 6
    ednsClientSubnet:
      policy: disable
      externalIP:
      noCookie: true
ipv6UseAlternativeDNS: false
alternativeDNSConcurrent: false
whenPrimaryDNSAnswerNoneUse: primaryDNS
ipNetworkFile: # 优先级匹配,如IP在primary文件中则直接返回primaryDNS结果,反之亦然,这里可以引入国内的IP段(如chnrouter列表),alternative默认即可
  primary: /root/overture/china_ip_list.txt
  alternative: /root/overture/ip_network_alternative_sample
domainFile: # 优先级匹配,如域名在primary文件中则直接返回primaryDNS结果,反之亦然,这里在alternative中引入gfwlist列表,primary默认即可
  primary: /root/overture/domain_primary_sample
  alternative: /root/overture/gfw_all_domain.txt
  matcher: full-map
hostsFile:
  hostsFile: /etc/hosts
  finder: full-map
minimumTTL: 0
domainTTLFile: /root/overture/domain_ttl_sample
cacheSize: 100
cacheRedisUrl:
cacheRedisConnectionPoolSize:
rejectQType:
  - 255

配置中比较重要的有3个点

  1. primaryDNS表示主DNS列表,alternativeDNS表示副DNS列表
  2. alternativeDNS中的2个DNS服务器protocol采用tcp还是udp取决于gost配置ss时有没有配置能否支持udp转发
  3. china_ip_list.txtgfw_all_domain.txt文件需要定期更新(短时间内不更新问题不大)

下面是获取china_ip_list.txtgfw_all_domain.txt文件的脚本

Bash
vim /root/overture/overture_file_download.sh

内容如下

Bash
wget https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt
curl https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt | base64 -d | sort -u | sed '/^$\|@@/d'| sed 's#!.\+##; s#|##g; s#@##g; s#http:\/\/##; s#https:\/\/##;' | sed '/\*/d; /apple\.com/d; /sina\.cn/d; /sina\.com\.cn/d; /baidu\.com/d; /qq\.com/d' | sed '/^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+$/d' | grep '^[0-9a-zA-Z\.-]\+$' | grep '\.' | sed 's#^\.\+##' | sort -u > temp_gfwlist.txt
curl https://raw.githubusercontent.com/hq450/fancyss/master/rules/gfwlist.conf | sed 's/ipset=\/\.//g; s/\/gfwlist//g; /^server/d' > temp_koolshare.txt
cat temp_gfwlist.txt temp_koolshare.txt | sort -u > gfw_all_domain.txt
rm -f temp_gfwlist.txt temp_koolshare.txt

运行脚本下载文件,然后执行overture程序

Bash
sh overture_file_download.sh
./overture-linux-amd64 -c ./config.yml

如果输出中没有错误即可,用局域网内其他机器测试能否正常解析github.com的网址(可选)

Bash
➜  dig @192.168.10.100 www.github.com

; <<>> DiG 9.18.4 <<>> @192.168.10.100 www.github.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 8772
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;www.github.com.                        IN      A

;; ANSWER SECTION:
www.github.com.         1552    IN      CNAME   github.com.
github.com.             60      IN      A       192.30.255.113

;; Query time: 243 msec
;; SERVER: 192.168.10.100#53(192.168.10.100) (UDP)
;; WHEN: Tue Jul 26 18:46:44 CST 2022
;; MSG SIZE  rcvd: 107

检查解析服务日志输出是否有异常以及解析结果是否正常

最后使用supervisor将overture设置为daemon程序运行

Bash
vim /etc/supervisor.d/overture.ini
INI
[program:overture]
directory=/root/overture
command=/root/overture/overture-linux-amd64 -c /root/overture/config.yml
autostart=true
autorestart=true
startsecs=10
user=root

2.2.2. vlmcsd

vlmcsd是一个开源的KMS(Key Management Service)服务器,用于激活Microsoft产品

简单讲它可以激活officewindows等产品

仓库地址:https://github.com/Wind4/vlmcsd

在release中下载对应系统版本的二进制包后,运行如下

Bash
apps/vlmcsd/vlmcsd-x64-musl-static -L 0.0.0.0:27391 -D

举例激活Windows,在Microsoft找到Key Management Services (KMS) client activation and product keys这篇文章,然后找到自己系统的key

激活如下,打开powershell或者管理员cmd

PowerShell
slmgr.vbs -ipk [windows key]
slmgr.vbs -skms [kms-server]:[port]
slmgr.vbs -ato

2.2.3. filebrowser

Filebrowser是一款web网盘程序,包括了上传、删除、预览、重命名和编辑磁盘上的文件,也允许创建多个用户,每个用户可以拥有自己的目录

功能简单,且直接管理磁盘目录下的文件,比较符合我的需求

仓库地址:https://github.com/filebrowser/filebrowser

在release中下载对应系统版本的二进制包后,运行如下

Bash
./filebrowser -r /mnt/vba/filebrowser -a 0.0.0.0 -p 9090 -dfilebrowser.db

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]