如何利用github+hexo搭建個人博客

背景

爲了搭建個人在GitHub上的博客,看了幾個帖子,方便自己後續使用,記錄下自己搭建的過程,整合成一個帖子。

一、安裝環境 & 流程步驟

1.安裝環境

  1. 搭建node.js環境
  2. 搭建git環境 和 搭建Github個人倉庫
  3. 搭建本地hexo

2.發佈博客

  1. 將hexo和GitHub Pages關聯起來
  2. 編寫md文檔,並且用hexo操作發佈

二、環境搭建和操作

1. 安裝node.js

如果node之前安裝過,沒卸載乾淨,那麼輸入如下,可以清理乾淨:

1
2
3
4
5
6
sudo npm uninstall npm -g
sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receipts/org.nodejs.*
sudo rm -rf /usr/local/include/node /Users/$USER/.npm
sudo rm /usr/local/bin/node
sudo rm /usr/local/share/man/man1/node.1
sudo rm /usr/local/lib/dtrace/node.d

確認是否卸載乾淨:

1
2
node -v  //not found
npm -v //not found

1> 下載安裝包直接安裝node

Node.js下載地址:Node.js下載安裝包,注意安裝Node.js會包含環境變量及npm的安裝。
安裝後,檢測Node.js是否安裝成功,在命令行中輸入 node -v ;
檢測npm是否安裝成功,在命令行中輸入npm -v 。

2> 通過brew安裝node

安裝homebrew

首先輸入指令: /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
如果homebrew安裝報錯,如圖:

解決方法 輸入:brew doctor 修復 brew

安裝node和npm

安裝命令:

1
2
brew install node
brew install npm

 

如果輸入 brew install node 時,出現如圖錯誤:

解決方法:

  1. 輸入:sudo chown -R whoami /usr/local/
  2. 輸入:brew link –overwrite node
  3. 輸入:brew link node

這樣 homebrew 就和 node 鏈接到一起了

2.安裝git

這裏需要通過Git將本地的Hexo生成的網頁代碼push到GitHub上。

1> 下載安裝git

從Git官網下載:Git - Downloading Package 現在的機子基本都是64位的,選擇64位的安裝包,下載後安裝,在命令行裏輸入git測試是否安裝成功,若安裝失敗,參看其他詳細的Git安裝教程。

2> git和Github賬號綁定

安裝成功後,將你的Git與GitHub帳號綁定

1
2
git config --global user.name "你的GitHub用戶名"
git config --global user.email "你的GitHub註冊郵箱"

生成ssh密鑰文件:

1
ssh-keygen -t rsa -C "你的GitHub註冊郵箱"

然後直接三個回車即可,默認不需要設置密碼。
然後找到生成的.ssh的文件夾中的id_rsa.pub密鑰,將內容全部複製,打開GitHub_Settings_keys 頁面,新建new SSH Key。


如上圖,Title爲標題,任意填即可,將剛剛複製的id_rsa.pub內容粘貼進去,最後點擊Add SSH key。
在Git Bash中檢測GitHub公鑰設置是否成功,輸入

1
ssh [email protected]

3> 創建GitHub個人倉庫主頁

先去GitHub官網註冊Git賬戶
註冊之後,新建一個repository(倉庫)爲Github Pages的倉庫
Github Pages倉庫命名規範是: yourname.github.io

記住:名字好好起,如果以後你自己捨不得花錢買域名,那你主頁的域名就是:yourname.github.io

3. 安裝hexo

  1. 先創建一個文件夾Blog,後面hexo框架和本地寫的文章都會在這個文件夾下。

  2. 使用npm命令安裝Hexo

1
sudo npm install -g hexo-cli
  1. 進入blog目錄(Blog裏面一層)後面的指令都必須在這個目錄以內才能找到。
1
cd  blog
  1. 檢查hexo 是否安裝成功
1
hexo
  1. hexo安裝成功,初始化博客
1
hexo init blog
  1. 測試下博客主頁的基本框架OK沒
1
2
3
hexo new test_my_site
hexo g
hexo s
  1. 以上指令分別是:新建一篇博客,生成,啓動本地服務
    所以可以在瀏覽器中本地查看
1
2
3
localhost:4000
//關閉服務器
control + C
  1. 如果 hexo s 報錯,ERROR Server not found: git
1
npm install hexo -server --save

其他的指令同理,比如 hexo deployer 也沒有,那就:

1
npm install hexo -deployer --save

4. 環境配置—hexo和GitHub關聯起來

前面提到的博客所在根目錄blog,其目錄結構:

其中有個配置文件:_config.yml。打開_config.yml文件,找到Deployment,然後按照如下修改,用戶名改成你的,git地址改成你的。
注意冒號:後面要空一格

1
2
3
4
5
6
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: git
  repo: [email protected]:yourname/yourname.github.io.git
  branch: master

themes文件夾,裏面也有個_config.yml文件,這個稱爲主題配置文件。後面想下載別的主題使用,就會用到。

更換主題

如果你不喜歡Hexo默認的主題,可以更換不同的主題,主題下載:Themes 。複製喜歡的主題的git地址,進入blog目錄(就是命令行的位置處於blog目錄)打開命令行輸入:

1
git clone https://github.com/dongyuanxin/theme-ad.git themes/yourtheme

這是將XX主題下載到blog目錄的themes主題下的yourtheme文件夾中。打開站點的_config.yml配置文件,修改主題爲yourtheme

1
2
3
4
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: yourtheme

如上操作,主題就更改好了。不過有的源碼是需要插件的, 源碼的博客都會在git上註明的,照着操作就好。記得學會hexo s 本地啓動服務遊覽:http://localhost:4000/> 。

5. hexo編寫發佈博客

1
2
3
4
5
6
7
8
9
10
11
12
13
//創建新的博客
hexo n  title
//本地的博客會保存在本目錄下的source/_post文件夾中,會生成title.md文件,編輯並保存該文件
// 刪除舊的 public 文件
hexo clean
// 生成新的 public 文件
hexo generate
或者
hexo g
// 開始部署
hexo deploye
或者
hexo d

在瀏覽器中輸入 https:/yourname.github.io (用戶名改成你的),就可以看到你的git博客已經搭建成功了。

6.hexo常用指令整理

npm install hexo -g #安裝Hexo
npm update hexo -g #升級
hexo init #初始化博客

命令簡寫
hexo n “我的博客” == hexo new “我的博客” #新建文章
hexo g == hexo generate #生成
hexo s == hexo server #啓動服務預覽
hexo d == hexo deploy #部署

hexo server #Hexo會監視文件變動並自動更新,無須重啓服務器
hexo server -s #靜態模式
hexo server -p 5000 #更改端口
hexo server -i 192.168.1.1 #自定義 IP
hexo clean #清除緩存,若是網頁正常情況下可以忽略這條命令

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章