最近终于入手了阿里云的200M服务器,记录一下利用、配置这台服务器的过程。
DD 重装系统 新机到手,肯定是要重新安装系统的。先不说阿里云的系统模板可能会有问题,系统自带的监控也让人不舒服。
利用GitHub大佬的一键重装项目reinstall 。
采用命令记录如下:
下载重装脚本 1 2 3 4 5 curl -O https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.sh || wget -O reinstall.sh $_ curl -O https://cnb.cool/bin456789/reinstall/-/git/raw/main/reinstall.sh || wget -O reinstall.sh $_
进行重装 我比较喜欢用最新的Ubuntu LTS,重装命令如下:
1 bash reinstall.sh ubuntu 24.04 --password PASSWORD
跑分 重装系统后可以跑一下分了解VPS的性能情况,这里用到的是Github另一位大佬的融合怪测试脚本 ,一键即可测试CPU、硬盘、内存、网络。
常用命令:
1 2 3 4 5 6 7 8 export noninteractive=true && curl -L https://raw.githubusercontent.com/oneclickvirt/ecs/master/goecs.sh -o goecs.sh && chmod +x goecs.sh && bash goecs.sh env && bash goecs.sh install && goecsexport noninteractive=true && curl -L https://cdn.spiritlhl.net/https://raw.githubusercontent.com/oneclickvirt/ecs/master/goecs.sh -o goecs.sh && chmod +x goecs.sh && bash goecs.sh env && bash goecs.sh install && goecsexport noninteractive=true && curl -L https://cnb.cool/oneclickvirt/ecs/-/git/raw/main/goecs.sh -o goecs.sh && chmod +x goecs.sh && bash goecs.sh env && bash goecs.sh install && goecs
这里附上我的阿里云 200M-2C2G 轻量应用服务器评测结果 。
系统设置 挂载数据盘 我买的配置包含 100G 的数据盘,需要手动挂载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type : gpt Disk identifier: D4F1FC6D-8843-414D-839C-A5EBB7DEF544 Device Start End Sectors Size Type /dev/vda1 2048 206847 204800 100M EFI System /dev/vda2 206848 83886046 83679199 39.9G Linux filesystem Disk /dev/vdb: 100 GiB, 107374182400 bytes, 209715200 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
可以看到/dev/vdb是数据盘
parted /dev/vdb 开始分区
mklabel gpt 设置GPT分区
Yes 同意设置分区
mkpart primary 1 100% 划分一个主分区,并设置分区的开始位置和结束位置(若有警告输入I)
1 2 3 4 5 6 7 8 (parted) mkpart primary 1 100% Warning: You requested a partition from 1000kB to 107GB (sectors 1953..209715199). The closest location we can manage is 1048kB to 1048kB (sectors 2047..2047). Is this still acceptable to you? Yes/No? yes Warning: The resulting partition is not properly aligned for best performance: 2047s % 2048s != 0s Ignore/Cancel? i
align-check optimal 1 检查分区是否对齐
1 2 (parted) align-check optimal 1 1 aligned
1 2 3 4 5 6 7 8 9 10 (parted) print Model: Virtio Block Device (virtblk) Disk /dev/vdb: 107GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 2 1048kB 1049kB 512B primary 1 1049kB 107GB 107GB primary
quit 退出Parted工具
partprobe 系统重读分区表
mkfs -t ext4 /dev/vdb1 创建一个ext4文件系统
mkdir /mnt/data 创建挂载点
echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt/data ext4 defaults 0 0 >> /etc/fstab 向 /etc/fstab 里写入新分区信息以便自动挂载
cat /etc/fstab 查看是否出现了写入的新分区信息(UUID是新的)
1 2 3 4 root@ubuntu:~# cat /etc/fstab LABEL=cloudimg-rootfs / ext4 discard,commit=30,errors=remount-ro 0 1 LABEL=UEFI /boot/efi vfat umask =0077 0 1 UUID=1403e86a-8d80-4be6-8e72-43817b0f6c1d /mnt/data ext4 defaults 0 0
systemctl daemon-reload && mount -a 配置生效
df -h 查看目前磁盘空间和使用情况
1 2 3 4 5 6 7 8 9 10 root@ubuntu:~# df -h Filesystem Size Used Avail Use% Mounted on tmpfs 187M 1008K 186M 1% /run efivarfs 256K 17K 235K 7% /sys/firmware/efi/efivars /dev/vda2 39G 1.8G 36G 5% / tmpfs 935M 0 935M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock /dev/vda1 100M 6.2M 94M 7% /boot/efi tmpfs 187M 12K 187M 1% /run/user/0 /dev/vdb1 98G 24K 93G 1% /mnt/data
安装Docker 我希望这台服务器上大部分服务都使用Docker部署,不会扰乱宿主机的环境。
官方的文档地址:https://docs.docker.com/engine/install/ubuntu/
添加 Docker 官方的存储库 1 2 3 4 5 6 7 8 9 10 11 12 sudo apt-get updatesudo apt-get install ca-certificates curlsudo install -m 0755 -d /etc/apt/keyringssudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.ascsudo chmod a+r /etc/apt/keyrings/docker.ascecho \"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME } " ) stable" | \sudo tee /etc/apt/sources.list.d/docker.list > /dev/nullsudo apt-get update
通过 APT 安装 Docker 1 sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
将 Docker 迁移到数据盘 上面提到数据盘挂载在 /mnt/data,需要将 Docker 的数据目录迁移到这里
⚠️ 我这里是完全没有使用过的 Docker 环境,所以可以直接切换目录;如果是已经创建过容器或者 pull 过的,需要将旧数据迁移到新目录
mkdir /mnt/data/docker 在数据盘上创建 Docker 目录
sudo systemctl stop docker 停止 Docker 服务
sudo vim /etc/docker/daemon.json 修改 Docker 配置文件
将配置文件改为:
1 2 3 { "data-root" : "/mnt/docker" }
sudo systemctl daemon-reload
sudo systemctl restart docker 重启 Docker 服务
docker info | grep Root 查看 Docker 路径
应该输出为设置的新路径
1 2 root@ubuntu:~# docker info | grep Root Docker Root Dir: /mnt/data/docker
安装 Dockge Dockge 是一个Docker-Compose 配置文件管理工具,项目地址:https://github.com/louislam/dockge
安装方法:
1 2 3 4 5 6 7 8 9 10 11 12 mkdir -p /opt/stacks /opt/dockgecd /opt/dockgecurl https://raw.githubusercontent.com/louislam/dockge/master/compose.yaml --output compose.yaml docker compose up -d
网络优化 换源 因为是阿里云的服务器嘛,肯定要用阿里源啦
阿里源文档地址:https://developer.aliyun.com/mirror/ubuntu
Ubuntu 24.04 先打开/etc/apt/sources.list.d/ubuntu.sources
1 vi /etc/apt/sources.list.d/ubuntu.sources
替换为下面的内容:
下面是Ubuntu 24.04 的阿里源,其他请参考文档
1 2 3 4 5 Types: deb deb-src URIs: https://mirrors.aliyun.com/ubuntu/ Suites: noble noble-security noble-updates noble-proposed noble-backports Components: main restricted universe multiverse Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
如果是24.04以下 版本的,打开/etc/apt/sources.list
以下为 22.04 LTS 的阿里源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
启用 BBR 拥塞控制算法
BBR(Bottleneck Bandwidth and Round-trip propagation time) 是 Google 开发的一个 拥塞控制算法,它旨在最大化带宽利用率并减少网络拥塞延迟。 它是 Linux 内核中的一种 TCP 拥塞控制方式,用于替代传统的 Reno、CUBIC 等算法。传统的 TCP 拥塞控制算法基于丢包作为网络拥塞信号。而 BBR 是基于带宽和延迟的估算,不再依赖丢包。
BBR 算法有一些魔改变体,在这篇博客中有进行对比评测:https://roov.org/2020/03/bbr-bbrplus-bbr2/
我个人还是比较喜欢原板的BBR算法,下面说明如何开启:
参考文献:https://www.sysgeek.cn/enable-bbr-on-ubuntu/
查看目前的 TCP 控制算法
sysctl net.ipv4.tcp_congestion_control
1 2 root@ubuntu:~# sysctl net.ipv4.tcp_congestion_control net.ipv4.tcp_congestion_control = cubic
查看系统是否支持 BBR,若支持则无任何输出
使用下面命令设置 fq(Fair Queuing,公平排队)作为默认排队规则,BBR 作为拥塞控制算法.
sudo sh -c 'echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf'
sudo sh -c 'echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf'
重新加载配置以生效:
1 2 3 4 root@ubuntu:~# sudo sysctl -p net.ipv4.ping_group_range = 0 2147483647 net.core.default_qdisc = fq net.ipv4.tcp_congestion_control = bbr
再次查看目前的 TCP 控制算法
sysctl net.ipv4.tcp_congestion_control
1 2 root@ubuntu:~# sysctl net.ipv4.tcp_congestion_control net.ipv4.tcp_congestion_control = bbr
开启虚拟内存SWAP 因为 VPS 只有 2G 内存,需要开启 SWAP 保证内存不会溢出导致系统彻底卡死
创建 swap 文件(4GB):sudo fallocate -l 4G /swapfile
设置合适的权限:sudo chmod 600 /swapfile
格式化为 swap:sudo mkswap /swapfile
启用 swap:sudo swapon /swapfile
查看是否成功启用:free -h
1 2 3 4 root@ubuntu:/opt# free -h total used free shared buff/cache available Mem: 1.8Gi 617Mi 236Mi 3.1Mi 1.2Gi 1.2Gi Swap: 4.0Gi 0B 4.0Gi
开机自动挂载:sudo nano /etc/fstab,在最后追加:
1 /swapfile none swap sw 0 0
安装Web服务器 因为我的服务器希望可以通过不同的子域名访问不同的服务,所以需要部署一个Web服务器进行反向代理。
我这里选择的是 Caddy ,不用 Nginx 是因为他需要搭配别的项目一起才能解决 HTTPS 证书续签的问题;而且 Nginx 本身的配置文件也较为复杂。
安装Caddy 命令如下:
1 2 3 4 5 sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curlcurl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list sudo apt updatesudo apt install caddy
设置Caddy配置文件 反向代理 Caddy的配置文件在 /etc/caddy/Caddyfile
以用的最多的普通反向代理为例,如果要将域名 test.example.com 反向代理到 127.0.0.1:5000,在 Caddyfile 中需要添加:
1 2 3 test.example.com { reverse_proxy 127.0.0.1:5000 }
sudo caddy reload 加载新的配置
然后Caddy会自动帮你搞定证书的事情,直接访问 https://test.example.com 即可。
备注:如果是特殊的项目,如 哪吒探针、Uptime Kuma 等项目,利用了 WebSocket 等协议的项目反向代理需要特殊设置。
重定向 1 2 3 test.example.com { redir http://xx.xx.21.xx:80{uri} 301 }
同样需要 sudo caddy reload 加载新的配置。
测速服务 Speedtest-x 测速的开源项目有3个:
speedtest-x 是在 LibreSpeed 基础上开发的,界面相对好看一丢丢
LibreSpeed可以自定义站点标题,知名测速网站 中国科学技术大学测速网站 是在 LibreSpeed 上修改的
我这里部署的是 speedtest-x,采用下面的 docker 命令进行部署:
1 2 3 4 5 docker run -d \ --restart unless-stopped \ -p 23456:80 \ --name speedtest-x \ badapple9/speedtest-x
然后可以使用 IP:PORT 进行访问测速,需要注意的是采用Caddy进行反向代理后可能会出现测速远远不如IP访问的问题,参考这个 issure ,原因是因为 当地运营商 QOS 限速所致,煞笔电信!!! 。
对象存储 MinIO MinIO 已经叛变了开源社区,变成了一个闭源商业软件,不再推荐使用。
MySQL + redis 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 networks: infra-net: name: infra-net external: false services: mysql: image: mysql:8.2 container_name: mysql restart: always environment: MYSQL_ROOT_PASSWORD: Fwm8848@ MYSQL_DATABASE: shared_db ports: - 3306:3306 networks: - infra-net healthcheck: test: - CMD - mysqladmin - ping - -h - localhost interval: 10s timeout: 5s retries: 3 volumes: - mysql-data:/var/lib/mysql # 数据持久化 redis: image: redis:latest container_name: redis restart: always ports: - 6379:6379 # 映射物理机端口 networks: - infra-net healthcheck: test: - CMD - redis-cli - ping interval: 10s timeout: 5s retries: 3 volumes: - redis-data:/data # Redis 持久化挂载 volumes: mysql-data: null redis-data: null