一.準(zhǔn)備工作
1、安裝 Node.js: https://nodejs.org/en/
2捏鱼、安裝 Git: https://github.com/waylau/git-for-win
Git教程 https://github.com/waylau/git-for-win
廖雪峰老師的教程蕾额,非常好崭庸。
3梳玫、安裝完成后谍珊,在開始菜單里找到“Git”->“Git Bash”治宣,名稱和郵箱是Github上的。
4砌滞、安裝 Hexo侮邀。所有必備的應(yīng)用程序安裝完成后,即可使用 npm 安裝 Hexo贝润。
$ npm install -g hexo-cli
4.1 安裝hexo出錯:
unable to verify the first certificate(無法驗證第一證書)
查找到解決方案:
參考網(wǎng)址:npm報錯
依據(jù)解決方案所述绊茧,在命令行輸入如下命令,來取消ssl驗證:
npm config set strict-ssl false
之后安裝就會成功了题暖,顯示如下:
二按傅、配置Github
首先注冊捉超、登錄 https://github.com/
記住自己的Username(很重要)
然后右上角選擇 Create a new repository https://github.com/new
Repository name(填自己的名字) yourname.github.io(yourname與你的注冊用戶名一致,這個就是你博客的域名了)
倉庫創(chuàng)建完成后,開始生成添加秘鑰唯绍。
在終端(cmd)輸入:
ssh-keygen -t rsa -C "Github的注冊郵箱地址"
一路Enter過來就好拼岳,待秘鑰生成完畢,會得到兩個文件id_rsa
和id_rsa.pub
况芒,用帶格式的記事本打開id_rsa.pub
惜纸,Ctrl + a
復(fù)制里面的所有內(nèi)容,然后進入https://github.com/settings/ssh:
將復(fù)制的內(nèi)容粘貼到Key的輸入框绝骚,隨便寫好Title里面的內(nèi)容耐版,點擊Add SSH key按鈕即可。
若沒有配置GitHub压汪,就執(zhí)行第三步(初始化博客)粪牲,會出現(xiàn)如下錯誤:
三、初始化博客
在電腦F盤(自己隨意)目錄下新建文件夾 test止剖,進入test腺阳,按住Shift鍵點擊鼠標(biāo)右鍵,點擊“在此處打開命令窗口”:
輸入
hexo init blog
稍微等待下穿香,速度有點慢亭引。成功提示:
INFO Start blogging with Hexo!
因為你初始化hexo 之后source目錄下自帶一篇hello world
文章, 所以直接執(zhí)行下方命令:
$ hexo generate
# 啟動本地服務(wù)器
$ hexo server
# 在瀏覽器輸入 http://localhost:4000/就可以看見網(wǎng)頁和模板了
INFO Start processing
INFO Hexo is running at http://localhost:4000/. Press Ctrl+C to stop.
訪問http://localhost:4000/,便可以看到網(wǎng)站初步的模樣皮获,不要激動焙蚓,我們還要把網(wǎng)頁發(fā)布到Github上去。
四洒宝、配置博客
在blog
目錄下购公,用sublime/notepad++
等文本編輯器打開_config.yml
文件,修改參數(shù)信息
特別提醒雁歌,在每個參數(shù)的:后都要加一個空格
1.修改網(wǎng)站相關(guān)信息
title: wangwlj測試所用博客
subtitle: 積跬步以至千里君丁!
description: 網(wǎng)頁描述
author: wangwlj
language: zh-Hans
timezone: Asia/Shanghai
language
和timezone
都是有輸入規(guī)范的,詳細(xì)可參考 語言規(guī)范和時區(qū)規(guī)范将宪。
2.配置部署(qwerty200696,修改成自己的)
deploy:
type: git
repo: https://github.com/qwerty200696/qwerty200696.github.io
branch: master
其中repo
項是之前Github
上創(chuàng)建好的倉庫的地址橡庞,可以通過如下圖所示的方式得到:
branch是項目的分支较坛,我們默認(rèn)用主分支master
。
3.配置統(tǒng)一資源定位符(個人域名)
如果有個人域名的話可以設(shè)置扒最,否則跳過即可丑勤。GitHub地址也可以。
url: http://inerdstack.com
對于root(根目錄)吧趣、permalink(永久鏈接)法竞、permalink_defaults(默認(rèn)永久鏈接)等其他信息保持默認(rèn)耙厚。
五、發(fā)表文章
1.新建一篇博文岔霸,在CMD中輸入
$ hexo new "我的測試文章"
INFO Created: F:\test\blog\source\_posts\我的測試文章.md
2.找到該文章薛躬,打開,使用Markdown語法呆细,該語法介紹可以查看:markdown語法
3.接著輸入如下的一系列命令:
F:\test\blog
$ hexo clean
INFO Deleted database.
INFO Deleted public folder.
F:\test\blog
$ hexo generate
INFO Start processing
INFO Files loaded in 1.48 s
#省略
INFO 29 files generated in 4.27 s
$ hexo server
INFO Start processing
INFO Hexo is running at http://localhost:4000/. Press Ctrl+C to stop.
我直接跑的server型宝,也可以。
最后一步絮爷,發(fā)布到網(wǎng)上趴酣,執(zhí)行:
F:\test\blog
$ hexo deploy
INFO Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
#省略
其中會跳出Github登錄,直接登錄坑夯,如果沒有問題岖寞,在瀏覽器輸入qwerty200696(換成你的).github.io/
然后就可以看到已經(jīng)發(fā)布了。
4.錯誤記錄一:
deploy not found: git
解決方案:執(zhí)行如下語句后柜蜈, 再部署即可:
npm install hexo-deployer-git --save
參考自:https://www.v2ex.com/t/175940
5.錯誤記錄二:
今天運行:
hexo deploy
同步文章到github
時仗谆,出錯:
fatal: unable to access 'https://username:password@github.com/username/username.github.io.git/': SSL certificate problem: unable to get local issuer certificate
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
Error: fatal: unable to access 'https://username:password@github.com/username/username.github.io.git/': SSL certificate problem: unable to get local issuer certificate
at ChildProcess.<anonymous> (D:\test\blog2\node_modules\hexo-util\lib\spawn.js:37:17)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at ChildProcess.cp.emit (D:\test\blog2\node_modules\cross-spawn\lib\enoent.js:40:29)
at maybeClose (internal/child_process.js:920:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:230:5)
解決方法:執(zhí)行如下語句:
$ git config --global http.sslVerify false
參考自:git獲取代碼提示SSL certificate problem: unable to get local issuer certificate
(optional)另外,可將_config.yml
中的repo
修改為如下標(biāo)準(zhǔn)格式:
repo: https://用戶名:密碼@github.com/用戶名/用戶名.github.io.git
參考自: hexo提交報錯 unable to access
之后再次運行發(fā)布指令跨释,終于成功:
hexo deploy
六胸私、添加圖片
經(jīng)過上面的配置后,發(fā)現(xiàn)上傳的博客文章里面的本地圖片居然顯示不來(沒有同步上傳)鳖谈。
于是岁疼,找到解決方案:
1 把主頁配置文件_config.yml
里的post_asset_folder:
這個選項設(shè)置為true
2 在你的hexo目錄下執(zhí)行這樣一句話npm install hexo-asset-image --save
,這是下載安裝一個可以上傳本地圖片的插件
3 等待一小段時間后缆娃,再運行hexo n "xxxx"
來生成md
博文時捷绒,/source/_posts
文件夾內(nèi)除了xxxx.md
文件還有一個同名的文件夾
4 最后在xxxx.md
中想引入圖片時,先把圖片復(fù)制到xxxx這個文件夾中贯要,然后只需要在xxxx.md
中按照markdown的格式引入圖片:

注意: xxxx
是這個md文件的名字暖侨,也是同名文件夾的名字。只需要有文件夾名字即可崇渗,不需要有什么絕對路徑字逗。你想引入的圖片就只需要放入xxxx
這個文件夾內(nèi)就好了,很像引用相對路徑宅广。
5 最后檢查一下葫掉,hexo g
生成頁面后,進入public\2017\02\26\index.html
文件中查看相關(guān)字段跟狱,可以發(fā)現(xiàn)俭厚,html標(biāo)簽內(nèi)的語句是
,而不是<img src="xxxx/圖片名.jpg>
驶臊。(這點可以跳過挪挤,按照1-4步執(zhí)行下來一般沒有問題叼丑。)
6.hexo s
,運行本地服務(wù)器扛门,打開http://localhost:4000/
鸠信,可實時查看修改情況。
7.hexo g
尖飞,同步到github症副。
七、總結(jié)
發(fā)布文章的步驟:
1政基、hexo new
創(chuàng)建文章
2贞铣、Markdown
語法編輯文章
3、部署(所有打開CMD都是在blog目錄下)
hexo clean #清除緩存 網(wǎng)頁正常情況下可以忽略此條命令
hexo generate #生成
hexo server #啟動服務(wù)預(yù)覽沮明,非必要辕坝,可本地瀏覽網(wǎng)頁
hexo deploy #部署發(fā)布
簡寫Tips:
hexo n “我的博客” == hexo new “我的博客” #新建文章
hexo p == hexo publish
hexo g == hexo generate#生成
hexo s == hexo server #啟動服務(wù)預(yù)覽
hexo d == hexo deploy#部署
參考文獻:
本文在GitHub博客上即時更新 https://qwerty200696.github.io/2017/09/08/blog_setup/