misterli's Blog.

使用hugo搭建博客

字数统计: 727阅读时长: 3 min
2020/09/01

使用hugo搭建个人博客

Hugo是由Go语言实现的静态网站生成器。简单、易用、高效、易扩展、快速部署。

项目地址:https://github.com/gohugoio/hugo.git

安装hugo

这里使用二进制安装,到 Hugo Releases 下载对应的操作系统版本的Hugo二进制文件

1
2
3
wget https://github.com/gohugoio/hugo/releases/download/v0.69.2/hugo_0.69.2_Linux-64bit.tar.gz
tar xf hugo_0.69.2_Linux-64bit.tar.gz
mv hugo /usr/bin/

image-20200501161515193

解压后得到一个hugo文件,我们将文件复制到/usr/bin/下

生成站点

使用hugo在当前目录下生成一个名为lishuai的站点

1
2
3
hugo new site lishuai	
# 我们还能指定生成站点的位置
hugo new site /path/lishuai

站点目录结构:

1
2
3
4
5
▸ archetypes/
▸ content/
▸ layouts/
▸ static/
config.toml

安装主题

请参阅themes.gohugo.io以获取主题列表。这里使用了Ananke主题

1
2
3
4
# 创建主题存放目录
mkdir themes
git clone https://github.com/budparr/gohugo-theme-ananke.git themes/ananke

使用主题有两种方法:

1、写在config.toml文件中

1
echo 'theme = "ananke"' >> config.toml

2、启动时指定参数 –theme=ananke

创建文章

创建一个 about 页面:

1
hugo new about.md

about.md 自动生成到了 content/about.md ,打开 about.md 看下:

image-20200501162614407

内容是 Markdown 格式的,+++ 之间的内容是 TOML 格式的,根据你的喜好,你可以换成 YAML 格式(使用 --- 标记)或者 JSON 格式。

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

1
hugo new post/first.md

打开编辑 post/first.md

1
2
3
4
5
6
7
8
9
10
11
---
title: "First"
date: 2020-05-01T16:27:17+08:00
draft: true
---
### Hello Hugo

1. aaa
1. bbb
1. ccc

运行hugo

在站点根目录执行 Hugo 命令

1
hugo server --bind=0.0.0.0 --port=8081 --theme=ananke --baseURL=http://hugo.baixue.fun -D

参数说明:

1
2
3
4
5
--bind 监听地址
--port 端口
--theme 使用的主题名
--baseURL 服务要使用的域名
-D 包括被标记为draft的文章

我们可以把参数写在config.toml中作为默认参数使用

image-20200501163929152

启动后我们可以打开http://hugo.baixue.fun:8081

image-20200501164022043

我们也能看到我们之前修改的first.md这篇文档

nginx 结合hugo

很多时候我们的80端口是提供给了nginx使用,并且hugo虽然提供了小型web服务器,但是我们还是想用nginx 作为入口,此时我们可以用hugo生成静态页面然后nginx 反向代理访问

生成静态文件

在站点根目录执行 Hugo 命令

1
hugo -D

默认会在当前目录下生成一个public目录存放静态文件,可以使用-d /path 指定生成目录位置

nginx反向代理

我们将生成目录移动的/usr/share/nginx/html下

1
mv public /usr/share/nginx/html/

添加配置文件/etc/nginx/conf.d/hugo.conf

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name hugo.baixue.fun;
keepalive_timeout 70;

location / {
root /usr/share/nginx/html/public;
index index.html;
}
}

启动nginx ,访问http://hugo.baixue.fun

image-20200501165602417

CATALOG
  1. 1. 使用hugo搭建个人博客
    1. 1.1. 安装hugo
    2. 1.2. 生成站点
      1. 1.2.1. 安装主题
    3. 1.3. 创建文章
    4. 1.4. 运行hugo
    5. 1.5. nginx 结合hugo
      1. 1.5.1. 生成静态文件
      2. 1.5.2. nginx反向代理