新手来袭,最近朋友推荐Hexo,并用hexo建立了自己的博客,不由对hexo的高效搭建博客能力感到赞叹!

Welcome to Hexo! A fast,simple & powerful blog framework
hexo是一款基于Node.js、快速、简洁且高效的博客框架

主要目录

Node.js

安装node.js,最佳方式是使用 nvm 管理nodejs版本。

Git

安装git (或者其它git客户端 个人推荐SmartGit

Hexo

Node环境搭好后,就可以使用npm 安装Hexo

安装

1
$ npm install -g hexo-cli // 全局安装hexo-cli

初始化

1
2
3
4
5
$ hexo init my-blog // 创建一个hexo框架
$ cd my-blog // 进入目录
$ npm install // 安装依赖包
$ hexo generate // 生成静态文件(结果文件)
$ hexo server // 启动本地服务,可以通过http://localhost:4000/ 访问

新建的框架目录如下:

1
2
3
4
5
6
7
|—— _config.yml // 网站的配置信息
|—— package.json // 项目包信息
|—— scaffolds // 模板文件夹,Hexo根据scaffold中的模板来新建文件
|—— source // 存放用户资源的地方
| |—— _drafts // 存放草稿
| |—— _posts // 存放文章
|—— themes // 主题文件夹

常用的主要命令

1
2
3
4
5
$ hexo generate // hexo g,生成静态文件,会在当前目录下生成一个public文件夹
$ hexo server // hexo s,启动本地服务,用于博客的预览
$ hexo deploy // hexo d,部署到远程(如GitHub,可以在_config.yml中配置)
$ hexo new post-name // hexo n post-name, 新建文章
$ hexo new page page-name // hexo n page page-name,新建页面

常用组合命令

1
2
$ hexo d -g // 生成部署
$ hexo s -g // 生成预览

草稿命令:

1
2
3
$ hexo new draft <title> // 新建草稿,存放在source/_drafts
$ hexo publish post <title> // 发布草稿为文章,文章转移到source/_posts
$ hexo s -g --drafts // 显示草稿

设置hexo主题

1
$ git clone https://github.com/WongMinHo/hexo-theme-miho.git themes/miho

然后修改Hexo目录下的_config.yml配置文件中的theme属性,将其设置为miho。

部署到GitHub

首先注册一个GitHub帐号,没有的话去注册
建立与你用户名对应的仓库,且仓库名为your_user_name.github.io,这是固定的命名约定。

创建后,可以通过https://irisjiao.github.io/来访问个人主页

1
$ npm install hexo-deployer-git --save

修改_config.yml中的配置:

1
2
3
4
deploy:
type: git
repo: git@github.com:Irisjiao/Irisjiao.github.io.git
branch: master

最后执行命令:

1
$ hexo d

hexo d 报错

好久没有更新博客了,报错找了好多文档,终于找到答案了,亲测有效

error: failed to execute prompt script (exit code 1)
fatal: could not read Username for ‘https://github.com‘: No error

修改配置文件:
根目录下的 _config.yml
deploy节点

原来的配置为:

1
2
3
4
deploy:
type: git
repo: https://github.com/{yourname}/{yourname}.github.io.git
branch: master

修改为如下:

1
2
3
4
deploy:
type: git
repo: https://{yourname}:{yourpassword}@github.com/{yourname}/{yourname}.github.io.git
branch: master

参考文章
More info: Deployment
Y Augerlmir 博客

最后更新: 2018年10月31日 11:30

原始链接: http://irisjiao.github.io/2017/09/12/hello-world/