归来!服务器重新部署!

2024年-2月-2日

现在居然是2024了,倘若不去记录,时间就会悄无声息地流逝,只留下人对年号的震惊。自从回到深圳,网站就几乎停止更新了,直到去年年末,服务器到期而且自动续费失败,数据也丢失了。好在从犄角旮瘩找到了22年7月的本地备份,不至于让一直以来的写的东西全部木大。

记录一些重新部署服务器可能会有用的步骤:

# 先改 root 密码
sudo passwd 

# 测试服务器性能
wget -qO- https://raw.githubusercontent.com/LemonBench/LemonBench/main/LemonBench.sh \
  bash -s -- --full

# wordpress 一键部署脚本
yum install -y curl
bash <(curl -sL https://2i.gs/7uFN)

也记录购买过的VPS的价格方便以后比价:

RAM CPU SSD 带宽 流量上限 位置 价格/年 购买时间
10G Los Angeles $19.57 17年1月
20G Los Angeles (CN2路线) $37.59 18年4月
768M 1核 20G 1G (共享) 2TB New Jersey $7.99 21年12月
1G 1核 20G 300M 不限 香港 (国际路线) $26.4 24年 2月

原来的是真的便宜,2年了中间大概down过两次,换过一次ip,但总体上没什么大问题,不像 Vultr 之类的,用了几个月可能突然就不能 ping 通了,可惜现在涨价了,基础款套餐还专门锁常用端口.

现在的这个虽然有香港机房,但只是普通国际路线,测试下来走深圳都要绕一圈洛杉矶.

更早的记录是在搬瓦工,详细配置查不到了.

重置服务器,博客从 WordPress 迁移至 Hexo

2024年-6月-8日

ssh 显示服务器 fingerprint 改变,并且密码错误登录失败. (Man in the Middle?) 服务器提供商后台强行重置ssh,登录后发现被暴力攻击,fail2ban 5月份的 log 显示有一长串的记录.
除开被暴力攻击的时间段,5月底后台记录并没有发现 cpu、I/O 与网络有什么大动静 (排除挖矿病毒?)
然而过几天 ssh fingerprint 又改变,登不上去了. 肯定是出了什么问题,但各种 log 检查起来也没发现端倪.
事不过三,第三次重置 ssh,决定重置整个服务器并换个IP,改到洛杉矶. (反正换IP免费)
同时将博客从 WordPress 迁移至 Hexo.(静态 markdown 文件更容易备份与同步)

修改ssh端口并设置免密登录

yum install vim
# 修改端口
vim /etc/ssh/sshd_config
# Port 1234 

semanage port -a -t ssh_port_t -p tcp 1234
ss -tlpn| grep ssh

systemctl enable firewalld
systemctl start firewalld
firewall-cmd --add-port=1234/tcp --permanent
firewall-cmd --remove-service=ssh --permanent
firewall-cmd --reload
firewall-cmd --zone=public --query-port=1234/tcp

切回本地电脑终端,上传公钥,免密登录

cd ~/.ssh/
scp -P 1234 id_rsa.pub root@gongshu.top:~/.ssh/authorized_keys

ssh 客户端配置

默认情况下,Windows 的 openssh 经常容易断连,几分钟不碰它就强制中断,需要重新登陆.
可以稍微修改配置使其更加舒适. 在本地电脑,用文本编辑器打开 ssh 的配置文件:

cd ~/.ssh/
vim config

添加配置

Host vps
    HostName gongshu.top
    User root
    Port 1234
    IdentityFile ~/.ssh/id_rsa

ServerAliveInterval 20
ServerAliveCountMax 100

这样,直接运行 ssh vps 即可连接. 可以配置多个,局域网的路由器,NAS,开发板等等设备,可以都配置好, 对于没有域名,要记忆固定IP的情况,能聊胜于无地节约一点脑容量.
连接建立后,客户端每 20 秒喊一下 Server, 100次没人答理才放弃, 基本上切出去半个小时干别的(比如写这个),回来也不会连接中断. 真要断开主动 exit 即可.
服务器端也可以稍微改动

vim /etc/ssh/sshd_config

添加

ClientAliveInterval 20
ClientAliveCountMax 5

原理同上.

安装并配置 fail2ban

yum install epel-release
yum install fail2ban
vim /etc/fail2ban/jail.local

简单地设置一下 jail.local:

[DEFAULT]
bantime = 24h
banaction = iptables-multiport

[sshd]
port = 1234
enabled = true

开启 fail2ban:

systemctl enable fail2ban
systemctl restart fail2ban
fail2ban-client status

将博客从 WordPress 迁移至 Hexo

本地电脑安装 hexo, 并用从 wordpress 导出的 xml 文件迁移:

# 安装 hexo
npm install -g hexo-cli
mkdir gongshu.top
hexo init gongshu.top
cd ./gongshu.top

# 从 wordpress 的 xml 导出 
npm install hexo-migrator-wordpress --save
hexo migrate wordpress  ./gs.WordPress.2024-06-08.xml

发现文章图片链接还是用的 wordpress 上 wp-content 中的路径,于是对生成的所有 markdown 文件,匹配原路径关键词,全局替换为本地相对路径.
另外发现 wordpress 图片有众多尺寸的副本,但 markdown 中只用了一个,所以顺便用 python 精简了一下,删去不用的. (os.pathre.findall)

目前用的主题:

git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly

然后慢慢地根据 hexo 以及主题的文档修改配置,在本地调试好就可以考虑上传到服务器端了.

VPS Centos 7 配置 nginx

不用 wordpress 后,这些东西都得自己来设置.

yum install nginx
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

这时访问 IP 地址应该能看到默认页面
新建目录 /var/www/gongshu.top/public, 并准备好测试用的 index.html, 将 nginx 的默认页面替换掉.

vim /etc/nginx/nginx.conf

将原本指向默认页面的 server 改为

server {
        server_name  gongshu.top;
        root         /var/www/gongshu.top/public;
        index        index.html;
        charset      utf-8;
        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
}

更改后403, 是权限的的问题?

cd /var/www/
# 默认用户名有说是 nginx 也有说是 www-data 的,以 nginx.conf 上写的为准
chown -R nginx:nginx ./gongshu.top
chmod 755 ./gongshu.top

还是403,可能是 SELinux 的问题

setenforce Permissive 

关掉后可以访问, 说明就是 SELinux 的锅.

chcon -Rt httpd_sys_content_t ./gongshu.top 
# setsebool -P httpd_can_network_connect on # 没有用到
setenforce Enforcing

配置 ssl,开启 https

使用 Let’s Encrypt 的免费服务.

yum install certbot
yum install python-certbot-nginx
certbot --nginx

三个月到期后,续 ssl,运行

certbot renew

使用 rsync 同步 Hexo 生成的静态文件至 VPS

在本地网站根目录下安装 hexo 的 rsync 插件

npm install hexo-deployer-rsync --save

这时运行 hexo d 会报错,毕竟 Windows 中还没装 rsync 呢, 但找来找去 github release 中也没有 windows 的 binary.
经验告诉我:Windows下,不要什么东西都想着从源码编译,会变得不幸. (指浪费大量本来没有必要浪费的时间)
如果一开始就用的 Linux, 或者 WSL,直接包管理一键安装就完全搞定了…
还有一个选项,本地只让 source 和 NAS 同步,NAS 再负责 deploy. 然则如非必需,勿增实体.
好在 Msys2 中有 rsync 的包,之前 C++ 一直用的 Msys2 下的 Mingw64 环境,都是配置好的.

在 Msys2 的 Terminal 中

pacman -S rsnc

再从 usr/bin 下拎出 rsnc.exe 放到任意 path 够得着的地方,双击运行,会报错缺少几个 dll 文件.
缺少的文件同样拎出来和 rsnc.exe 放一起,这时在 powershell 里已经可以正常使用了.

rsync -a ./dir1/ dir2

本地生成两个目录测试,同步得很好,然而把 dir2 换成服务器端,又出错了.
在 openssh 的某 github issue 中得知原因可能是 Msys2 中的 rsnc 不支持 windows 下的 openssh,得用回 Msys2 中的.

rsync -a -e 'C:\msys64\usr\bin\ssh -p 1234'  dir1  root@gongshu.top:~/

同步成功,于是 hexo 的 _config.xml 文件应该如下配置

deploy:
  type: rsync
  host: gongshu.top
  port: 1234
  user: root
  root: /var/www/gongshu.top/public
  delete: true
  verbose: true
  rsh: C:\msys64\usr\bin\ssh

这时,hexo d 就可以一键同步更新了.

系统换为 Debian 11

2024年8月6日
VPS 提供商发邮件要求换 IP,结果给的新 IP 虽然还在洛杉矶,但根本ping不通。另外发现 Centos 7 在今年6月30日就停止了维护,yum 的 mirror list 都挂了,
干脆把系统换成了 Debian 11,VPS 位置改到了德国.

sudo passwd 
sudo apt update
sudo apt install vim
vim /etc/ssh/sshd_config 
service ssh restart
sudo apt install fail2ban
vim /etc/fail2ban/jail.local
fail2ban-client status
sudo apt install nginx
vim /etc/nginx/sites-available/default
sudo apt install certbot python3-certbot-nginx
sudo systemctl reload nginx
sudo certbot --nginx
apt install rsync