最近心血來潮,想要重新折騰一番自己的博客。于是,發現了Hugo比之前使用的Jekyll更好用。本文是個人參照Hugo官網 搭建個人博客的記錄。
Step 1. Install Hugo 下載、安裝Hugo
下載地址:https://github.com/spf13/hugo/releases。
根據自己的操作系統,下載對應的安裝包。我的操作系統是Win7 64bit
, 選擇了hugo_0.19_Windows-64bit.zip
。
下載完成之后,解壓的文件夾包含以下3個文件:
hugo_0.19_windows_amd64.exe
LICENSE.md
README.md
將 hugo_0.19_windows_amd64.exe
重命名為hugo.exe
。在你的軟件安裝盤新增名為hugo
的文件目錄,在里面在新建一個bin
子目錄。然后將hugo.exe
放到hugo\bin
目錄下。再將這個bin
目錄的路徑添加到系統環境變量
中。
完成以上步驟后,打開命令行輸入
hugo help
如果得到如下信息,說明安裝成功。
hugo is the main command, used to build your Hugo site.
Hugo is a Fast and Flexible Static Site Generator
built with love by spf13 and friends in Go.
Complete documentation is available at http://gohugo.io/.
Step 2. 創建一個名為blog的Hugo站點
我希望在E盤下創建站點,所以我先切換目錄
cd e:
e:
然后輸入命令:
hugo new site blog
得到如下提示創建成功的信息:
Congratulations! Your new Hugo site is created in E:\blog.
Just a few more steps and you're ready to go:
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".
Visit https://gohugo.io/ for quickstart guide and full documentation.
進入該目錄,可以看到自動生成了5個目錄和一個配置文件config.toml
|-- archetypes
|-- content #存放內容的目錄
|-- data
|-- layouts
|-- static #存放靜態資源(圖片,css,js)
|-- themes #存放主題
|-- config.toml #配置文件
Step 3. 添加內容
切換到該目錄下,然后輸入如下命令,會在content
的目錄下創建post
目錄,在post
目錄下創建名為test.md
的文件。
cd blog
hugo new post/test.md
提示文件創建成功:
E:\blog\content\post\test.md created
用文本編輯器打開文件test.md
文件
可以看到如下內容:
+++
date = "2017-01-02T17:45:06+08:00" #創建文件的時間
title = "test" #文件的標題
draft = true # 是否為草稿
+++
上述內容為自動創建的與文章有關的內容。自己也可以在兩個+++
之間添加如下內容:
image = "hugo.png" #指定圖片。
category = "test" #文章的類別
tags = ["Hugo", "intro"] #文章的標簽分類。
url = "new_start" #該文章訪問時的相對的url地址,默認為文件名。
更多的設置,可以參考官方文檔。
以后寫博客文章就是這樣創建markdown
文件, 之后通過Hugo
編譯成靜態的html文件。
Step 4. 添加主題
官方提供了多種主題可供選擇,具體在 https://themes.gohugo.io/ 可以找到。
找到想要的主題后,切換到themes
目錄(該目錄可以存放多個不同的主題)。
cd themes
由于需要使用到Git下載主題,以及版本管理和代碼推送。需要事先下載安裝好Git,關于如何安裝和使用Git,可以參考我之前的一篇文章:Git學習筆記。
把選定的主題下載到themes
目錄下。
git clone https://github.com/dim0627/hugo_theme_robust.git
Step 5. 啟動服務,本地預覽
先從themes
目錄下退回到blog
目錄, 然后啟動服務。
cd ..
hugo server --theme=hugo_theme_robust --buildDrafts
在瀏覽器中打開 [http://localhost:1313/](https://themes.gohugo.io/)
Step 6. 修改配置文件config.toml
languageCode = "zh-cn"
title = "Frank Wang's Coding World"
baseurl = "http://www.wangxingfeng.com/"
[Params]
Author ="Frank Wang"
Step 7. 生成網站
1.改變文章draft
(草稿)狀態:
hugo undraft content/post/*.md
2. 啟動hugo
, 生成發布文件到public
目錄下。
hugo --theme=hugo-icarus-theme
Step 8. 托管到GitHub Pages
1. 使用Git
來進行版本管理
git init
echo "/public/" >> .gitignore
echo "/themes/" >> .gitignore
git add --all
git commit -m "Initial commit"
2. 創建Git
遠程倉庫:
登錄你的GitHub
. 創建一個新的倉庫,倉庫名為Github用戶.github.io
比如我的是frankwang0909.github.io
.
3. 添加Git
遠程倉庫,并提交代碼。
cd public
git init
git remote add origin git@github.com:frankwang0909/frankwang0909.github.io.git
git add --all
git commit -m "blog added"
git push -u origin master
4.以后有內容改動,提交代碼.
(cd ..; hugo --theme=hugo_theme_robust)
git add --all
git commit -m "<some change message>"
git push -u origin master
本文首發于我的個人博客www.wangxingfeng.com,想要了解更多,請點擊訪問。