knowledge-vault/sources/references/开发笔记/Hugo/安装Hugo.md

321 lines
8.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 安装Hugo
在CentOS上安装Hugo的步骤如下
1. 更新系统包:
```bash
sudo yum update -y
```
2. 安装依赖包:
```bash
sudo yum install -y git gcc
```
3. 下载Hugo的最新版本
查看最新版本
```bash
curl -s https://api.github.com/repos/gohugoio/hugo/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'
```
```bash
wget https://github.com/gohugoio/hugo/releases/download/v0.142.0/hugo_0.142.0_Linux-64bit.tar.gz
```
请将`0.XX.0`替换为最新的Hugo版本号。
4. 解压下载的文件:
```bash
tar -zxvf hugo_extended_0.XX.0_Linux-64bit.tar.gz
tar -zxvf hugo_0.142.0_Linux-64bit.tar.gz
```
5. 将Hugo二进制文件移动到系统路径中
```bash
sudo mv hugo /usr/local/bin/
mv hugo /usr/bin/
```
6. 验证安装是否成功:
```bash
hugo version
```
8. 在具体目录执行命令创建blog站点
```bash
hugo new site myblog
```
目录结构
```
├── archetypes
│ └── default.md
├── hugo.toml # 博客站点的配置文件
├── content # 博客文章所在目录
├── data
├── layouts # 网站布局
├── static # 一些静态内容
└── themes # 博客主题
```
文章就放在content目录下的posts中只需要按照Markdown格式编写hugo就会读取到文章然后展示在博客中。
9. 安装主题:
```bash
mkdir myblog
cd myblog
mkdir themes
cd themes
git clone https://github.com/zhaohuabing/hugo-theme-cleanwhite.git
cp -r hugo-theme-cleanwhite/exampleSite/** ../
cd ..
hugo server
```
在myblog 目录下使用git 命令来下载主题
```bash
cd myblog
git clone https://github.com/Track3/hermit.git ./themes/hermit
git clone https://github.com/alanorth/hugo-theme-bootstrap4-blog.git themes/hugo-theme-bootstrap4-blog
```
下载下来的主题会放在themes 目录中:
```
└── hugo-theme-bootstrap4-blog
├── CHANGELOG.md
├── LICENSE.txt
├── README.md
├── archetypes
├── assets
├── exampleSite # 本主题示例内容
| ├── content # 示例博客文章
│ |-- static
│ |-- config.toml # 本主题配置
├── i18n
├── images
├── layouts
├── package-lock.json
├── package.json
├── screenshot.png
├── source
├── theme.toml
└── webpack.config.js
```
将exampleSite 目录中的内容复制到博客根目录myblog 中在myblog 目录中执行命令:
```bash
cp themes/hugo-theme-bootstrap4-blog/exampleSite/* ./ -r
```
10. 下载、解压 Go 语言:
```bash
wget https://golang.org/dl/go1.18.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.18.linux-amd64.tar.gz
```
11. 设置 Go 环境变量: 编辑你的 shell 配置文件(如 ~/.bashrc 或 ~/.profile添加以下行
```bash
export PATH=$PATH:/usr/local/go/bin
```
12. 加载新的环境变量:
```bash
source ~/.bashrc
```
或者
```bash
source ~/.profile
```
13. 验证 Go 安装:
```bash
go version
```
14. 更改配置
```ini
# Hugo 属性设置
# 网站地址
baseurl = "https://sayings.wantsong.life/"
# 网站语言
languageCode = "en-us"
# 网站title
title = "强哥语录"
# 主题的名字这个要跟myblog/themes 目录中的子目录的目录名一致
theme = "hugo-theme-bootstrap4-blog"
# home/category/tag 页面显示的文章数 (Default: 10)
paginate = 5
# home/category/tag 页面用于摘要的字数 (Default: 70)
summaryLength = 50
# optionally override the site's footer with custom copyright text
# copyright = "Except where otherwise noted, content on this site is licensed under a [Creative Commons Attribution 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/)."
#googleAnalytics = "UA-123-45"
#disqusShortname = "XYW"
# 博客链接的路径格式
[permalinks]
posts = "/:year/:month/:title/"
page = "/:slug/"
# 顶部栏
[[menu.navbar]]
name = "首页"
url = "http://localhost:1313"
# 侧边栏,可以写多个
[[menu.sidebar]]
name = "新浪"
url = "https://www.sina.com"
[[menu.sidebar]]
name = "Github"
url = "https://github.com"
# Theme 属性设置
#
[params]
# Site author
author = "作者名"
# homepage 页描述信息
description = "我的博客站点"
# Show header (default: true)
#header_visible = true
# Format dates with Go's time formatting
date_format = "Mon Jan 02, 2006"
# verification string for Google Webmaster Tools
#google_verify_meta = "BAi57DROASu4b2mkVNA_EyUsobfA7Mq8BmSg7Rn-Zp9"
# verification string for Bing Webmaster Tools
#bing_verify_meta = "3DA353059F945D1AA256B1CD8A3DA847"
# verification string for Yandex Webmaster Tools
#yandex_verify_meta = "66b077430f35f04a"
# Optionally display a message about the site's use of cookies, which may be
# required for your site in the European Union. Set the parameter below to a
# page where the user can get more information about cookies, either on your
# site or externally, for example:
#cookie_consent_info_url = "/cookie-information/"
#cookie_consent_info_url = "http://cookiesandyou.com"
# show sharing icons on pages/posts (default: true)
#sharingicons = true
# Display post summaries instead of content in list templates (default: true)
#truncate = true
# Disable the use of sub-resource integrity on CSS/JS assets (default: false)
# Useful if you're using a CDN or other host where you can't control cache headers
#disable_sri = false
[params.sidebar]
# Optional about block for sidebar (can be Markdown)
about = "我的博客[简单示例](http://localhost:1313/)."
# 侧边栏显示最近几条文章 (Default: 5)
#num_recent_posts = 2
[params.social]
# Optional, used for attribution in Twitter cards (ideally not a person
# for example: nytimes, flickr, NatGeo, etc).
# See: https://dev.twitter.com/cards/types/summary-large-image
twitter = "username"
# Default content language for Hugo 0.17's multilingual support (default is "en")
# See: https://github.com/spf13/hugo/blob/master/docs/content/content/multilingual.md
#DefaultContentLanguage = "en"
# Languages to render
#[languages.en]
#[languages.bg]
# Bulgarian date format is dd.mm.yyyy
#date_format = "02.01.2006"
# vim: ts=2 sw=2 et
```
15. **设置 baseURL**
编辑 Hugo 配置文件(`config.toml`、`config.yaml` 或 `config.json`),找到 `baseURL` 参数并设置为你的网站 URL。例如如果你使用的是 `config.toml` 文件:
```toml
baseURL = "https://yourdomain.com/"
```
16. **删除 `public` 文件夹**
```bash
rm -rf public
```
17. **重新生成网站内容**
运行 Hugo 命令重新生成网站内容:
```bash
hugo
```
18. **重新启动 Hugo 服务**
```bash
hugo server
```
19. **停止 Hugo 服务**
```bash
ps aux | grep hugo
kill <PID>
```
`<PID>` 替换为实际的进程 ID。
20. **编写博客文章**
你只需要按照Markdown 的格式编写自己的文章让后将写好的文章放在myblog/content/postshugo 就会读取到这片文章,并将这片文章展示在比的博客中。
与普通Markdown 文章不一样的地方是你需要在文章的开头写入如下结构的内容这些内容包含在三杠线之间在三杠线下边就是Markdown 的正文了:
```
---
文章属性内容
---
Markdown 正文
```
这些内容会被hugo 解析,作为当前文章的一些属性,常用的属性如下:
```
---
title: "文章标题" # 文章标题
author: "作者" # 文章作者
description : "描述信息" # 文章描述信息
date: 2015-09-28 # 文章编写日期
lastmod: 2015-04-06 # 文章修改日期
tags = [ # 文章所属标签
"文章标签1",
"文章标签2"
]
categories = [ # 文章所属标签
"文章分类1",
"文章分类2",
]
keywords = [ # 文章关键词
"Hugo",
"static",
"generator",
]
next: /tutorials/github-pages-blog # 下一篇博客地址
prev: /tutorials/automated-deployments # 上一篇博客地址
---
```