一、前言
由于不想部署在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'); |
创建 generateAndDeploy.js
文件
完成Clean、生成静态文件、推送到GIT、发布到互联网
require('shelljs/global'); |
创建 scripts\eventListener.js
文件
监听
Hexo
的new
事件,创建文件后在Visual Studio Code
中打开文件所新建的文件
var os = require('os'); |
在 package.json
中修改配置
{"scripts": { |
将博客目录整体GIT
到github.com
上进行版本控制
-
进入项目所在目录,右键,点击Git Bash Here进入终端
-
将目录初始化为一个 Git 项目
git init
执行完后会在目录中创建一个 .git 隐藏文件夹
-
将所有文件放进新的本地git仓库
git add .
如果本地已经有.gitignore文件,会按照已有规则过滤不需要添加的文件。如果不想要添加所有文件,可以把符号换成具体的文件名
-
将添加的文件提交到仓库
git commit -m "Initial commit"
-
将本地仓库关联到远程仓库
git remote add origin https://github.com/javacfox/Blog_Back_Up
-
运行以下命令查看结果
git pull origin master
-
提交代码之前先执行一下同步
git push origin master
-
如果报错,可以执行如下命令,慎用
git pull origin master --allow-unrelated-histories
后面加上 --allow-unrelated-histories,把两段不相干的 分支进行强行合并,因为是提交到一个空白仓库,所以可以强制执行,然后在执行git push origin master