目录
  1. 1. 一、前言
  2. 2. 二、环境安装
    1. 2.1. 本地环境安装(MAC)
    2. 2.2. 服务器部分
      1. 2.2.1. 新建项目
      2. 2.2.2. 添加用户
      3. 2.2.3. 创建本地GIT仓库
      4. 2.2.4. 配置 GIT HOOKS
    3. 2.3. 本地部分
      1. 2.3.1. 安装 Hexo
      2. 2.3.2. 初始化 Hexo 博客
      3. 2.3.3. 创建密钥,设置免密git连接
  3. 3. 配置 Hexo
    1. 3.1. 创建 createHexoPost.js 文件
    2. 3.2. 创建 generateAndDeploy.js 文件
    3. 3.3. 创建 scripts\eventListener.js 文件
    4. 3.4. 在 package.json 中修改配置
    5. 3.5. 将博客目录整体GIT到github.com上进行版本控制
在阿里云中搭建Hexo博客

一、前言

  由于不想部署在github上,因为Github的访问速度在国内比较慢,所以有了此文。
  部署之前先了解一下hexo利于git更新的流程,部署在Github上和部署在云服务器上没有特别大的本质区别。
  整个流程就是本地将 *.md 渲染成静态文件,然后Git推送到服务器的repository,服务器再通过 git-hooks 同步网站根目录。再利于nginx托管这些静态资源,就可以实现快速的访问。

二、环境安装

  • 本地环境安装(MAC)

  • brew 安装

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • node.js 安装

    brew install nodejs
    npm install -g cnpm --registry=https://registry.npm.taobao.org
  • git 安装

    系统已经自带,不用单独安装。

  • hexo 安装

    npm install hexo-cli hexo-server -g
  • 安装 vs code

  • 使用git bash生成ssh

    ssh-keygen -t rsa -C "gadflybsd@gmail.com" -f ~/.ssh/gadfly_pub_authorized_rsa

    在本地服务器中配置好了公钥,接下来我们需要把这个公钥交给服务器,相当于本地机器有了一把能访问服务器的“钥匙”,所以接下来需要配置服务器。

  • 服务器部分

    新建项目

    $ mkdir /data/repo/lcy.gadfly.pub.git
    $ git init --bare /data/repo/lcy.gadfly.pub.git
    # Initialized empty Git repository in /data/repo/lcy.gadfly.pub.git
    $ /root/oneinstack/vhost.sh # 创建lcy.gadfly.pub虚拟站点

    添加用户

    $ useradd GadflyLCY
    $ passwd GadflyLCY
    $ usermod -G git GadflyLCY #将用户添加到git用户组
    $ cat /etc/group # 查看用户组及用户是否成功配置
    $ chown -R GadflyLCY:git /data/repo/lcy.gadfly.pub.git
    $ chown -R GadflyLCY:git /data/wwwroot/lcy.gadfly.pub

    创建本地GIT仓库

    $ mkdir /data/repo
    $ cd /data/repo
    $ git init --bare lcy.gadfly.pub.git

    配置 GIT HOOKS

    $ vi /data/repo/lcy.gadfly.pub.git/hooks/post-receive
    #!/bin/sh
    git --work-tree=/data/wwwroot/lcy.gadfly.pub --git-dir=/data/repo/lcy.gadfly.pub.git checkout -f
    $ chmod +x /data/repo/lcy.gadfly.pub.git/hooks/post-receive
  • 本地部分

    安装 Hexo

    npm install -g hexo-cli
    npm install -g shelljs
    npm install -g readline-sync
    npm install -g lunar-calendar

    初始化 Hexo 博客

    hexo init lcy.gadfly.pub
    cd lcy.gadfly.pub
    npm i hexo-permalink-pinyin --save
    git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/Butterfly
    npm install hexo-renderer-pug hexo-renderer-stylus --save
    npm install hexo-wordcount --save
    npm uninstall hexo-generator-index --save
    npm install hexo-generator-index-pin-top --save
    npm install --save hexo-tag-aplayer
    npm install hexo-douban --save
    npm install hexo-tag-youku --save
    npm install --save hexo-filter-plantuml
    npm install --save hexo-tag-qrcode
    npm un hexo-renderer-marked --save
    npm un hexo-renderer-kramed --save
    npm un hexo-math --save
    npm i @upupming/hexo-renderer-markdown-it-plus --save
    npm install hexo-tag-echarts3 --save
    npm install --save hexo-blog-encrypt
    npm install --save hexo-lunar-calendar
    npm install hexo-tag-youku --save
    npm install --save hexo-tag-bilibili
    npm install --save hexo-filter-flowchart
    npm install --save hexo-filter-sequence
    npm install hexo-generator-search --save
    npm install --save hexo-pdf
    hexo new page about
    hexo new page tags
    hexo new page categories
    hexo new page link
    hexo new page music
    hexo new page gallery

    创建密钥,设置免密git连接

    $ ssh-keygen -t rsa -C "GadflyLcy@gmail.com" -f ~/.ssh/GadflyLCY-GitHub
    $ ssh-copy-id -i GadflyLCY-GitHub.pub GadflyLCY@gadfly.pub
    $ ssh -v GadflyLCY@gadfly.pub
    # Hi GadflyLCY! You've successfully authenticated, but GitHub does not provide shell access.
    # 如果不通,进行以下这两步操作。
    $ ssh-agent -s
    $ ssh-add ~/.ssh/GadflyLCY-GitHub

配置 Hexo

创建 createHexoPost.js 文件

选择新建博客的分类、拉取GIT仓库

require('shelljs/global');

var readlineSync = require('readline-sync');
process.stdin.setEncoding('utf8');
process.stdout.write("现在开始新建Hexo博客文章......\n");
process.stdout.write("系统中的博客分类如下:\n");
process.stdout.write("0:无分类\t1:小学数学\t2:小学语文\t3:小学英语\t4:杂谈随笔\t5:工具字典\t6:手工输入分类名称\t其他按键:退出\n");
var input = readlineSync.question('请选择新建文章应该所属的分类(默认:0):');
if([''].indexOf(input) == 0) input = '0';
var category, cmdStr;
switch(input){
case '0':
break;
case '1':
category = '小学数学';
break;
case '2':
category = '小学语文';
break;
case '3':
category = '小学英语';
break;
case '4':
category = '杂谈随笔';
break;
case '5':
category = '工具字典';
break;
case '6':
category = readlineSync.question("请输入您的分类名称:");
break;
default:
bye("再见,Bye!");
}
var title = readlineSync.question("请输入您的文章标题:").toString().trim();
if(['', 'null'].indexOf(title) == -1){
if(category != null || ['', 'null'].indexOf(input) > -1){
console.warn("您选择的分类是【" + category + "】");
cmdStr = 'hexo new -p "' + category + '/' + title + '" -c "' + category + '" "' + title + '"';
}else{
cmdStr = 'hexo new -c "" "'+ title + '"';
}
}else{
bye("再见,Bye!");
}
var isPush = readlineSync.question("是否拉取最新的GIT仓库数据(y/n,默认:y):").toString().trim();
if(['', 'y', 'Y', 'yes', 'Yes', 'YES'].indexOf(isPush) > -1){
exec('git pull origin master', (err,stdout,stderr) => {
if(err) {
console.log('get weather api error:' + stderr);
} else {
newPost(cmdStr);
}
});
}else{
newPost(cmdStr);
}

function newPost(cmdStr){
exec(cmdStr, (err,stdout,stderr) => {
if(err) {
console.log('get weather api error:' + stderr);
} else {
bye("《" + title + "》已经创建成功,请开始尽情的写作吧!");
}
})
}
function bye(message){
process.stdout.write("\n");
console.log(message);
process.exit(0);
}

创建 generateAndDeploy.js 文件

完成Clean、生成静态文件、推送到GIT、发布到互联网

require('shelljs/global');
var readlineSync = require('readline-sync');
process.stdin.setEncoding('utf8');
var isPush = readlineSync.question("是否推送至GIT仓库数据(y/n,默认:y):").toString().trim();
var isDeploy = readlineSync.question("是否仅本地预览不发布到互联网(y/n,默认:n):").toString().trim();
console.log("正在清理【Hexo】缓存文件 ...");
exec('hexo clean', (err,stdout,stderr) => {
if(err) {
console.log('get Hexo Clean API error:' + stderr);
} else {
console.log("正在生成【Hexo】静态文件 ...");
exec('hexo generate', (err,stdout,stderr) => {
if(err) {
console.log('get Hexo Generate API error:' + stderr);
}else{
hexoToGit(isPush, isDeploy)
}
});
}
});

function hexoToGit(isPush, isDeploy){
if(['', 'y', 'Y', 'yes', 'Yes', 'YES'].indexOf(isPush) > -1){
console.log('正在推送至Github.com代码仓库 ...');
exec('git add .');
exec('git commit -m "add new hexo post!"');
exec('git push --all', (err,stdout,stderr) => {
if(err) {
console.log('get GIT API error:' + stderr);
} else {
hexoDeploy(isDeploy);
}
});
}else{
hexoDeploy(isDeploy);
}
}

function hexoDeploy(isDeploy){
if(['', 'n', 'N', 'no', 'No', 'NO'].indexOf(isDeploy) > -1){
console.log('正在发布【Hexo】到互联网 ...');
exec('hexo deploy');
} else {
console.log('正在启动【Hexo】本地预览服务器 ...');
console.log('如果需要退出预览模式,请按 Ctrl + C 键退出!');
exec('hexo server');
}
}

创建 scripts\eventListener.js 文件

监听 Hexonew 事件,创建文件后在 Visual Studio Code 中打开文件所新建的文件

var os = require('os');
var exec = require('child_process').exec
try {
hexo.on('new', function(data){
if(os.platform() == 'win32'){
exec('start "" "C:\\Program Files\\Visual Studio Code\\Visual_Studio_Code.exe" ' + data.path);
}
if(os.platform() == 'darwin'){
exec('open -a "/Applications/Visual Studio Code.app" ' + data.path);
}
});
} catch (e) {
console.log("产生了一个错误<( ̄3 ̄)> !,错误详情为:" + e.toString());
}

package.json 中修改配置

{"scripts": {
"新建博客": "node createHexoPost.js",
"发布博客": "node generateAndDeploy.js",
}
}

将博客目录整体GITgithub.com上进行版本控制

  1. 进入项目所在目录,右键,点击Git Bash Here进入终端

  2. 将目录初始化为一个 Git 项目

    $ git init

    执行完后会在目录中创建一个 .git 隐藏文件夹

  3. 将所有文件放进新的本地git仓库

    $ git add .

    如果本地已经有.gitignore文件,会按照已有规则过滤不需要添加的文件。如果不想要添加所有文件,可以把符号换成具体的文件名

  4. 将添加的文件提交到仓库

    $ git commit -m "Initial commit"
  5. 将本地仓库关联到远程仓库

    $ git remote add origin https://github.com/javacfox/Blog_Back_Up
  6. 运行以下命令查看结果

    $ git pull origin master
  7. 提交代码之前先执行一下同步

    $ git push origin master
  8. 如果报错,可以执行如下命令,慎用

    $ git pull origin master --allow-unrelated-histories

后面加上 --allow-unrelated-histories,把两段不相干的 分支进行强行合并,因为是提交到一个空白仓库,所以可以强制执行,然后在执行git push origin master

文章作者: Gadfly
文章链接: https://blog.gadfly.pub/2019/11/08/gong-ju-zi-liao/zai-a-li-yun-zhong-da-jian-hexo-bo-ke/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 牛虻的世界
打赏
  • 微信
  • 支付寶

评论