原文地址:
- 個人博客:https://vimior.github.io/article/193aafc44fcaf793/
- CSDN:https://blog.csdn.net/Vimor/article/details/90753844
前言
本文將使用Hexo+Github一步步搭建屬于自己的博客
不多說,先上圖
搭建步驟
-
注意
-
本文中所有命令或配置中以#開頭的表示注釋,僅僅用于說明
# 注釋,僅僅用于說明 echo "hello" # 注釋,僅僅用于說明,#號開始的屬于注釋,不屬于命令
-
-
安裝Node.js
這里不對Node.js的安裝做講解,Node.js官網下載安裝即可(https://nodejs.org/zh-cn/)
安裝配置好Node.js后可以通過以下命令查看node和npm的版本
node -v # 說明:由于我電腦的node不是新裝的,所以是v8.12.0版本 npm -v # 說明:我這里是6.4.1版本的npm
-
安裝Git
這里不對Git的安裝做講解,可以參考https://git-scm.com/book/zh/v2/起步-安裝-Git
安裝好后,通過以下命令可以查看git的版本
git --version # 說明:我這里是git version 2.19.1.windows.1
-
創建Github倉庫
打開github并登錄(https://github.com/)
-
新建倉庫
-
選中Repositories欄,然后點擊右側的New按鈕創建
image -
設置倉庫名字
- 倉庫名必須嚴格遵守格式:用戶名.github.io,不要隨意起名,后續會有麻煩,這里vimior是我的用戶名)
- 記得選中Public和勾選Initialize this repository with a README
image
-
-
查看Github Pages
選中剛剛創建的倉庫的Settings欄,找到Github Pages的設置項
此時Github Pages會顯示如下,表明當前倉庫可以作為Github Pages
image
-
安裝Hexo環境
-
安裝Hexo
npm install hexo -g # 說明:這里-g參數是為了把hexo安裝到全局 hexo -v # 說明:查看hexo是否安裝成功,獲取其版本信息
新建一個工程目錄(這里是vimior_blog),然后進入這個目錄
-
初始化Hexo
hexo init # 說明:此命令會在當前文件夾下生成一個hexo工程
image -
生成靜態頁面
hexo generate # 可以簡寫成 hexo g
-
啟動本地服務器,啟動后可以通過http://localhost:4000來訪問
hexo server # 可以簡寫成 hexo s
-
新建博文
通過hexo new <文章名>來新建或者直接在工程目錄下的source/_post目錄下創建一個md文件即可,然后重新生成靜態頁面并啟動服務器即可
hexo new "文章名" # 此命令會在工程目錄下的source/_post目錄下生成一個<文章名>.md的文件 hexo g && hexo s # 此命令重新生成靜態文件并啟動服務器,此時即可通過瀏覽器訪問到我們剛創建的博文了,修改該md文件即可修改博文內容
博文格式如下
--- title: 博文的標題 --- 博文內容 # 注意:如果需要給博文指定類別,可以在頭部使用categories: "xxx類別名",參考如下 --- title: 博文的標題 categories: "測試類別" ---
-
-
配置Hexo
注意:這里我們先統一把需要的包安裝好,具體配置會在配置里面用注釋說明
-
安裝hexo-theme-next主題(配置使用在后面統一說明)
# 說明:以下命令會從https://github.com/theme-next/hexo-theme-next下載內容到當前目錄下的themes/next里面 git clone https://github.com/theme-next/hexo-theme-next themes/next
-
安裝hexo-helper-live2d(用于生成Live2D看板娘)
npm install hexo-helper-live2d --save # 下面的命令是安裝live2d的模型,這里選擇live2d-widget-model-haruto # 參考https://github.com/xiazeyu/live2d-widget-models選擇自己需要的 # 如果選擇別的模型,在配置_config.yml文件里的live2d->model->use的值也要對應修改 npm install live2d-widget-model-haruto --save
-
安裝hexo-symbols-count-time(用于字數統計和計算閱讀時間)
npm install hexo-symbols-count-time --save
-
安裝hexo-generator-sitemap(用于生成站點地圖)
npm install hexo-generator-sitemap --save
-
安裝hexo-generator-feed(用于生成RSS訂閱)
npm install hexo-generator-feed --save
-
安裝hexo-generator-searchdb(用于生成搜索數據庫)
npm install hexo-generator-searchdb --save
-
安裝hexo-deployer-git(用于推送到Github)
npm install hexo-deployer-git --save
-
新建categories頁(分類頁面)
hexo new page categories # 會在source目錄下生成categories目錄 # 修改source/categories/index.md文件如下 --- title: categories date: 2019-06-01 23:34:54 type: "categories" comments: false ---
-
新建tags頁(標簽頁面)
hexo new page tags # 會在source目錄下生成tags目錄 # 修改source/categories/index.md文件如下 --- title: 標簽 date: 2019-06-01 14:21:23 type: "tags" comments: false ---
-
新建about頁
hexo new page about # 會在source目錄下生成about目錄 # 修改source/categories/index.md文件如下 --- title: 我 date: 2019-06-01 23:35:51 comments: false --- <h3 style="text-align: center;font-size: 18px;">就算有一天我一無所有,也不缺從頭再來的勇氣!</h3>
-
修改工程的配置修改_config.yml文件)
詳細配置參考https://hexo.io/zh-cn/docs/configuration
# 以下是在原有配置項修改的,大家僅需修改對應的項 # 博客站點標題 title: 落 # 博客站點子標題 subtitle: 你若安好,便是晴天! # 描述 description: 代碼千萬行,注釋第一行。</br>編程不規范,同事兩行淚。 # 作者 author: Vimor # 語言 language: zh-CN # 時區 timezone: Asia/Shanghai # URL,這里的url值大家修改成自己的Github Page地址 url: https://vimior.github.io # 主題配置,這里把默認的landscape注釋掉,使用我們安裝的next主題 # theme: landscape theme: next # 使用hexo-theme-next主題 # 關聯Github Pages,此處的repo值請修改為自己的Github Pages倉庫路徑 # 這里是執行hexo deploy命令時才需要用到的配置,不影響本地調試運行的使用 deploy: type: git repo: git@github.com:vimior/vimior.github.io.git branch: master ############上面的配置是在原有配置基礎上修改的############## ##################完美的分割線:######################### ################下面的配置是新增的######################## # Live2D:看板娘的配置 # 詳細配置參考:https://github.com/EYHN/hexo-helper-live2d # live2d->model->use這項要根據自己之前安裝Live2D支持的時候安裝那個model來配置 live2d: enable: true scriptFrom: local # 默認 pluginRootPath: live2dw/ # 插件在站點上的根目錄(相對路徑) pluginJsPath: lib/ # 腳本文件相對與插件根目錄路徑 pluginModelPath: assets/ # 模型文件相對與插件根目錄路徑 # scriptFrom: jsdelivr # jsdelivr CDN # scriptFrom: unpkg # unpkg CDN # scriptFrom: https://cdn.jsdelivr.net/npm/live2d-widget@3.x/lib/L2Dwidget.min.js # 你的自定義 url tagMode: false # 標簽模式, 是否僅替換 live2d tag標簽而非插入到所有頁面中 debug: false # 調試, 是否在控制臺輸出日志 log: false model: use: live2d-widget-model-haruto # npm-module package name # use: hijiki # 博客根目錄/live2d_models/ 下的目錄名 # use: ./wives/hijiki # 相對于博客根目錄的路徑 # use: https://cdn.jsdelivr.net/npm/live2d-widget-model-hijiki@1.0.5/assets/hijiki.model.json # 你的自定義 url scale: 1 hHeadPos: 0.5 vHeadPos: 0.618 display: superSample: 2 width: 125 height: 125 position: left hOffset: 30 vOffset: 20 mobile: show: true scale: 0.05 react: opacityDefault: 1 opacityOnHover: 0.2 # symbols_count_time配置:用于計算字數,閱讀時間 symbols_count_time: symbols: true time: true total_symbols: true total_time: true exclude_codeblock: false # 版權協議的配置 creative_commons: by-nc-sa # 訂閱的配置 feed: type: atom path: atom.xml limit: 20 hub: content: content_limit: 140 content_limit_delim: ' ' # 搜索的配置 search: path: search.xml field: post format: html limit: 10000
-
修改__hexo-theme-next主題的配置(修改_themes/next/config.yml文件)
詳細配置參考http://theme-next.iissnan.com/theme-settings.html
# RSS配置 rss: /atom.xml # 頁腳配置 footer: since: 2019 icon: name: heart animated: true color: "#D0104C" # 菜單欄配置1:把不需要的菜單的菜單注釋掉就好 # 這里的要顯示的菜單包括(首頁、關于、標簽、分類、歸檔、站點地圖、搜索) menu: home: / || home about: /about/ || user tags: /tags/ || tags categories: /categories/ || th archives: /archives/ || archive # schedule: /schedule/ || calendar sitemap: /sitemap.xml || sitemap # commonweal: /404/ || heartbeat menu_settings: icons: true badges: true # 主題風格:這里選用Gemini # scheme: Muse #scheme: Mist #scheme: Pisces scheme: Gemini # 社交配置:這里把需要顯示的配置好,不需要的注釋就好 # 這里僅僅顯示Github和E-Mail,記得把地址修改為自己的 social: GitHub: https://github.com/vimior || github E-Mail: mailto:vinman.cub@gmail.com || envelope #Weibo: https://weibo.com/yourname || weibo #Google: https://plus.google.com/yourname || google #Twitter: https://twitter.com/yourname || twitter #FB Page: https://www.facebook.com/yourname || facebook #VK Group: https://vk.com/yourname || vk #StackOverflow: https://stackoverflow.com/yourname || stack-overflow #YouTube: https://youtube.com/yourname || youtube #Instagram: https://instagram.com/yourname || instagram #Skype: skype:yourname?call|chat || skype social_icons: enable: true # 是否顯示圖標 icons_only: true # 是否只顯示圖標 transition: false # 用戶頭像配置 avatar: # In theme directory (source/images): /images/avatar.gif # In site directory (source/uploads): /uploads/avatar.gif # You can also use other linking images. url: /images/avatar.png # 注意:需要把avatar.png放在themes/next/source/images里面 # If true, the avatar would be dispalyed in circle. rounded: true # The value of opacity should be choose from 0 to 1 to set the opacity of the avatar. opacity: 1 # If true, the avatar would be rotated with the cursor. rotated: false # 回到頂部的配置 back2top: enable: true sidebar: true scrollpercent: true # 博文的元數據配置,即博文標題下面顯示的內容(如發表時間、所屬分類等等) post_meta: item_text: true created_at: true updated_at: enable: true another_day: false categories: true # 字數統計配置 symbols_count_time: separated_meta: true item_text_post: true item_text_total: false awl: 2 wpm: 300 # brand的背景顏色配置 android_chrome_color: "#fff" # 代碼高亮的主題配置 highlight_theme: night eighties # 卜算子配置:統計閱讀次數等 busuanzi_count: enable: true total_visitors: true total_visitors_icon: user total_views: true total_views_icon: eye post_views: true post_views_icon: eye # 本地搜索配置 local_search: enable: true # If auto, trigger search by changing input. # If manual, trigger search by pressing enter key or search button. trigger: auto # Show top n results per article, show all results by setting to -1 top_n_per_article: 1 # Unescape html strings to the readable one. unescape: false # 更多配置請參閱http://theme-next.iissnan.com/theme-settings.html # 如評論的配置可以通過配置valine(使用leancloud)
-
-
推送到Github Pages
注意:
推送前先本地運行,通過瀏覽器訪問沒問題再行推送
-
初次使用git需要設置user name和email
git config --global user.name "your name" git config --global user.email "your email"
-
(如果未曾添加過ssh-agent)需要添加ssh-key到github的ssh-agent
# 1. 生成ssh密鑰 ssh-keygen -t rsa -C "your email" # 生成路徑在%USERPROFILE%/.ssh目錄 # 2. 把%USERPROFILE%/.ssh/id_rsa的內容拷貝 # 3. 粘貼到github
add_ssh_key_1.pngadd_ssh_key_2.pngadd_ssh_key_3.png
-
生成靜態文件
hexo generate # 可以簡寫為 hexo d
-
推送到github pages
hexo deploy # 可以簡寫為 hexo d # hexo deploy -g # 可以簡寫hexo d -g,等效于hexo g && hexo d
總結
? ? ? 此時,我們已經搭建好一個屬于自己的博客了,如果需要部署到其他倉庫,只需要修改deploy配置即可,詳情請查閱https://hexo.io/zh-cn/docs/deployment
? ? ? Hexo的功能很強大,用?Hexo+Github搭建一個屬于自己的博客,不需要我們寫代碼,只需要通過修改配置即可搭建一個屬于自己的酷炫博客。另外,我們平時可以通過在source/_post目錄下用Markdown來寫自己的筆記、博文,然后重新推送即可在自己的線上博客上看到。
? ? ? 這是我博客里的第一篇博文,由于本人學識有限,難免有錯漏,敬請諒解,歡迎指出!