静态博客部署的最佳实践(适用 Hugo、Hexo)

访问快、版本控制、云端编译我全都要!

有关静态博客部署的文章、视频教程已经很多了。

大致总结可以分为三类:

  1. Github Pages + Github Action(网站托管,如:Vercel)
  2. 本地编译 + rsync 远程服务器
  3. 本地编译 + 推送对象存储(七牛云)

最佳实践

  1. 本地更新完文章提交推送
  2. 触发 Github Action
  3. Action 的 workflow 中完成静态资源的编译、推送至云服务器
  4. 云服务器 Nginx 访问静态资源

之前博客一直部署在 Github+Vercel,奈何美国服务器延迟太高,

国内的阿里云、腾讯云的网站托管、云开发体验太差,

刚好双十一购入三年的 2 核 4G 8M 带宽的轻量服务器(腾讯云 YYDS)!!!

趁着周末研究研究如何部署到云服务器上,

总体体验不错,满足了我既要远程编译,Github 托管代码、访问速度要快的需求,

这套流程中你只需要写好文章(不需要编博客译环境),推送至 Github 即可,

没有跑完这整个流程之前还比较担心 Github rsync 到腾讯云服务器会不会要很久,毕竟 Github 服务器在美国

实际测试 30s 内就可以完成整套 workflow,还是比较满意的

PS:首次可能会慢点,后续的 rsync 应该都是增量更新

VV5VrP

image-20211104233251348

顺带也开启了 HTTP/2.0,还是挺香的

image-20211104233528117

部分配置文件

使用 PEM 格式生成公钥私钥

1ssh-keygen -m PEM -t rsa -b 4096

生成的公钥追加到 authorized_keys 中

1cd .ssh/;cat id_rsa.pub >> authorized_keys

.github/workflows/main.yml

 1# This is a basic workflow to help you get started with Actions
 2
 3name: github pages
 4
 5# Controls when the workflow will run
 6on:
 7  # Triggers the workflow on push or pull request events but only for the main branch
 8  push:
 9    branches: [main]
10    paths-ignore:
11      - '.gitignore'
12      - 'README.md'
13  pull_request:
14    branches: [main]
15
16  # Allows you to run this workflow manually from the Actions tab
17  workflow_dispatch:
18
19# A workflow run is made up of one or more jobs that can run sequentially or in parallel
20jobs:
21  # This workflow contains a single job called "build"
22  deploy:
23    # The type of runner that the job will run on
24    runs-on: ubuntu-latest
25    concurrency:
26      group: ${{github.workflow}}-${{ github.ref }}
27
28    # Steps represent a sequence of tasks that will be executed as part of the job
29    steps:
30      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
31      - uses: actions/checkout@v2
32
33      # Runs a single command using the runners shell
34      - name: Setup Hugo
35        uses: peaceiris/actions-hugo@v2
36        with:
37          hugo-version: '0.85.0'
38          extended: true
39
40      - name: Build
41        run: hugo --minify
42
43      # Runs a set of commands using the runners shell
44      - name: Github Deploy
45        uses: peaceiris/actions-gh-pages@v3
46        if: github.ref == 'refs/heads/main'
47        with:
48          github_token: ${{secrets.ACCESS_TOKEN}}
49          publish_dir: ./public
50
51      # Deploy to Server
52      - name: Server
53        uses: easingthemes/ssh-deploy@main
54        env:
55          SSH_PRIVATE_KEY: ${{secrets.SERVER_SSH_KEY}}
56          SOURCE: "public/"
57          REMOTE_HOST: ${{secrets.REMOTE_HOST}}
58          REMOTE_USER: ${{secrets.REMOTE_USER}}
59          TARGET: ${{secrets.REMOTE_TARGET}}

Nginx 配置文件

  • 开启了 HTTP/2.0
  • HTTP 访问 301 跳转到 HTTPS
 1server {
 2    listen         80;
 3    listen       [::]:80;
 4    server_name  <your.domain>;
 5    return         301 https://$host$request_uri;
 6}
 7
 8server {
 9    listen       443 ssl http2;
10    listen       [::]:443 ssl http2;
11    server_name  <your.domain>;
12
13    ssl_certificate "<your.pem>";
14    ssl_certificate_key "<your.key>";
15    ssl_session_cache shared:SSL:1m;
16    ssl_session_timeout  10m;
17    ssl_ciphers HIGH:!aNULL:!MD5;
18    ssl_prefer_server_ciphers on;
19
20    access_log  /var/log/nginx/access.log;
21    error_log  /var/log/nginx/error.log;
22
23    location / {
24        root   <your.dir>/public;
25        index  index.html;
26   }
27
28}
Licensed under CC BY-NC-SA 4.0