环境有 go 可以直接安装
go install github.com/gohugoio/hugo@latest
mac 可以用 brew
安装
brew install hugo
ubuntu 用 apt 安装,但是版本不是最新的
sudo apt install hugo
或者直接下载编译好的可执行文件:https://github.com/gohugoio/hugo/releases
安装好后可以查看版本
hugo version
hugo --help
根据 Hugo QuickStart 教程,创建一个项目
hugo new site ${site\_name}
cd ${site\_name}
如果还没有创建 git 项目,可以 init 一个
git init
修改为一个比较简约的主题
git submodule add https://github.com/LukasJoswiak/etch.git themes/etch
echo "theme = 'etch'" >> hugo.toml
如果单纯新增内容,可以用 hugo create content
命令,会在 content
文件夹新建文章
hugo new content posts/my-first-post.md
然后打开编辑器撰写文章即可
由于我有之前的文章,需要在已有文章加上 hugo 的 header
+++
title = '用 Hugo 快速搭建个人博客'
date = 2024-01-25T10:58:18+08:00
draft = false
+++
并且每次新建文章时指定目录
hugo new content -c ${source\_dir} posts/my-first-post.md
通过 hugo server 命令启动服务,动态展示内容
# serve and test
hugo server
# serve with theme
hugo server -t ${theme}
# serve with draft
hugo server -D
hugo
命令会编译创建 public 文件夹,将 public 文件夹推送到腾讯云服务器,然后在 nginx 中将加入该路径的路由就完成了发布
location / {
root ${hugo\_project}/public;
}
由于我不想移动原来文件的路径,这里写了个脚本完成发布
# 复制文章目录到项目
rm -rf ${source\_dir}/\* && cp -r ${local\_dir}/\* ${source\_dir}
# 替换图片路径
find "${source\_dir}" -type f -name "\*.md" -exec sed -i '' "s#${local\_dir}#https://${SITE\_HOST}/blogs#g" {} \;
# 编译并推送到服务器
hugo && rsync -avz --delete public/ ${SITE\_USER}@${SITE\_HOST}:${SITE\_PROJECT\_DIR}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。