在Linux系统搭建Hugo博客
簡介
Hugo是由Go語言實(shí)現(xiàn)的靜態(tài)網(wǎng)站生成器。簡單、易用、高效、易擴(kuò)展、快速部署。
該篇文章是在Linux/Ubuntu系統(tǒng)下安裝搭建 Hugo 博客。
本博客參考了該視頻:https://www.bilibili.com/video/BV1q4411i7gL
一、安裝Hugo
到 https://github.com/gohugoio/hugo/releases 查看與CPU對應(yīng)的版本下載
我用 sudo apt-get install 命令下載的 hugo 是16年的版本,所以我建議到官網(wǎng)手動下載 deb 安裝程序,或者復(fù)制下載地址,用命令
wjy@ubuntu:~$ wget https://github.com/gohugoio/hugo/releases/download/v0.80.0/hugo_0.80.0_Linux-64bit.deb? 進(jìn)行下載,下載完后安裝:
wjy@ubuntu:~/Downloads$ sudo dpkg -i hugo_0.80.0_Linux-64bit.deb (正在讀取數(shù)據(jù)庫 ... 系統(tǒng)當(dāng)前共安裝有 183289 個文件和目錄。) 正準(zhǔn)備解包 hugo_0.80.0_Linux-64bit.deb ... 正在將 hugo (0.80.0) 解包到 (0.80.0) 上 ... 正在設(shè)置 hugo (0.80.0) ...這樣表示成功。驗(yàn)證(查看版本):
wjy@ubuntu:~$ hugo version Hugo Static Site Generator v0.80.0-792EF0F4 linux/amd64 BuildDate: 2020-12-31T13:37:58Z二、用hugo來生成博客
命令:
wjy@ubuntu:~$ hugo new site myblog Congratulations! Your new Hugo site is created in /home/wjy/myblog.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/ orcreate your own with the "hugo new theme <THEMENAME>" command. 2. Perhaps you want to add some content. You can add single fileswith "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.myblog 為博客的目錄名,這個看個人意愿取名
三、下載并設(shè)置主題
主題官網(wǎng):https://themes.gohugo.io ,找到想要的主題,點(diǎn)擊去,復(fù)制下載命令,下載到 myblog 目錄下的 themes 目錄下,
例如,在 themes 目錄下命令:
wjy@ubuntu:~/myblog/themes$ git clone https://github.com/vaga/hugo-theme-m10c.git m10c 正克隆到 'm10c'... remote: Enumerating objects: 22, done. remote: Counting objects: 100% (22/22), done. remote: Compressing objects: 100% (17/17), done. remote: Total 345 (delta 3), reused 13 (delta 3), pack-reused 323 接收對象中: 100% (345/345), 459.21 KiB | 236.00 KiB/s, 完成. 處理 delta 中: 100% (118/118), 完成. 檢查連接... 完成。下載后目錄名默認(rèn)為鏈接的最后的“/”后的目錄名,在鏈接后面加 m10c,相當(dāng)于把下載的主題放在 m10c 目錄下,而不是使用默認(rèn)的名字,需要注意,下載主題要使用到 git 命令。下載 git :
wjy@ubuntu:~/myblog/themes$ sudo apt-get install git驗(yàn)證(查看版本):
wjy@ubuntu:~/myblog$ git version git version 2.7.4四、在本地啟動博客
在 myblog 目錄下命令:
root@ubuntu:/home/wjy/myblog# hugo server -t m10c --buildDrafts Start building sites … | EN -------------------+-----Pages | 7 Paginator pages | 0 Non-page files | 0 Static files | 1 Processed images | 0 Aliases | 1 Sitemaps | 1 Cleaned | 0 Built in 11 ms Watching for changes in /home/wjy/myblog/{archetypes,content,data,layouts,static,themes} Watching for config changes in /home/wjy/myblog/config.toml Environment: "development" Serving pages from memory 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-t:指定主題(主題目錄名)
訪問 Web Server is available at http://localhost:1313/ (bind address 127.0.0.1) 里的本地網(wǎng)址,即可打開博客網(wǎng)頁,如下:
五、寫一篇文章
1、生成md文件
在 myblog 目錄下:
root@ubuntu:/home/wjy/myblog# hugo new post/blog.md /home/wjy/myblog/content/post/blog.md created生成的 md 文件在myblog/context/post目錄下:
root@ubuntu:/home/wjy/myblog/content/post# ls blog.md2、編寫
可以現(xiàn)在Windows下寫好博客,比如我這篇,也是在Windows下編寫的,編寫完后,保存,用記事本打開,全選,復(fù)制,在Linux下編輯blog.md,打開文件命令:
root@ubuntu:/home/wjy/myblog/content/post# vim blog.md進(jìn)入編輯頁面:
輸入 i ,在 ~ 前,--- 后右鍵,粘貼,保存退出(鍵入 ESC : wq ,回車),這樣,文章就寫好了,可以重新運(yùn)行看看文章。
六、將個人博客部署到遠(yuǎn)程服務(wù)器
1、登錄GitHub官網(wǎng)
①新建一個倉庫
②填寫倉庫地址,填寫格式為:用戶名小寫.github.io ,例如我的倉庫地址:wujvya.github.io ,那么我就可以通過 https://wujvya.github.io 來訪問我的博客
③創(chuàng)建倉庫
2、生成靜態(tài)網(wǎng)頁
命令:
root@ubuntu:/home/wjy/myblog# hugo --theme=m10c --baseUrl="https://wujvya.github.io" --buildDrafts Start building sites … | EN -------------------+-----Pages | 10 Paginator pages | 0 Non-page files | 0 Static files | 1 Processed images | 0 Aliases | 2 Sitemaps | 1 Cleaned | 0 Total in 25 ms其中:
--theme=m10c 指定主題
--baseUrl="https://wujvya.github.io" 指定倉庫地址
會把博客編譯成網(wǎng)頁形式,在myblog/public目錄里面
3、上傳到GitHub
a、把 public 文件夾創(chuàng)建成 git 倉庫:
root@ubuntu:/home/wjy/myblog/public# git init 初始化空的 Git 倉庫于 /home/wjy/myblog/public/.git/b、把所有的文件添加進(jìn)去:
root@ubuntu:/home/wjy/myblog/public# git add *c、提交:
root@ubuntu:/home/wjy/myblog/public# git commit -m "第一次提交" [master (根提交) 2033233] 第一次提交15 files changed, 436 insertions(+)create mode 100644 404.htmlcreate mode 100644 avatar.jpgcreate mode 100644 categories/index.htmlcreate mode 100644 categories/index.xmlcreate mode 100644 css/main.min.88e7083eff65effb7485b6e6f38d10afbec25093a6fac42d734ce9024d3defbd.csscreate mode 100644 index.htmlcreate mode 100644 index.xmlcreate mode 100644 page/1/index.htmlcreate mode 100644 post/blog/index.htmlcreate mode 100644 post/index.htmlcreate mode 100644 post/index.xmlcreate mode 100644 post/page/1/index.htmlcreate mode 100644 sitemap.xmlcreate mode 100644 tags/index.htmlcreate mode 100644 tags/index.xmld、本地倉庫(public文件夾)與遠(yuǎn)程倉庫關(guān)聯(lián):
root@ubuntu:/home/wjy/myblog/public# git remote add origin https://github.com/WuJvya/wujvya.github.io.git注意:關(guān)聯(lián)的地址為 打開你新建的倉庫的全地址(瀏覽器地址欄的地址) 加上 .git
e、把博客推送到遠(yuǎn)程倉庫(把代碼放到遠(yuǎn)程倉庫):
先輸入下面的命令,用于保存賬號和密碼(第一次要輸入賬號和密碼,之后就不用了)
root@ubuntu:/home/wjy/myblog/public# git config --global credential.helper store root@ubuntu:/home/wjy/myblog/public# git push -u origin master Username for 'https://github.com': 1813114396@qq.com Password for 'https://1813114396@qq.com@github.com': 對象計數(shù)中: 26, 完成. Delta compression using up to 2 threads. 壓縮對象中: 100% (21/21), 完成. 寫入對象中: 100% (26/26), 6.45 KiB | 0 bytes/s, 完成. Total 26 (delta 9), reused 0 (delta 0) remote: Resolving deltas: 100% (9/9), done. To https://github.com/WuJvya/wujvya.github.io.git* [new branch] master -> master 分支 master 設(shè)置為跟蹤來自 origin 的遠(yuǎn)程分支 master。輸入完命令后,需要你輸入GitHub的賬號和密碼,賬號可以為用戶名或者郵箱,完成后刷新遠(yuǎn)程倉庫,就可以看到提交的文件。
好了,到這里,你的博客就搭建完成了。
七、新增文章
生成文章后,相當(dāng)于執(zhí)行六、將個人博客部署到遠(yuǎn)程服務(wù)器中的第2步及之后,除下第3.d步的所有步驟。
新建博客文章文件:
root@ubuntu:/home/wjy/myblog# hugo new post/blog2.md /home/wjy/myblog/content/post/blog2.md created生成靜態(tài)網(wǎng)頁:
root@ubuntu:/home/wjy/myblog# hugo --theme=m10c --baseUrl="https://wujvya.github.io" --buildDrafts Start building sites … | EN -------------------+-----Pages | 11 Paginator pages | 0 Non-page files | 0 Static files | 1 Processed images | 0 Aliases | 2 Sitemaps | 1 Cleaned | 0 Total in 28 ms添加文件:
root@ubuntu:/home/wjy/myblog/public# git add *提交:
root@ubuntu:/home/wjy/myblog/public# git commit -m "第二次提交" [master 6bc7870] 第二次提交6 files changed, 132 insertions(+), 7 deletions(-)create mode 100644 post/blog2/index.html推送到遠(yuǎn)程倉庫:
root@ubuntu:/home/wjy/myblog/public# git push -u origin master Username for 'https://github.com': 1813114396@qq.com Password for 'https://1813114396@qq.com@github.com': 對象計數(shù)中: 10, 完成. Delta compression using up to 2 threads. 壓縮對象中: 100% (9/9), 完成. 寫入對象中: 100% (10/10), 1.10 KiB | 0 bytes/s, 完成. Total 10 (delta 7), reused 0 (delta 0) remote: Resolving deltas: 100% (7/7), completed with 5 local objects. To https://github.com/WuJvya/wujvya.github.io.git2033233..6bc7870 master -> master 分支 master 設(shè)置為跟蹤來自 origin 的遠(yuǎn)程分支 master。八、疑難解答
本文圖片有時候無法正常顯示,可切換手機(jī)瀏覽(在寫這篇文章時遇到過)。
若有疑問或發(fā)現(xiàn)不足,請留言或至本人郵箱:1813114396@qq.com
祝每位程序猿在以后的日子里蒸蒸日上!
總結(jié)
以上是生活随笔為你收集整理的在Linux系统搭建Hugo博客的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA Calendar详解
- 下一篇: 构建工具 | Gulp压缩JavaScr