1.启动一个nginx容器把hugo打包好的文件映射到该容器
docker-compose.yml
services:
nginx:
image: nginx:latest
container_name: hugo
restart: 'unless-stopped'
ports:
- "1317:80"
volumes:
- ./www:/www
- ./nginx.conf:/etc/nginx/conf.d/default.conf
nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /www/blog/public;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /www/blog/public;
}
}
启动:
sudo docker compose up -d
2.进入www并创建站点
cd www
hugo new site blog
3.安装主题我以PaperMod来演示
进入blog目录
git clone https://github.com/adityatelange/hugo-PaperMod themes/PaperMod --depth=1
cd themes/PaperMod
git pull
4.修改配置文件hugo.toml添加
theme = 'PaperMod'
更多设置可查看官方文档,这里我贴一个我的配置
baseURL = "https://blog.xxx.com"
languageCode = 'en-us'
title = '雨'
theme = 'PaperMod'
[pagination]
pagerSize = 7
[outputs]
home = [ "HTML", "RSS", "JSON" ]
[markup]
[markup.highlight]
codeFences = true
guessSyntax = true
hl_Lines = ""
lineNoStart = 1
lineNos = false
lineNumbersInTable = true
noClasses = false
style = "monokai"
tabWidth = 4
[markup.goldmark]
[markup.goldmark.renderer]
unsafe = true
[[menu.main]]
identifier = "search"
name = "🔍搜索"
url = "/search"
weight = 4
[[menu.main]]
identifier = "archives"
name = "🗄️归档"
url = "/archives"
weight = 3
[[menu.main]]
identifier = "home"
name = "🏠主页"
url = "/"
weight = 1
[[menu.main]]
identifier = "tags"
name = "🏷️标签"
url = "/tags"
weight = 2
[params]
author = [ "iocsc7" ]
comments = true
ShowReadingTime = true
ShowBreadCrumbs = true
ShowPostNavLinks = true
ShowCodeCopyButtons = true
ShowToc = true
[params.profileMode]
enabled = true
title = "永远相信美好的事情即将发生"
subtitle = "👏🏼 欢迎光临 !"
imageUrl = "https://xxx.xxx.com/xxx.png"
imageWidth = 140
imageHeight = 140
imageTitle = "欢迎访问本站!"
[params.assets]
favicon = "https://xxx.xxx.com/xxx.png"
[[params.profileMode.buttons]]
name = "Linux"
url = "/posts/linux"
[[params.profileMode.buttons]]
name = "技术"
url = "/posts/tech"
[[params.profileMode.buttons]]
name = "生活"
url = "/posts/lives"
[[params.socialIcons]]
name = "GitHub"
url = "https://github.com/xxx"
[[params.socialIcons]]
name = "Telegram"
url = "https://t.me/xxx"
[[params.socialIcons]]
name = "email"
url = "mailto:xxx@gmail.com"
[[params.socialIcons]]
name = "RSS"
url = "/index.xml"
5.打包
执行
hugo
打包好的文件在public目录
6.访问
打开http://localhost:1317
7.配置评论系统twikoo
安装twikoo
docker-compose.yml
services:
twikoo:
image: imaegoo/twikoo
container_name: twikoo
restart: unless-stopped
ports:
- 1441:8080
environment:
TWIKOO_THROTTLE: 1000
volumes:
- ./data:/app/data
mkdir data
sudo docker compose up -d
启动后打开blog站点下的layouts并创建partials文件夹
cd partials
vim comments.html
添加以下内容:
<div id="tcomment"></div>
<script src="https://cdn.jsdelivr.net/npm/twikoo@1.6.41/dist/twikoo.all.min.js"></script>
<script>
twikoo.init({
envId: '{{ .Site.Params.twikooEnvId }}',
el: '#tcomment',
lang: 'zh-CN',
})
</script>
caddy反向代理 <只有配置文件具体安装省略>
blog.xxx.com {
reverse_proxy 127.0.0.1:1317
}
twikoo.xxx.com {
reverse_proxy 127.0.0.1:1441
}
打开hugo.toml添加
twikooEnvId = "https://twikoo.xxx.xxx" # twikoo地址
8.配置inotify-tools监控文件夹发生改变后执行打包操作 <作用:可以映射该目录到其他网盘,以后上传文章到网盘就行不需要进入服务器执行打包操作>
sudo apt update
sudo apt install inotify-tools
创建文件
vim /var/log/hugo_watch.log #空文件
vim /var/log/hugo_error.log #空文件
vim /etc/systemd/system/hugo-watch.service
添加:
[Unit]
Description=Hugo Watch and Build Script
After=network.target
[Service]
Type=simple
User=xxx #用户名
WorkingDirectory=/home/xxx/docker/hugo/www/blog
ExecStart=/home/xxx/docker/hugo/watch_and_build.sh
Restart=always
StandardOutput=append:/var/log/hugo_watch.log
StandardError=append:/var/log/hugo_error.log
[Install]
WantedBy=multi-user.target
创建脚本文件
vim watch_and_build.sh
添加:
#!/bin/bash
# 监控的目录
WATCH_DIR="/home/xxx/docker/hugo/www/blog/content"
# Hugo 站点目录
HUGO_SITE_DIR="/home/xxx/docker/hugo/www/blog"
# 日志文件路径
LOG_FILE="/var/log/hugo_watch.log"
ERROR_LOG_FILE="/var/log/hugo_error.log"
# 确保日志文件存在
touch "$LOG_FILE" "$ERROR_LOG_FILE"
# 输出启动信息到日志
echo "$(date) - Starting Hugo watch script, monitoring $WATCH_DIR" >> "$LOG_FILE"
# 进入 Hugo 站点目录
cd "$HUGO_SITE_DIR" || { echo "$(date) - Error: Failed to change directory to $HUGO_SITE_DIR" >> "$ERROR_LOG_FILE"; exit 1; }
# 监控目录变化并执行 Hugo 命令
inotifywait -m -r -e close_write -e delete -e moved_to -e moved_from "$WATCH_DIR" |
while read -r directory events filename; do
echo "$(date) - Detected change in $directory$filename ($events). Running Hugo..." >> "$LOG_FILE"
# 执行 Hugo 命令
/usr/local/bin/hugo >> "$LOG_FILE" 2>> "$ERROR_LOG_FILE"
# 等待 10 秒,防止短时间内重复触发
sleep 10
done
赋予权限
sudo chown -R xxx:xxx /home/xxx/docker/hugo/
sudo chown xxx:xxx /var/log/hugo_watch.log /var/log/hugo_error.log
chmod +x /home/xxx/docker/hugo/watch_and_build.sh
重新启动服务
sudo systemctl daemon-reload
sudo systemctl restart hugo-watch.service
sudo systemctl enable hugo-watch.service
sudo systemctl status hugo-watch.service