快速搭建Hexo博客

Hexo简介

Hexo 是一个快速、简洁且高效的博客框架。Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页。

Hexo搭建

安装前提

需要先安装下列应用程序

安装hexo脚手架

打开cmd,输入下面命令全局安装

npm install hexo-cli -g

hexo -v查看一下版本,验证是否安装成功

创建一个文件夹blog,然后cd到这个文件夹下

输入命令初始化hexo项目

hexo init
或 hexo init blog(博客名)

新建完成后,指定文件夹目录下有:

  • node_modules: 依赖包

  • public:存放生成的静态页面

  • scaffolds:生成文章的一些模板

  • source:用来存放你的文章

  • themes:主题

  • _config.yml: 博客的配置文件

编译

hexo g

测试运行

hexo s

打开hexo的服务,在浏览器输入localhost:4000就可以看到你默认生成的博客了。

使用ctrl+c可以把服务关掉。

GitHub/Gitee创建个人仓库

首先,你先要有一个GitHub/Gitee账户,去注册一个吧。

注册完登录后,新建仓库

创建一个和你用户名相同的仓库,后面加.github.io/gitee.io,只有这样,将来要部署到GitHub page/Gitee page的时候,才会被识别,也就是xxxx.github.io/xxxx.gitee.io,其中xxx就是你注册的用户名。

将hexo部署到GitHub

这一步,我们就可以将hexo和GitHub关联起来,也就是将hexo生成的文章部署到GitHub上,打开站点配置文件 _config.yml,翻到最后,修改为

username为GitHub用户名

deploy:
type: git
repo: https://github.com/username/username.github.io.git
branch: master

安装deploy-git ,也就是部署的命令。

npm install hexo-deployer-git --save

输入命令进行部署

hexo d

过一会儿就可以在http://username.github.io 这个网站看到你的博客了!!


主题模板

配置主题

https://hexo.io/themes/官网中预览喜欢的主题,在GitHub中搜索主题名,并clone到博客目录的themes文件夹内(参考路径:blog/themes/)

以Butterfly主题为例

cd themes
git clone https://github.com/jerryc127/hexo-theme-butterfly

并在_config.yml中配置该主题,ctrl+f搜索关键字theme,把值修改成butterfly(themes中克隆模板的文件夹名)

theme: butterfly

如果没有 pug 以及 stylus 的渲染器,需要安装

npm install hexo-renderer-pug hexo-renderer-stylus --save

重新编译启动

hexo clean
hexo g
hexo s

访问localhost:4000,成功即可


常用命令

hexo clean 清除了之前生成的东西
hexo g 编译代码
hexo d 部署文章
hexo n page xx 新建页面
hexo new xx 新建博客文章