基于Node.js搭建hexo博客過程詳解
一、安裝新版本的nodejs和npm
安裝n模塊:
npm install -g n
升級node.js到最新穩(wěn)定版
n stable
二、安裝hexo
note: 參考github,不要去其官網(wǎng)
安裝Hexo
npm install hexo-cli -g
Setup your blog
hexo init blemesh cd blemesh
安裝Cactus主題,眾多開源主題中比較簡潔的一個:
git clone https://github.com/probberechts/hexo-theme-cactus.git themes/cactus
修改主題配置:
vim _config.yml
# Extensions ## Plugins: https://hexo.io/plugins/ ## Themes: https://hexo.io/themes/ ## theme: landscape theme: cactus theme_config: colorscheme: white
Create pages and articles with the hexo new [layout] <title> command. For example, to create an "about me" page, run:
hexo new page about
This will create a new file in source/about/index.md Similary, you can create a new article with
hexo new post "hello world"
and add some interesting content in source/_posts/hello-world.md.
Start the server:
hexo server
8001 port:
hexo server -p 8001
三、安裝hexo-admin并配置
安裝:
npm install --save hexo-admin
打開目錄下的_config.yml配置hexo-admin:
admin:
username: XXXX(自己設(shè)置用戶名) password_hash: XXXXXXXXX(密碼,但是是明文經(jīng)過bcrypt hash加密后生成的) secret: hey hexo(用于cookie安全) deployCommand: './admin_script/hexo-generate.sh'(調(diào)用該腳本)
注:
1)其中password_hash是你自己的明文密碼經(jīng)過加密后的字符串,但是如果用類似下面的網(wǎng)址: https://bcrypt-generator.com/ 會生成:$2y$10$pJjIxxxxxfMn9U/xxxxxNuuA20kh1eoB7vZxxxxx/7WpeV7IOxxxx類似的加密串,但是運(yùn)行會報(bào)invalid salt revision錯誤,其原因是:
➜ blemesh cat node_modules/hexo-admin/www/bundle.js | head -4851 | tail -10 if (salt.charAt(0) != '$' || salt.charAt(1) != '2') throw "Invalid salt version"; if (salt.charAt(2) == '$') off = 3; else { minor = salt.charAt(2); if (minor != 'a' || salt.charAt(3) != '$') throw "Invalid salt revision"; off = 4; }
需要版本號是2a的加密方式,因此只能用python自己寫了:
https://pypi.org/project/bcrypt/3.1.0/
>>> hashed = bcrypt.hashpw(password, bcrypt.gensalt(prefix=b"2a")) >>> print(hashed) b'$2a$12$PAoJr3USOBxxxxxxxxxxxxxxV/.h.QNbh/6q.xxxxxxxxxxxxxxxxcDcJ.'
2)其中配置中有個腳本: ./admin_script/hexo-generate.sh 需要自己創(chuàng)建:
➜ blemesh cat admin_script/hexo-generate.sh hexo g ➜ blemesh chmod +x admin_script/hexo-generate.sh
這個腳本有什么用,啥時(shí)候觸發(fā)?可以參考: https://www.jianshu.com/p/68e727dda16d step 5,admin后臺管理博客有個deploy按鈕,點(diǎn)擊這個按鈕就會執(zhí)行這個腳本,該腳本會將md文件生成靜態(tài)網(wǎng)頁,如果用nginx配置去訪問靜態(tài)網(wǎng)頁,速度會快很多。
四、nginx配置
配置nginx:編輯 /etc/nginx/nginx.conf 插入下面代碼:
server { listen 3001; server_name www.beautifulzzzz.com; index index.html index.htm index; root /root/App/blemesh/public; }
之后重啟nginx:nginx -s reload
注:
執(zhí)行nginx后會報(bào)錯誤:nginx 403 Forbidden,原因是配置文件nginx.conf文件的執(zhí)行用戶和當(dāng)前用戶不一致導(dǎo)致的,把之前的nobody改成當(dāng)前用戶root。
五、增加tag
hexo主頁下的tag標(biāo)簽、category標(biāo)簽無顯示找不到:
解決辦法: 在主目錄下執(zhí)行 hexo new page "tags"或者h(yuǎn)exo new page "category"
在/source/tags/index.md中設(shè)置修改
➜ blemesh cat ./source/tags/index.md --- type: "tags" comments: false date: 2019-02-24 02:53:03 ---
同理categories:
➜ blemesh cat ./source/category/index.md --- type: "category" comments: false date: 2019-02-24 02:53:34 ---
或者about me:
➜ blemesh cat ./source/about/index.md --- title: about type: "about-me" comments: false date: 2019-02-22 00:09:58 ---
六、后臺啟動
hexo server進(jìn)程一直在后臺運(yùn)行的辦法(執(zhí)行hexo server -d &在一段時(shí)間后會停止hexo,此時(shí)無法打開后臺),采用pm2接管hexo進(jìn)程:
npm install -g pm2
在博客的根目錄下創(chuàng)建一個hexo_run.js的文件,文件內(nèi)容如下:
➜ blemesh cat hexo_run.js const { exec } = require('child_process') exec('hexo server -p 8001 -d',(error, stdout, stderr) => { if(error){ console.log('exec error: ${error}') return } console.log('stdout: ${stdout}'); console.log('stderr: ${stderr}'); })
運(yùn)行開啟命令: pm2 start hexo_run.js
最后附上 zhouwaiqiang 寫的一個hexo重啟腳本restart_hexo.sh(需要先配置好nginx),需要重啟刷新的時(shí)候執(zhí)行source restart_hexo.sh即可:
➜ blemesh cat restart_hexo.sh #!/bin/bash PROCESS=`ps -ef|grep hexo|grep -v grep|grep -v PPID|awk '{ print $2 }'` PROC_NAME="pm2" for i in $PROCESS do echo "Kill the $1 process [ $i ]" kill -9 $i done hexo clean #清除數(shù)據(jù) hexo generate #生成靜態(tài)文件public文件夾 ProcNumber=`ps -ef |grep -w $PROC_NAME|grep -v grep|wc -l` if [ $ProcNumber -le 0 ];then pm2 start hexo_run.js else pm2 restart hexo_run.js fi service nginx restart
七、體驗(yàn)
- 啟動:sh ./restart_hexo.sh
- 訪問主頁: http://www.beautifulzzzz.com:8001/
- 訪問nginx靜態(tài)快速版網(wǎng)頁: http://www.beautifulzzzz.com:3001/
- 訪問后臺編寫文章: http://www.beautifulzzzz.com:8001/admin/
- 編寫好之后點(diǎn)擊Deploy會自動調(diào)用之前的腳本,靜態(tài)網(wǎng)頁就有了
完
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mongoose實(shí)現(xiàn)虛擬字段查詢的方法詳解
這篇文章主要給大家介紹了關(guān)于Mongoose實(shí)現(xiàn)虛擬字段查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08解決node-webkit 不支持html5播放mp4視頻的方法
本文給大家分享的是解決node-webkit 不支持html5播放mp4視頻的方法,其原因大概是因?yàn)閚ode-webkit沒有購買mp4格式的專利授權(quán),恩,我們來想個辦法來解決這個事情吧。2015-03-03深入理解Node.js 事件循環(huán)和回調(diào)函數(shù)
這篇文章主要介紹了深入理解Node.js 事件循環(huán)和回調(diào)函數(shù),詳細(xì)的介紹Node.js 事件循環(huán)和Node.js回調(diào)函數(shù),需要學(xué)習(xí)的可以參考一下。2016-11-11socket.io學(xué)習(xí)教程之深入學(xué)習(xí)篇(三)
這篇文章更加深入的給大家介紹了socket.io的相關(guān)資料,之前已經(jīng)介紹了socket.io的基本教程和應(yīng)用,本文更為深入的來介紹下socket.io的使用,需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04Node.js查詢MySQL并返回結(jié)果集給客戶端的全過程
nodejs最大的優(yōu)勢也是大家用著最為難以理解的一點(diǎn),就是它的異步功能,它幾乎所有的io操作都是異步的,這也就導(dǎo)致很多人不理解也用不習(xí)慣,下面這篇文章主要給大家介紹了關(guān)于Node.js查詢MySQL并返回結(jié)果集給客戶端的相關(guān)資料,需要的朋友可以參考下2022-12-12