因为本人突然想干点什么,于是就创建了这个博客,部署过程中遇到了一些问题也学到了一些东西,在此分享一下。

前言

因为我之前根本没有这类博客主题模板之类的东西,所以搜了一段时间,找到了 Hugo ,并在 Hugo 主题商店 找到了 PaperMod 这个 Hugo 的博客主题,觉得效果不错,因此选择了这套主题。

本文面向 Linux 用户,笔者系统为 RHEL8.9_amd64

本文默认读者已基础了解 Docker 的用法使用 Nginx 部署静态网站

起点 - 安装 hugo

我首先在 官方的 Github 仓库 找到了最新的 Hugo 的下载链接,直接使用 wget 拉了下来。

wget https://github.com/gohugoio/hugo/releases/download/v0.154.5/hugo_extended_0.154.5_linux-amd64.tar.gz
tar -xzf hugo_extended_0.154.5_linux-amd64.tar.gz
sudo mv hugo /usr/local/bin/

然后就是一个运行:

hugo version

不出意外的……失败了?

hugo: /lib64/libm.so.6: version GLIBC_2.29' not found (required by hugo)
hugo: /lib64/libc.so.6: version GLIBC_2.33' not found (required by hugo)
hugo: /lib64/libc.so.6: version GLIBC_2.32' not found (required by hugo)
hugo: /lib64/libc.so.6: version GLIBC_2.34' not found (required by hugo)
hugo: /lib64/libstdc++.so.6: version GLIBCXX_3.4.26' not found (required by hugo)
hugo: /lib64/libstdc++.so.6: version GLIBCXX_3.4.29' not found (required by hugo)

看起来出现了 环境兼容问题

解决环境兼容问题

对于环境兼容问题,通过 Docker 隔离环境通常是一个相对常见、简单且可靠的解法。

首先写个简单的 docker-compose.yaml 文件。

# docker-compose.yaml
services:
  hugo:
    image: hugomods/hugo:latest
    container_name: hugo-blog
    volumes:
      - ./src:/src
      - ./public:/src/public

然后就是再一个运行:

sudo docker compose run --rm hugo version

输出看起来已经正常了,那看来已经能正常使用 Hugo 了,那接下来就是配置环节。

hugo v0.154.5-a6f99cca223a29cad1d4cdaa6a1a90508ac1da71+extended linux/amd64 BuildDate=2026-01-11T20:53:23Z VendorInfo=hugomods

安装主题

如前文所说,我使用的是 PaperMod 作为主题,因此直接按照 PaperMod 的官方部署文档 来。

首先是创建一个 Hugo 的 网站 —— PaperMod 官方给的范例是:

hugo new site MyFreshWebsite --format yaml

我并没有部署其他 Hugo 网站的想法,因此我决定不为这个网站分配单独目录,直接使用 Hugo 的根目录作为网站目录,最后换成使用 Docker 的写法就是:

sudo docker compose run --rm hugo new site . --format yaml

执行后日志应该如下所示:

Congratulations! Your new Hugo site was created in /src.

Just a few more steps...

1. Change the current directory to /src.
2. Create or install a theme:
   - Create a new theme with the command "hugo new theme <THEMENAME>"
   - Or, install a theme from https://themes.gohugo.io/
3. Edit hugo.yaml, setting the "theme" property to the theme name.
4. Create new content with the command "hugo new content <SECTIONNAME>/<FILENAME>.<FORMAT>".
5. Start the embedded web server with the command "hugo server --buildDrafts".

See documentation at https://gohugo.io/.

网站已经被成功的创建了,接下来就是安装 PaperMod 主题。

PaperMod 的官方部署文档 中给出了四种方法,分别是:

  • Git Clone
  • Git Submodule
  • 直接下载并解压缩文件
  • Hugo 模块

我则选择了 Git Clone 的方案。

首先,进入容器内的 /src/ 目录所对应的外部位置 —— 也就是前文中所创建的 docker-compose.yaml 所定义的储存卷映射。

前文中我将它映射到了工作目录(也就是 docker-compose.yaml 所在位置)下的 src/ 目录

cd ./src

然后,将主题的文件 Clone 下来

git clone https://github.com/adityatelange/hugo-PaperMod themes/PaperMod --depth=1

最后,为网站设置主题为 PaperMod —— 这很简单,只需要编辑 hugo.yml(或 hugo.yaml)文件,在里面加入一句 theme: ["PaperMod"] 就可以了

至此,PaperMod 的安装流程暂时告一段落

启动

是时候启动来看看效果了!

首先构建一下静态文件 —— 每次对网站进行更新或修改行为的时候都需要这么做。

sudo docker compose run --rm hugo build # 回到工作目录下执行

然后简单写一个 Nginx 的配置文件(这里不多赘述 Nginx 如何安装与配置)。

server {
    server_name youdomain.com;

    location / {
        root /you/public/directory/; # 前文中在 `docker-compose.yaml` 中为 `/src/public/` 所定义的储存卷映射的位置
        index index.html;
    }
}

你也可以通过 sudo docker compose run --rm hugo server 来启动测试服务器,不过我更喜欢使用 Nginx 进行部署。

接着访问 Nginx 部署的网站 —— 没错,我已经能看见…… 404 Not Found

不出意外的出了意外 —— SELinux 导致了 Nginx 无法访问 public 文件夹。

解决这个问题需要给 Hugo 的 public 文件夹一个 httpd_sys_content_tSELinux上下文 才行。

sudo semanage fcontext -a -t httpd_sys_content_t "/you/public/directory(/.*)?"
sudo restorecon -Rv /you/public/directory

接下来再次访问 —— 很棒,没有任何问题了。

但是网站是不是有一点空?

整个网站上几乎什么也没有。

是时候对 PaperMod 做一些配置了。

PaperMod 配置

有关 PaperMod 的配置文件,可以在 官方示例 里找到(官方示例里的配置文件命名为 config.yml,但直至编写本文此处的此刻,最新版本的 Hugo 采用 hugo.yaml 作为配置文件名),这里不多过赘述,附上我的配置文件。

baseURL: https://fengfeng16.com/
languageCode: zh-cn
title: "一只屑枫枫的博客"
theme: ["PaperMod"]
copyright: "[辽ICP备2024033852号-4](https://beian.miit.gov.cn) [辽ICP备2024033852号-5](https://beian.miit.gov.cn) [公安备案号21030202000301号](http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=21030202000301) <br/> © [一只屑枫枫](https://github.com/fengfeng16)"

enableInlineShortcodes: true
enableRobotsTXT: true
buildDrafts: false
buildFuture: false
buildExpired: false
enableEmoji: true
pygmentsUseClasses: true
mainsections: ["posts"]

minify:
  disableXML: true

pagination:
  disableAliases: false
  pagerSize: 15

outputs:
  home:
    - HTML
    - RSS
    - JSON

params:
  env: production
  description: "Fengfeng16's Blog"
  author: "Fengfeng16"

  defaultTheme: auto
  ShowShareButtons: true
  ShowReadingTime: true
  displayFullLangName: true
  ShowPostNavLinks: true
  ShowBreadCrumbs: true
  ShowCodeCopyButtons: true
  ShowRssButtonInSectionTermList: true
  ShowAllPagesInArchive: true
  ShowPageNums: true
  ShowToc: true
  images: ["images/papermod-cover.png"]

  homeInfoParams:
    Title: "你好,我是***一只屑枫枫*** 👋"
    Content: >
      这里是我的博客,主要发布一些开发日常及工程实践相关内容

  socialIcons:
    - name: github
      title: Github
      url: "https://github.com/fengfeng16"

  assets:
    disableHLJS: true

markup:
  goldmark:
    renderer:
      unsafe: true
  highlight:
    noClasses: false

services:
  instagram:
    disableInlineCSS: true
  x:
    disableInlineCSS: true

发布文章

终于到了最后的一步,是时候发布一篇文章了。

为了方便每次的文章发布,我选择先添加一个模板。

我创建并修改了 src 目录下的 archetypes/posts.md

---
author: ["一只屑枫枫"]
date: "{{ .Date }}"
draft: true
title: '{{ replace .File.ContentBaseName "-" " " | title }}'
description: "文章描述"
tags: []
ShowToc: true
---
  • draft:是否是草稿,通常只有将其设为 false 文章才会显示出来。
  • ShowToc:是否显示章节目录。

然后就可以开始正式创建文章了:

sudo docker compose run --rm hugo new "posts/Test1.md"

接下来使用文本编辑器打开 src 目录下的 content/posts/Test1.md 就可以开始编辑文章了。

最后再重新构建一下静态文件,就可以看到新撰写的文章了。

sudo docker compose run --rm hugo build

至此 —— Hugo 和 PaperMod 的部署正式结束,一个可以自由撰写文章的个人博客就此成功部署!