介绍使用Hexo搭建博客

[TOC]

环境

mac 10.14
默认已安装好了HomeBrew和Git

搭建

安装Node.js环境

先检测电脑山管是否安装有Node.js环境,能正确显示版本号即表示node安装成功(mac不自带Node.js环境)

1
2
$ npm -v
$ node -v

安装Node.js(Hexo是基于Node.js环境的)

1
$ brew install node

安装npm

1
$ npm install

安装Hexo

安装

1
2
# -g表示全局安装
$ npm install hexo-cli -g

初始化博客

1
2
$ cd myblog             # 创建并进入存储博客的目录(myblog)
$ hexo init # 初始化本地博客

生成并运行服务

执行下述命令生成本地网页文件并开启服务器,然后通过http://localhost:4000查看本地博客。

1
2
$ hexo g                # Generate static files
$ hexo s # Run server

关联GitHub

修改myblog目录下的_config.yml文件

1
2
3
4
deploy: 
type: git
repo: 这里填入你之前在GitHub上创建仓库的完整路径,记得加上 .git
branch: maste

双线部署

1
2
3
4
5
6
deploy:
type: git
repo:
gitee: https://gitee.com/ticsmatic/ticsmatic.git
github: https://github.com/ticsmatic/ticsmatic.github.io.git
branch: master

部署发布

1
2
3
4
$ hexo g 
$ hexo d // Deploy to remote sites
或者使用组合命令:
$ hexo d -g

若执行hexo g出错则执行npm install hexo --save
若执行hexo d出错则执行npm install hexo-deployer-git --save。错误修正后再次执行hexo ghexo d上传到服务器

若未关联GitHub,执行hexo d时会提示输入GitHub账号用户名和密码,即:

1
2
username for 'https://github.com':
password for 'https://github.com':

hexo d执行成功后便可通过git 仓库地址访问博客,看到的内容和http://localhost:4000相同。

更换Hexo主题

以比较精简的为例 maupassant

1
2
3
$ git clone https://github.com/tufu9441/maupassant-hexo.git themes/maupassant
$ npm install hexo-renderer-pug --save
$ npm install hexo-renderer-sass --save

将myblog目录下_config.yml里的theme的名称landscape更改为next。

1
2
3
$ hexo g  // 生成缓存和静态文件
$ hexo d // 重新部署到服务器
$ hexo s // Run server

当本地博客部署到服务器后,网页端无变化时可以采用下述命令。

1
hexo clean  // 清楚缓存文件(db.json)和已生成的静态文件(public)

推荐主题

写个博客

1
$ hexo new "article title"

然后执行生成,发布即可

其它知识

当安装的一些插件不需要时,可以卸载

1
2
3
4
5
6
npm uninstall 模块
 
删除本地模块时你应该思考的问题:是否将在package.json上的相应依赖信息也消除?
npm uninstall 模块:删除模块,但不删除模块留在package.json中的对应信息
npm uninstall 模块 --save 删除模块,同时删除模块留在package.json中dependencies下的对应信息
npm uninstall 模块 --save-dev 删除模块,同时删除模块留在package.json中devDependencies下的对应信息

参考

hexo官方文档

评论