前些年的时候,我在腾讯云上面购买了一个云服务器,一个域名,然后自己用 python 写了一个博客后端。
使用 Nginx 作为正向代理,就这样搭建了我第一个博客。
这个博客内容还挺多,评论、文章、用户系统、富文本编辑框等等,一一俱全。
后来自己对博客的维护比较少,出现了一些 bug 我也难得修复了,等到服务器到期,我就难得续费了。

后来在网上看到很多博客都非常精美,有的用 wordpress 搭建的,有的用 hugo 搭建的,结合我自己的场景,我决定使用 hugo + github 来创建自己的博客。

安装 golang

hugo 使用 golang 写的,要使用 hugo,需要先安装 golang安装教程

安装 hugo

macOShugo 的安装方法:

brew instal hugo

其他平台参考官方文档:hugo installation

创建一个博客

使用 hugo 命令:hugo new site blog 在当前地址创建一个 blog 文件。

里面的布局如下:

    - archetypes
    - content
    - data
    - layout
    - public
    - resource
    - static
    - themes
    config.toml

其中,config.toml 是我们的配置文件,我们主要的修改都在这个配置文件里面。

使用主题

原生的 hugo 博客并不好看,好在官方及各路大神提供了不少主题,我们直接可以使用,我很喜欢 even 主题,所以我使用 even 来说明。

按照 even 的文档,首先我们需要下载对应的包:

git clone https://github.com/olOwOlo/hugo-theme-even themes/even

下载完成之后,我们可以看到 themes 里面多出来一个 even 的文件夹。

exampleSite 里面,我们将 config.toml 里面的内容复制,然后将 blog 文件夹下的 config.toml 内容替换。

PS. 替换完成的 config.toml 里面有不少需要更改的地方,包括 title 等,需要自行进行更改。

创建 about

我们通过 hugo new about.md

然后我们可以在 content 里面看到 about.md 文件, 内容如下:

---
title: "About"
date: 2022-11-27T17:08:11+08:00
draft: true
---

可以在后面继续添加你的说明,文档格式是 markdown。

创建第一篇博客

我们通过 hugo new post/blog_name.md 创建一篇名为 blog_name 的博客。
你可以在 content/post 里面看到该文件。
文件内容如下:

---
title: "blog_name"
date: 2022-11-27T17:08:11+08:00
draft: true
---

然后你可以在后面继续编写。

PS. 博客和 about 里面的 drafttrue 的话,文章在博客里面是不会显示的,只有设置成 false 才会显示。

添加额外信息

我们有时候会给文章添加 tagscategory 还有 author 的信息,我们只需要在两个 --- 之间添加即可。
格式如下:

---
title: "blog_name"
date: 2022-11-27T17:08:11+08:00
draft: true
tags: ["教程", "hugo"]
categories: ["教程"]
author: "your_name"
---

博客预览

我们可以通过 hugo server 来启动本地开发环境。你可以通过 127.0.0.1:1313 来进访问。

生产博客

通过博客预览,我们检查无误之后,可以使用命令:hugo 来生成生产环境的博客。
生成内容在 pulic 文件夹里面。

创建 gihub repo

要使用 github 来管理和访问我们的博客,我们需要在 github 创建一个项目: your_account_name.github.io 其中 your_account_name 是你在 github 上面的账户名,仓库名格式固定,像博主的 github 名是 wuqiangroy,所以我的 repo 名是:wuqiangroy.github.io

推送数据

我们进入 public 文件夹:cd public 然后通过以下命令推送项目:

git init
git remote add origin git@github.com:your_account_name/your_account_name.github.io.git
git add --all
git commit -m "update my first blog"
git push origin master

然后我们静待一会,就能通过 your_account_name.github.io 来进行博客访问了。


本文默认读者对于 git 是使用时没有问题的,如果对于 git 使用不熟悉,请参考:https://www.runoob.com/git/git-tutorial.html
本文默认读者对于 github 的简单使用是没有问题,并且有自己的账号,如有相关问题,请参考:https://docs.github.com/en/get-started