天天看點

[Node.js] 3、搭建hexo部落格

目錄

  • 一、安裝新版本的nodejs和npm
  • 二、安裝hexo
  • 三、安裝hexo-admin并配置
  • 四、nginx配置
  • 五、增加tag
  • 六、背景啟動
  • 七、體驗

安裝n子產品:

npm install -g n
           

更新node.js到最新穩定版

n stable
           

note: 參考github,不要去其官網: https://github.com/hexojs/hexo

安裝Hexo

npm install hexo-cli -g
           

Setup your blog

hexo init blemesh
cd blemesh
           

安裝Cactus主題,衆多開源主題中比較簡潔的一個:

主題頁: https://hexo.io/themes/

Cactus頁: https://github.com/probberechts/hexo-theme-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
           

安裝:

npm install --save hexo-admin
           

打開目錄下的_config.yml配置hexo-admin:

admin:
    username: XXXX(自己設定使用者名)
    password_hash: XXXXXXXXX(密碼,但是是明文經過bcrypt hash加密後生成的)
    secret: hey hexo(用于cookie安全)
    deployCommand: './admin_script/hexo-generate.sh'(調用該腳本)
           

注:

1)其中password_hash是你自己的明文密碼經過加密後的字元串,但是如果用類似下面的網址: https://bcrypt-generator.com/ 會生成

:$2y$10$pJjIxxxxxfMn9U/xxxxxNuuA20kh1eoB7vZxxxxx/7WpeV7IOxxxx

類似的加密串,但是運作會報

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 需要自己建立:

➜  blemesh cat admin_script/hexo-generate.sh 
hexo g
➜  blemesh chmod +x admin_script/hexo-generate.sh 
           

這個腳本有什麼用,啥時候觸發?可以參考: https://www.jianshu.com/p/68e727dda16d step 5,admin背景管理部落格有個deploy按鈕,點選這個按鈕就會執行這個腳本,該腳本會将md檔案生成靜态網頁,如果用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

執行nginx後會報錯誤:nginx 403 Forbidden,原因是配置檔案nginx.conf檔案的執行使用者和目前使用者不一緻導緻的,把之前的nobody改成目前使用者root。再有錯誤可以參考[7] [8]。

hexo首頁下的tag标簽、category标簽無顯示找不到:

  • 解決辦法: 在主目錄下執行 hexo new page "tags"或者hexo new page "category"
  • 在/source/tags/index.md中設定修改
➜  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程序一直在背景運作的辦法(執行hexo server -d &在一段時間後會停止hexo,此時無法打開背景),采用pm2接管hexo程序:

npm install -g pm2
           

在部落格的根目錄下建立一個hexo_run.js的檔案,檔案内容如下:

➜  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}');
})
           

運作開啟指令: pm2 start hexo_run.js

最後附上 zhouwaiqiang 寫的一個hexo重新開機腳本restart_hexo.sh(需要先配置好nginx),需要重新開機重新整理的時候執行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 #清除資料
hexo generate #生成靜态檔案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
           

  • 啟動:sh ./restart_hexo.sh
  • 通路首頁: http://www.beautifulzzzz.com:8001/
  • 通路nginx靜态快速版網頁: http://www.beautifulzzzz.com:3001/
  • 通路背景編寫文章: http://www.beautifulzzzz.com:8001/admin/
  • 編寫好之後點選Deploy會自動調用之前的腳本,靜态網頁就有了
: 完~
大家覺得不錯,可以點推薦給更多人~

LINKS

[1]. linux下線上更新nodejs

[2]. hexo github

[3]. Cactus theme

[4]. hexo documentation

[5]. Error: EACCES: permission denied, mkdir '......node-sass/build'錯誤解決方案

[6]. bcrypt加密算法

[7]. nginx 403 Forbidden錯誤的原因和解決方法

[8]. Failed to start A high performance web server and a reverse proxy server 錯誤提示

[9]. 重要-hexo搭建個人部落格完整版

[10]. 如何優雅地釋出Hexo部落格

@beautifulzzzz
以藍牙技術為基礎的的末梢無線網絡系統架構及創新型應用探索!
領域:智能硬體、物聯網、自動化、前沿軟硬體
部落格:https://www.cnblogs.com/zjutlitao/
園友交流群|微信交流群:414948975|園友交流群