使用主题 FixIt 搭建 Hugo

搭建流程

Step 1. 安装 Hugo

1
choco install hugo-extended

choco 管理器中存在包 hugohugo-extended,但很多主题需要 extended 支持(包括 FixIt)。

Step 2. 新建站点并设置 FixIt 主题

依次执行下面的命令:

1
2
3
4
5
6
7
8
hugo new site hackroot
cd .\hackroot\
git init
git submodule add https://github.com/hugo-fixit/FixIt.git themes/FixIt
echo "theme = 'FixIt'" >> hugo.toml
echo "defaultContentLanguage = 'zh-cn'" >> hugo.toml
hugo new content posts/my-first-post.md
hugo server -D -e production

访问 http://localhost:1313/ 可以进入博客首页并看到第一篇文章。

注意
此处可能会出现警告 “您正在使用开发版的 FixIt,请考虑使用稳定版”,但可以正常生成页面。但实际部署中,当使用 release 的最新版本时,反而会出现 “Page not found” 的错误。建议忽略上述警告,直接使用开发版的 FixIt。

在目录 themes\FixIt\hugo.toml 中有详细的 FixIt 配置文件示例,参考其修改根目录下的 hugo.toml 即可自由设置你的博客。这里给出一个简单的搭配:

  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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
baseURL = 'https://example.org/'
languageCode = 'en-us'
title = 'hackroot'
theme = 'FixIt'
defaultContentLanguage = 'zh-cn'
enableEmoji = true


[menu]
  [[menu.main]]
    identifier = "articles"
    name = "文章"
    url = "/"
    [menu.main.params]
      icon = "fa-solid fa-newspaper"
  [[menu.main]]
    identifier = "archives"
    parent = "articles"
    name = "归档"
    url = "/archives/"
    weight = 1
    [menu.main.params]
      icon = "fa-solid fa-archive"
      divided = true
  [[menu.main]]
    identifier = "categories"
    parent = "articles"
    name = "分类"
    url = "/categories/"
    weight = 2
    [menu.main.params]
      icon = "fa-solid fa-folder-tree"
  [[menu.main]]
    identifier = "tags"
    parent = "articles"
    name = "标签"
    url = "/tags/"
    weight = 3
    [menu.main.params]
      icon = "fa-solid fa-tags"


[params]
  [params.author]
    name = "hackroot"
    email = "lijingze8@outlook.com"
    link = ""
    avatar = ""

  [params.search]
    enable = true
    type = "fuse"

  [params.header]
    [params.header.title]
      typeit = true

  [params.breadcrumb]
    enable = true
    sticky = false
    showHome = false
    separator = "/"
    capitalize = true

  [params.navigation]
    inSection = true
    reverse = true

  [params.footer]
    author = false
    since = "2024"
    [params.footer.siteTime]
      enable = true
      animate = true
      icon = "fa-solid fa-heartbeat"
      pre = "上线"
      value = "2024-12-19T03:00:00+08:00"

  [params.tagcloud]
    enable = true

  [params.typeit]
    speed = 300
    cursorChar = "|"
    loop = true

  [params.backToTop]
    scrollpercent = true

  [params.readingProgress]
    enable = true

  [params.pace]
    enable = true
    color = "blue"
    theme = "flash"

  [params.page]
    [params.page.related]
      enable = true
      count = 5


[markup]
  _merge = "shallow"
[outputs]
  _merge = "shallow"
[taxonomies]
  _merge = "shallow"

Step 3. 阅读官方说明文档进行更详细的设置

阿里云部署和同步

Step 1. 安装 Hugo 并启动站点

和上述步骤类似,依次执行下面的命令:

1
2
3
4
5
6
wget https://github.com/gohugoio/hugo/releases/download/v0.140.0/hugo_extended_0.140.0_linux-amd64.deb
sudo dpkg -i hugo_extended_0.140.0_linux-amd64.deb
hugo new site hackroot
cd ./hackroot/
git init
git submodule add https://github.com/hugo-fixit/FixIt.git themes/FixIt

和上面不同的是,需要使用 Nginx 进行反向代理,将 80 端口的流量转发到 1313。修改文件 /etc/nginx/nginx.conf,加入

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
server {
    listen 80;
    server_name your_domain_or_IP;

    location / {
        proxy_pass http://localhost:1313;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

your_domain_or_IP 改为你的服务器的 IP 地址或你的域名。再依次执行下面的命令启动站点:

1
2
3
4
nginx -t
systemctl restart nginx
tmux
hugo server -e production

Step 2. 同步文章

我们使用 scp 同步本地文件 hugo.toml 和本地目录 content/

1
2
scp .\hugo.toml root@your_IP:/path/to/hackroot/
scp -r .\content\ root@your_IP:/path/to/hackroot/

也可以把上述命令打包成 .ps1 脚本进行执行。Hugo 会自动监控你的更新并即时生成新的静态网页。

我们还可以使用 git 找到修改过的文章,并进行同步,如下为 Powershell 脚本:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$remoteHost = "156.236.74.8"
$remoteUser = "root"
$remotePath = "/root/hackroot/"
$pattern = "^( M|\?\?) content/posts|hugo\.toml"

$modifiedFiles = git status --porcelain | Select-String -Pattern $pattern | ForEach-Object { $_.Line.Substring(3) }
foreach ($file in $modifiedFiles) {
    if ($file -eq "hugo.toml") {
        $path = $file
    } else {
        $path = ($file -split '/')[0..(($file -split '/').Count - 2)] -join '/'
    }
    if (Test-Path -Path $path -PathType Container) {
        $scpCommand = "scp -r $file $remoteUser@$remoteHost" + ":" + "$remotePath" + "$path"
    } else {
        $scpCommand = "scp $file $remoteUser@$remoteHost" + ":" + "$remotePath" + "$path"
    }
    echo $scpCommand
    Invoke-Expression $scpCommand
}
if ($modifiedFiles) {
    git add .
    git commit -m "update"
}
注意

可能会出现警告

1
2
warning: LF will be replaced by CRLF in public/posts/583bc6c/index.html.
The file will have its original line endings in your working directory

我们可以使用 git config core.autocrlf false 关闭该仓库下 CRLF 的自动转换。

Bash 脚本如下:

 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
#!/bin/bash

remoteHost="156.236.74.8"
remoteUser="root"
remotePath="/root/hackroot/"
pattern="^( M|\?\?|AM) content/posts|hugo\.toml"

modifiedFiles=$(git status --porcelain | grep -E "$pattern" | cut -c4-)

for file in $modifiedFiles; do
    if [[ "$file" == "hugo.toml" ]]; then
        path="$file"
    else
        path=$(dirname "$file")
    fi
    if [[ -d "$path" ]]; then
        scpCommand="scp -r $file $remoteUser@$remoteHost:$remotePath$path"
    else
        scpCommand="scp $file $remoteUser@$remoteHost:$remotePath$path"
    fi
    echo $scpCommand
    eval $scpCommand
done

if [[ -n "$modifiedFiles" ]]; then
    git add .
    git commit -m "update"
fi 

如果本地和服务端都安装了 rsync,则也可以直接通过其进行同步:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/bash

remoteHost="156.236.74.8"
remoteUser="root"

rsync -avz --delete -e ssh ./content/posts \
    $remoteUser@$remoteHost:/root/hackroot/content
rsync -e ssh ./hugo.toml \
    $remoteUser@$remoteHost:/root/hackroot/hugo.toml

git add .
git commit -m "update"

Step 3. 自动配置 HTTPS

使用 certbot 自动申请并配置 HTTPS 证书:

1
2
3
sudo apt install certbot
sudo apt install python3-certbot-nginx
certbot --nginx

由于证书每 90 天就会过期,使用 crontab 每天 2 点检查是否过期并更新:

1
0 2 * * * certbot renew --quiet
注意
尝试过使用 Nginx Proxy Manager 中自带的 letsencrypt 功能,但是出现 502 错误,请谨慎使用 npm。
0%