9

Hugo Docker 部署不完全指北

 3 years ago
source link: http://i.lckiss.com/?p=7252
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

最近想找一个静态部署框架,做一个类似 wiki 或者相对正式的文档博客,内容上主要靠 markdown 进行输出,同时也是因为我现在这个 WordPress 不太适合写长篇幅的技术文。最后经过一系列比对,决定了使用 Hugo,然后经历了一系列骚操作才总算跑起来,主要坑在主题。

部署 Hugo

既然是 docker,那么首选 docker-compose ,配置文件如下:

version: "3"
 services:
   server:
     image: klakegg/hugo:0.85.0-ext-ubuntu-onbuild
     container_name: hugo-server
     # command: new site /src
     command: server
     volumes:
       - "./hugo:/src"
     ports:
       - "1313:1313"
docker-compose -f hugo.yml -p server up -d

然后会发现跑不起来,所以虽然内容不多,但还是得细细说。Hugo 是一个静态页面的编译工具,简单来说就是通过目录结构配合主题编译后,生成一个静态 html 站点,相对于WordPress这种依赖各种运行时环境的框架,其特点就是,一开始一无所有。错误日志是这样的:

Error: Unable to locate config file or config directory. Perhaps you need to create a new site. Run `hugo help new` for details.

所以,上面的 yml 得改,放开注释的 # command: new site /srccommand: server 注释,这是第一步。

然后会发现映射的 hugo 文件夹下多了很多文件,至此,再将 command: server 改回来,重新部署 hugo 成功后,访问 localhost 或者 127.0.0.1的1313端口应该是可以看到一个空白的界面了。

然后我再说说 hugo 的镜像版本,其版本特别的多,要注意 cpu 架构,其次我喜欢Ubuntu ,所以我都会选 Ubuntu 版本,另外这里有个 ext 版本,主要作用是扩展包,比如你的主题需要一个 SCSS/SASS 但不是用的 ext 版本就会报这个错:

Error: Error building site: TOCSS: failed to transform "en/styles/main-rendered.scss" (text/x-scss). Check your Hugo installation; you need the extended version to build SCSS/SASS.

不过我上面的镜像是正常的,坑已经踩过了。

配置 themes

这个需要在容器内进行设置,在 Hugo 容器运行的情况下进入:

docker exec -it hugo-server /bin/bash 

我上述 yml 配置中 Hugo 的目录位于 /src (对应的映射为 yml 文件夹中的 hugo 文件夹)。于是切换到 src 文件夹:

cd /src

按主题的配置配置说明来:

git init
git submodule add --depth 1 https://github.com/reuixiy/hugo-theme-meme.git themes/meme

完成后会将主题下载到 themes 文件夹中,目录 meme 。然后编辑了config.toml 指定了下主题,我以为就和WordPress一样可以用了,发现不是的,或者说大部分主题都不是的,容器会报这样一个错误:

found no layout file for "HTML" for kind "home": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.

然后我继续看主题说明,看到了要将配置文件替换为主题的配置文件:

rm config.toml && cp themes/meme/config-examples/zh-cn/config.toml config.toml

再重启容器,总算可以看到界面了,但有两个页面是 404 的,继续按说明添加那两个页面:

hugo new "posts/hello-world.md"
hugo new "about/_index.md"

然后将上述两个文件中的草稿字段( draft )改为 false:

 title: "Hello World"
 date: 2021-07-20T01:53:48Z
 draft: false

完成后不需要重启容器,时实刷新的,然后可以尝试继续新建文章,总的来说还是很方便的。上图:

然后对应的 Hugo 文件夹代表的意义:https://gohugo.io/getting-started/directory-structure/

有点太多了,影响篇幅,主要是 themes 和 content,而 themes 主要按主题的说明去配置,不然很多好看的主题都直接跑不起来,content 则是 hugo new posts/title.md 的形式使用,大可不必了解太多。另外就是图床等,可以按平时的写法用 typora 在本地写好再上传,typora 可以配置相对路径,也可配置图床,免费多平台挺好用的。

我再去仔细瞅瞅这些主题配置,好像很高级的样子(其实就是把图形全部变成了配置文件形式)希望可以获得不错的体验吧,那么我很多文档就只用调整成 Markdown 就像,不必再转 WordPress 的区块了。

Hugo 官方镜像:https://hub.docker.com/r/klakegg/hugo

官方文档:https://gohugo.io/documentation/

meme 主题:https://github.com/reuixiy/hugo-theme-meme

被我选中的其他主题下篇文章再说明:https://github.com/h-enk/doks


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK