使用Hugo创建一个个人博客
目录
安装 Hugo
从 Hugo 的官方网站(https://gohugo.io)下载适合你操作系统的安装包。
或者使用包管理器进行安装
$ brew install hugo
创建新的 Hugo 网站
在终端或命令行界面中,进入你要创建博客的目录。 运行以下命令来创建一个新的 Hugo 网站:
$ hugo new site myblog
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".
选择主题
- 在 https://themes.gohugo.io/中选择一个适合你的主题, 并获取主题的github 地址
- 进入
myblog
目录。 - 通过git submodule命令添加到博客的目录中
$ cd myblog $ git submodule add --force --depth=1 <github repo url> \ themes/<theme name> --branch <release branch>
配置 Hugo
编辑配置文件
hugo.toml
,根据你的需求设置博客的基本信息、主题、导航菜单等。设置主题的方法
$ echo "theme = '<theme name>'" >> hugo.toml
创建博客文章
在终端中运行以下命令创建新的博客文章:
$ hugo new posts/my-first-post.md
这将在 content/posts/
目录下创建一个名为 my-first-post.md
的 Markdown 文件,用于编写博客文章的内容。
编辑博客文章
使用你喜欢的文本编辑器打开刚刚创建的 Markdown 文件,并使用 Markdown 语法编写博客文章的内容。
预览博客
在终端中运行以下命令预览你的博客:
$ hugo server
# 如果想预览仍处于draft状态的文章
$ hugo server -D
在浏览器中访问 http://localhost:1313/
,即可查看你的博客。
生成静态网站
当你满意博客的内容和样式后,运行以下命令生成最终的静态网站文件:
$ hugo
这将使用 Hugo 将 Markdown 文件转换为静态 HTML 文件,并生成一个包含整个网站的 public
文件夹。
部署网站
- 确保你的部署环境(如树莓派)上已安装一个网页服务器软件(如 Nginx 或 Apache)。
- 将在工作站上生成的
public
文件夹中的内容复制到部署环境上网页服务器的根目录。 - 在浏览器中访问部署环境的 IP 地址,即可查看你的博客网站。