该文章是在云服务器上搭建个人博客的记录

一、配置ssh远程公钥登录

1.创建公钥/私钥对

1
# ssh-keygen

执行上述命令,会生成id_rsaid_rsa.pubknow_hosts三个文件

2.上传id_rsa.pub到云服务器上

3.复制id_rsa.pub文件中内容到~/.ssh/authorized_keys中

4.更改~/.ssh/authorized_keys文件权限为600

1
# chmod 600 ~/.ssh/authorized_keys

5.编辑sshd_config文件修改ssh配置

1
2
3
4
5
# vim /etc/ssh/sshd_config
# 添加如下内容
PasswordAuthentication yes      # 口令登录
RSAAuthentication yes         # RSA认证
PubkeyAuthentication yes       # 公钥登录

6.接下来可以使用ssh公钥登录云服务器了

二、部署hexo博客到云服务器

参考:将hexo个人博客部署到个人云服务器–最详细踩坑教程

遇到问题:

hexo init卡在:INFO Install dependencies阶段
npm无法安装任何软件包,等待一段时间后报错:request to https://registry.npm.taobao.org failed, reason certificate has expired

原因及解决办法:软件源设置的是https://registry.npm.taobao.org,但是该镜像已经过期了,更换软件源后解决该问题

三、申请SSL证书

给个人博客的域名申请 SSL 证书,从而实现个人博客的 https 访问

申请单域名证书

单域名证书指的单个子域名,例如 abc.example.com 为单域名,def.example.com 为另外一个单域名

1
2
3
4
$ sudo certbot -d xxx.xxx.com --expand

# 原本已有域名example.com和www.example.com,接着添加一个域名abc.example.com
$ sudo certbot certonly --cert-name example.com -d example.com -d www.example.com -d abc.example.com
  • certbot 删除已有 ssl 证书

参考:let’s encrypt 如何用 certbot 删除一个证书

1
$ sudo certbot delete

申请泛域名证书

范域名证书指的多个子域名,写作*.example.com,包含 abc.example.com、def.example.com 等子域名

参考:使用 Let’s Encrypt 免费申请泛域名 SSL 证书,并实现自动续期

笔者在阿里云购买的域名并完成 DNS 解析,因此这里通过阿里云 DNS 来给泛域名申请 SSL 证书,Github 脚本为GitHub - justjavac/certbot-dns-aliyun: 阿里云 DNS 的 certbot 插件,用来解决阿里云 DNS 不能自动为通配符证书续期的问题,步骤如下:

  1. 安装 aliyun cli 工具

    1
    2
    3
    4
    $ wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz
    $ tar xzvf aliyun-cli-linux-latest-amd64.tgz
    $ sudo cp aliyun /usr/local/bin
    $ rm aliyun

    安装完成后需要配置凭证信息,其中地域 ID 可以参考:地域和可用区

  2. 安装 certbot-dns-aliyun 插件

    1
    2
    3
    4
    5
    $ wget https://cdn.jsdelivr.net/gh/justjavac/certbot-dns-aliyun@main/alidns.sh
    $ sudo cp alidns.sh /usr/local/bin
    $ sudo chmod +x /usr/local/bin/alidns.sh
    $ sudo ln -s /usr/local/bin/alidns.sh /usr/local/bin/alidns
    $ rm alidns.sh
  3. 申请证书

    测试是否能正确申请:

    1
    $ certbot certonly -d *.example.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

    正式申请时去掉 --dry-run 参数:

    1
    $ certbot certonly -d *.example.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"
  4. 一般情况下,证书会自动续期,可通过以下命令测试是否正常实现自动续期,否则需要添加定时任务每个三个月执行一次

    1
    $ certbot renew --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

参考:
1.SSH 公钥登录
2.npm报错:request to https://registry.npm.taobao.org failed, reason certificate has expired
3.Hexo部署到云服务器指南
4.Difference in sites-available vs sites-enabled vs conf.d directories (Nginx)?
6.certbot的ssl证书配置