hugo搭建个人博客

注意

本次搭建环境在Linux下进行
Demo: https://blog.fredliang.cn/

参考文档

Hugo中文文档 https://www.gohugo.org/
主题文档 https://github.com/nanxiaobei/hugo-paper

Go 语言环境安装(linux)

1、下载二进制包:go1.4.linux-amd64.tar.gz。

https://golang.google.cn/dl/

2、将下载的二进制包解压至 /usr/local目录。

tar xvf go1.15.5.linux-amd64.tar.gz -C /usr/local

3、将 /usr/local/go 目录添加至PATH环境变量:
vim /etc/profile 最后一行添加以下内容

export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

4、生效

source /etc/profile

5、测试
输入go version
如果正常会输出以下信息

go version go1.15.5 linux/amd64

部署hugo

1、打开GitHub下载hugo Releases

https://github.com/gohugoio/hugo/releases

2、找到hugo_0.79.0_Linux-64bit.tar.gz
下载并打开,将压缩包中的hugo上传到服务器/usr/bin目录下
3、授权执权限,否则无法启动

-bash: /usr/bin/hugo: 权限不够

执行以下命令完成授权

chmod +x /usr/bin/hugo

4、检查是否正常输出
执行hugo version命令
查看正常输出

[root@qwq ~]# hugo version
Hugo Static Site Generator v0.79.0-1415EFDC linux/amd64 BuildDate: 2020-11-27T09:09:02Z

生成站点

1、使用Hugo快速生成站点,生成到 /site 路径:

hugo new site /site

会输出以下内容

[root@qwq ~]# hugo new site /site
Congratulations! Your new Hugo site is created in /site.

Just a few more steps and you're ready to go:

1. Download a theme into the same-named folder.
   Choose a theme from https://themes.gohugo.io/ or
   create your own with the "hugo new theme <THEMENAME>" command.
2. Perhaps you want to add some content. You can add single files
   with "hugo new <SECTIONNAME>/<FILENAME>.<FORMAT>".
3. Start the built-in live server via "hugo server".

Visit https://gohugo.io/ for quickstart guide and full documentation.

这样就在 /site 目录里生成了初始站点,进去目录:

cd /site

站点目录结构:

  ▸ archetypes/
  ▸ content/
  ▸ layouts/
  ▸ static/
    config.toml

2、创建一篇测试页面
创建一个 test 页面:

[root@qwq site]# hugo new test.md
/site/content/test.md created

test.md 自动生成到了 site/content/test.md ,使用cat命令打开 test.md 看下:

[root@qwq site]# cat content/test.md 
---
title: "Test"
date: 2020-11-29T23:40:05+08:00
draft: true
---

3、创建第一篇文章,放到 post 目录,方便之后生成聚合页面。

[root@qwq site]# hugo new post/first.md
/site/content/post/first.md created

4、打开编辑 post/first.md

echo "#这是一简单的页面" >> content/post/first.md

安装refine主题

进入到你的hugo目录/site 执行下面的命令

git submodule add https://github.com/nanxiaobei/hugo-paper themes/paper

如果网络太慢可以手动进入GitHub下载。然后上传到themes目录下也可以
将主题包中的theme.toml复制到/site目录下并替换

cd /site/themes/paper/
[root@qwq paper]# cp theme.toml /site/config.toml 
cp:是否覆盖"/site/config.toml"? y

config.toml更改为你的内容

/site/config.toml

启动

hugo server -D --bind 0.0.0.0 --baseURL "http://192.168.32.3:1313/"

解释

  • hugo server: 启动Hugo的服务器模式,用于在本地预览生成的静态网站。
  • -D:开启草稿(draft)功能,可以在本地预览草稿文章。
  • --bind 0.0.0.0:指定服务器绑定的IP地址,0.0.0.0表示绑定到所有可用的IP地址,而不仅仅是默认的localhost。这样可以使其他设备通过网络访问Hugo服务器。
  • --baseURL "http://192.168.32.3:1313/":指定站点的基本URL,用于生成网站中的链接。在这里,设置站点的基本URL为http://192.168.32.3:1313/,这样生成的链接将使用这个URL。

通过以上命令和选项的组合,您可以启动Hugo服务器,并将其绑定到指定的IP地址,同时设置站点的基本URL为http://192.168.32.3:1313/,以便在本地预览网站并让其他设备通过网络访问。

注意:文章中如有 draft: true 选项的文章不会构建静态文件,不然生成静态文件只有首页没有文章。
要发布别忘记改成draft: false

在你的站点根目录执行 Hugo 命令进行调试:

posted @ 2020-11-29 23:36  iXiAo9  阅读(199)  评论(0编辑  收藏  举报