從0開始建立Github個人博客(hugo&PaperMod)
github提供給每個用戶一個網址,用戶可以建立自己的靜態網站。
一、Hugo
hugo是一個快速搭建網站的工具,由go語言編寫。
1.安裝hugo
到hugo的github標簽頁Tags · gohugoio/hugo選擇一個版本,下載對應的安裝包。比如hugo_extended_withdeploy_0.147.0_windows-amd64.zip
。
解壓后,在根目錄打開cmd,輸入
hugo new site YourSiteName
為你的網站建立文件夾。YourSiteName
更改為你的網站的名字。
根目錄會出現YourSiteName文件夾。
3.將根目錄的hugo.exe復制到YourSiteName里。
在YourSiteName文件夾里打開cmd,輸入
hugo server -D
會返回如下信息:
| EN
-------------------+-----Pages | 11Paginator pages | 0Non-page files | 0Static files | 0Processed images | 0Aliases | 2Cleaned | 0Built in 79 ms
Environment: "development"
Serving pages from disk
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop
在瀏覽器中輸入http://localhost:1313/
,顯示Page Not Found,說明服務器正常運行,但是此時網站還沒有頁面。
2.選擇網站主題
在Hugo Themes選擇你想要的theme,然后根據theme的安裝說明操作就行了。
在此以PaperMod為例。官方安裝教程界面:Installation · adityatelange/hugo-PaperMod Wiki
安裝PaperMod,可以:
-
在你的網站的theme文件夾使用:
git clone https://github.com/adityatelange/hugo-PaperMod themes/PaperMod --depth=1
-
或者,在Tags · adityatelange/hugo-PaperMod選擇版本,下載zip并解壓到theme文件夾。
在你的網站的根文件夾里的hugo.yml
文件里添加
theme: ["PaperMod"]
3.新建一個筆記
在你的網站的根頁面下使用cmd:
hugo new posts/first.md
YourSiteName/content/posts/first.md
就會建立,打開后,內容為:
---
date: '2025-05-01T18:41:05+08:00'
draft: true
title: 'first'
---
這三條短線圍起來的是該筆記的屬性。第一行是創建時間;第二行為false時表示草稿狀態,改為true才會顯示在網站中;第三行為該筆記的標題。之后還可以添加其他的屬性。
打開http://localhost:1313/
,刷新后就能看到剛才創建的筆記了。如果沒有就重新hugo server -D
。
你可以通過cmd,或者直接新建md文件來添加筆記。
4.定制個人博客
4.1添加菜單
在hugo.yml
文件中添加:
menu:main:- identifier: categoriesname: categoriesurl: /categories/weight: 10- identifier: tagsname: tagsurl: /tags/weight: 20- identifier: examplename: example.orgurl: https://example.orgweight: 30
在網站的右上角就能看到菜單了
4.2置頂帖子
在筆記的md
文件里添加:
---
...
weight: 1
---
weight為正整數,表示筆記順序。放到最頂上就設為1。
4.3hugo.yaml的可選項
hugo.yaml
是網站根目錄的配置文件
baseURL: https://Rook1eChan.github.io
languageCode: zh-cn
title: Chan's Bolg
theme: ["PaperMod"]
buildDrafts: falseparams: ShowBreadCrumbs: true ShowReadingTime: falseShowShareButtons: falseShowCodeCopyButtons: truefuseOpts:isCaseSensitive: falseshouldSort: truelocation: 0distance: 1000threshold: 0.4minMatchCharLength: 0keys: ["title", "permalink", "summary", "content"]homeInfoParams: Title: "你好,歡迎來到我的博客 \U0001F44B"Content: "welcome!"socialIcons: - name: githuburl: "https://github.com/Rook1eChan"# - name: twitter# url: "twitter.com"# - name: facebook# url: "facebook.com"assets:favicon: "/apple-touch-icon.png"label:icon: /apple-touch-icon.pngiconHeight: 35outputs:home:- HTML- RSS- JSONmenu:main:# - identifier: categories# name: categories# url: /categories/# weight: 10# - identifier: tags# name: tags# url: /tags/# weight: 20- identifier: 搜索name: searchurl: /search/weight: 25
-
baseURL
: 網站的基礎URL,這里是 “https://Rook1eChan.github.io”,必須要寫,不然導航出現錯誤。不要寫example.com -
languageCode
: 網站語言代碼,“zh-cn” 表示簡體中文 -
title
: 網站標題 -
theme
: 使用的主題 -
buildDrafts
: false 表示不設置草稿文章,所有文章都會被展示 -
顯示相關:
ShowBreadCrumbs
: true 顯示面包屑導航ShowReadingTime
: false 不顯示文章閱讀時間ShowShareButtons
: false 不顯示分享按鈕ShowCodeCopyButtons
: true 顯示代碼塊的復制按鈕
-
搜索功能 (
fuseOpts
):- 配置了基于 Fuse.js 的搜索功能參數,包括不區分大小寫、排序方式等
-
主頁信息 (
homeInfoParams
):Title
:標題Content
:內容
-
主頁社交媒體圖標 (
socialIcons
):- 只啟用了 GitHub 鏈接,指向 “https://github.com/Rook1eChan”
- Twitter 和 Facebook 鏈接
-
資源 (
assets
):- 設置了網站圖標 (favicon) 為 “/apple-touch-icon.png”
-
標簽 (
label
):- 設置了圖標及其高度
-
指定了主頁的輸出格式為 HTML、RSS 和 JSON
-
主菜單 (
main
) 中只配置了一個 “搜索” 項:- 標識符: “搜索”
- 名稱: “search”
- URL: “/search/”
- 權重: 25 (用于菜單項排序)
-
分類和標簽菜單項自選
二、在GitHubPage部署網站
基本思路:建立兩個倉庫,一個網站倉庫負責展示頁面,另一個源倉庫負責存儲源碼、更新內容并自動更新同步到網站倉庫。
1.建立網站倉庫
在Github頁面點擊最上面的加號-New repository
Repository name
填寫 你的GitHub用戶名.github.io
,這樣GitHub才會把它識別為網站倉庫
選擇Public
點擊綠色的Create repository
2.建立源倉庫
同上建立倉庫,隨便命名,Public或Private都行
這里我命名為mywebsite
3.GitHub 個人訪問令牌 (Token) 生成&配置
- 點擊右上角 頭像
- 選擇
Settings
- 左側菜單選擇
Developer settings
- 選擇
Personal access tokens
→Tokens (classic)
- 點擊
Generate new token
→Generate new token (classic)
- 設置 Token 信息:
- Token name:輸入名稱(如
mywebsite
) - Expiration:選擇
No expiration
(永不過期) - 權限勾選:
- ?
repo
(全倉庫權限) - ?
admin:repo_hook
(倉庫管理權限)
- ?
- Token name:輸入名稱(如
- 點擊綠色按鈕
Generate token
- 重要:立即復制生成的密鑰并妥善保存(離開頁面后將無法再次查看)
- 進入源倉庫
- 點擊
settings
- 左側
Secrets and variables-Actions
New repository secret
- 填寫剛才的名稱和密鑰
- Add sercet
- 點擊
4.配置workflow腳本
在本地網站根目錄新建文件夾及文件.github/workflows/hugo.yaml
寫入:
name: github pages # 名字自取on:push:branches:- main jobs:deploy: # 任務名自取runs-on: ubuntu-latest # 在什么環境運行任務steps:- uses: actions/checkout@v2 # 引用actions/checkout這個action,與所在的github倉庫同名with:submodules: true # Fetch Hugo themes (true OR recursive) 獲取submodule主題fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod- name: Setup Hugo # 步驟名自取uses: peaceiris/actions-hugo@v2 # hugo官方提供的action,用于在任務環境中獲取hugowith:hugo-version: 'latest' # 獲取最新版本的hugoextended: true- name: Buildrun: hugo --minify # 使用hugo構建靜態網頁- name: Deployuses: peaceiris/actions-gh-pages@v3 # 一個自動發布github pages的actionwith:# github_token: ${{ secrets.GITHUB_TOKEN }} 該項適用于發布到源碼相同repo的情況,不能用于發布到其他repoexternal_repository: Rook1eChan/Rook1eChan.github.io # 發布到哪個repopersonal_token: ${{ secrets.MYWEBSITE2 }} # 發布到其他repo需要提供上面生成的personal access tokenpublish_dir: ./public # 注意這里指的是要發布哪個文件夾的內容,而不是指發布到目的倉庫的什么位置,因為hugo默認生成靜態網頁到public文件夾,所以這里發布public文件夾里的內容publish_branch: main # 發布到哪個branch
只需要更改personal_token
和external_repository
5. SSH 密鑰配置
-
檢查是否已有 SSH Key
-
Windows:
進入C:\Users\你的用戶名\.ssh
,查看是否存在id_rsa
(私鑰)和id_rsa.pub
(公鑰)文件。- 若有,說明已生成過 SSH Key,可直接使用。
- 若無,需重新生成。
-
Linux:
cd ~/.ssh ls
檢查是否存在
id_rsa
和id_rsa.pub
文件。
-
-
生成 SSH Key(若無)
運行以下命令(替換xxx@xxx.com
為你的 GitHub 注冊郵箱):
ssh-keygen -t rsa -C "xxx@xxx.com"
- 連續按 3 次回車(使用默認路徑,不設密碼)。
- 生成的文件:
id_rsa
:私鑰(切勿泄露)。id_rsa.pub
:公鑰(需添加到 GitHub)。
- 將公鑰添加到 GitHub
- 復制公鑰內容(
id_rsa.pub
): - 登錄 GitHub → 點擊頭像 → Settings → SSH and GPG Keys → New SSH Key。
- 測試 SSH 連接
在終端運行:
ssh -T git@github.com
- 若顯示
Hi 你的用戶名!
,說明配置成功。
之后clone或push時都選擇SSH地址,而不是https地址。
6.上傳
本地網站根目錄使用cmd,git@github.com:XXX/mywebsite.git
改為源倉庫地址:
git init
git add .
git remote add origin git@github.com:XXX/mywebsite.git
git commit -m "Update"
git push -u origin main
然后在源倉庫的Action下,能看到正在Deploy,變綠色說明成功。此時網站倉庫已自動更新了內容。
進入網站倉庫-settings-Pages
-Build and deployment
選擇Deploy from a branch
剛才workflow
腳本里寫的是main
,這里就選擇main
然后進入xxx.github.io,就可以看到你的網站了!🎉
三、如何更新網站內容
不要在github和本地同時更改內容!不然會導致內容不同步,無法push。
最好是一直都在本地更改,然后push到源倉庫。
1.本地更改后,比如新建了筆記,在網站根目錄使用cmd:
不要復制注釋!
git init //初始化git文件夾git add . //添加變更內容git remote add origin git@github.com:XXX/mywebsite.git //最后一項改為源倉庫的地址,如果使用ssh連接的就復制ssh地址git commit -m "new" //設置本次操作的名稱,new可以隨便改git push -u origin main //把本地文件push到github上,增量更新
常見問題:
git push -u origin main的時候報錯:
error: src refspec master does not match any
error: failed to push some refs to ‘github.com:Rook1eChan/mywebsite.git’使用git branch -a,查看branch的名稱是不是main。如果是master,就把main改為master。
To github.com:Rook1eChan/mywebsite.git ! [rejected] main -> main (fetch first) error: failed to push some refs to ‘github.com:Rook1eChan/mywebsite.git’ hint: Updates were rejected because the remote contains work that you do not
說明你的GitHub和本地不同步。不建議強制合并,可以把GitHub整個repo clone到本地另一個文件,把變化的文件手動更改,再把新文件夾push上去。
感謝GitHub、Hugo和Deekseek。