基于caddy搭建hugo静态博客,就如本站。

Caddy

Caddy 镜像

spanda/caddy,具体dockerfile可以参见 ysicing/dockerfiles

Caddyfile

blog.pt.spanda.io {
    gzip
    log / /var/log/caddy/blog.pt.log "{remote} {when} {method} {uri} {proto} {status} {size} {>User-Agent} {latency}" {
   	rotate_size 50
	rotate_age  90
	rotate_keep 20
	rotate_compress
    }
    tls root@ysicing.net
    header / {
    	Strict-Transport-Security "max-age=31536000;includeSubDomains;preload"
    	Access-Control-Allow-Origin  *
	    Access-Control-Allow-Methods "GET, POST, OPTIONS"
    	X-XSS-Protection "1; mode=block"
	    X-Content-Type-Options "nosniff"
	    X-Frame-Options "SAMEORIGIN"
	    -Server
	}
    ipfilter /hao /secret {
        rule allow
        ip 10.10.10.3
    }
    root /proxy/data/pt/hugo-blog/public
    git {
        repo git@code.spanda.io:pt/hugo-blog
        path /proxy/data/pt/hugo-blog
        branch master
        key      /root/.ssh/id_rsa
        then hugo --destination=/proxy/data/pt/hugo-blog/public
        hook /webhook Github666
        hook_type gogs
        clone_args --recursive
        pull_args --recurse-submodules
        interval 86400
    }
    hugo
}

如上主要针对我的博客系统哈。

github类型大概如下

    git github.com/ysicing/handbook {
        path /proxy/data/ysicing/handbook
        branch master
        then hugo --destination=/proxy/data/ysicing/handbook/public
        hook /webhook Github666
        hook_type github
        clone_args --recursive
        pull_args --recurse-submodules
    }
    hugo

Hugo

安装hugo

# macOS
brew install hugo
# Debian
apt install hugo

创建与配置博客工程

创建博客站点,我的本地博客目录存放在~/Work/pt/hugo-blog

cd ~/Work/pt/
hugo new site hugo-blog
cd hugo-blog

# 添加博客主题
git clone https://github.com/kakawait/hugo-tranquilpeak-theme.git themes/hugo-tranquilpeak-theme

# 修改配置文件
cp -f themes/hugo-tranquilpeak-theme/exampleSite/config.toml config.
toml

# 高亮主题
mkdir -p static/css static/js
hugo gen chromastyles --style=fruity > static/css/fruity.css

大概调整配置文件

baseURL = "https://blog.pt.spanda.io"
languageCode = "zh-cn"
defaultContentLanguage = "zh-cn"
hasCJKLanguage = true #文章内容包含汉字
title = "Spanda Handbook"
theme = "hugo-tranquilpeak-theme"
disqusShortname = "xxx"
googleAnalytics = "xxx"
paginate = 7 # 文章分页
canonifyurls = true 
enableEmoji = true # 启用表情
enableRobotsTXT = true

# 语法高亮
pygmentsCodeFences = true
pygmentsCodeFencesGuessSyntax = true
pygmentsUseClasses = true
pygmentsStyle = "fruity"

# 博客文章链接
[permalinks]
  post = "/:year/:month/:day/:slug/"

[taxonomies]
  tag = "tags"
  category = "categories"
  archive = "archives"

[author]
  name = "ysicing"
  bio = "默默努力"
  job = "搬砖"
  location = "中国 北京"
  gravatarEmail = "ops.ysicing@gmail.com"


[params]
  dateFormat = "2006年1月2日 15时04分05秒"

  keywords = ["development", "linux", "kubernetes", "docker", "Go", "python"]

  syntaxHighlighter = "highlight.js"

  clearReading = true

  hierarchicalCategories = true

  description = "A handbook maintained by ysicing."

  sidebarBehavior = 2

  coverImage = "doge.jpg"

  imageGallery = true

  thumbnailImage = true
  thumbnailImagePosition = "bottom"
  autoThumbnailImage = true

  [[params.customCSS]]
     href = "css/fruity.css"

  [[params.sharingOptions]]
    name = "Weibo"
    icon = "fa-weibo"
    url = "http://service.weibo.com/share/share.php?url=%s"

配置默认文档模板

默认post

---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
Categories: []
Description: ""
Tags: []
draft: false
---
xxxxxx
<!--more-->
<!-- toc -->
# Title

默认页面

---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
Categories: []
Description: ""
Tags: []
draft: false
comments: false
showMeta: false
showActions: false
---
xxxxxx
<!--more-->
<!-- toc -->
# Title

运行

hugo server -D

访问localhost:1313即可


参考文档 Hugo Docs