跳到主要内容

10分钟搭建hexo博客平台

· 阅读需 4 分钟

由于ghost只支持4.2以下的node版本,最终选择了hexo这个非常优秀的博客框架。 下文的环境为: cvm:CentOS 本地:MacOS

特别感谢molunerfinn开发的hexo主题

1. 一些准备工作

本文的主要演示在云服务器上部署个人博客系统,如果仅仅想在本地(macos)体验的可以忽略一些条件。

1.1 部署的准备

  • 云服务器(云服务器大大降低了部署门槛)
  • 域名与备案工作(也可以暂时通过IP访问)
  • 在服务器(本地)安装Git
  • 安装node&npm
  • 使用npm全局安装pm2
  • 安装nginx(为了部署一些二级域名)
  • 图床服务器(推荐七牛云,私人有免费10G空间)

2.安装

2.1安装hexo

通过npm全局安装hexo-cli

npm install hexo-cli -g

小提示: 最好科学上网,或者将npm源切换为淘宝镜像 1.设成淘宝的

npm config set registry http://registry.npm.taobao.org/

2.换成原来的

npm config set registry https://registry.npmjs.org/

2.2生成博客站点

hexo init myblog

cd blog

npm install

2.3安装主题

强烈推荐melody这个主题,中文文档在此,图文并茂,简洁明了,另外hexo中文文档

安装

git clone -b master https://github.com/Molunerfinn/hexo-theme-melody themes/melody

设置

// 在 hexo 的工作目录下找到站点配置文件——_config.yml
theme: melody # 将主题设置成melody
// 这里也可以做其他配置,请参考hexo文档

如果你没有pug以及stylus的渲染器,请下载安装: npm install hexo-renderer-jade hexo-renderer-stylus --save or yarn add hexo-renderer-jade hexo-renderer-stylus

2.4主题的一些配置

// 社交
social:
github: https://github.com/xxx
weibo: https://weibo.com/xxx
// 头像
avatar: url
// 字数统计
wordcount:
enable: true

这里需要注意,如果开启字数统计需要安装hexo-wordcount,否则会遇到一些问题

npm install hexo-wordcount

或者用yarn的同学

yarn add hexo-wordcount
// 友情链接设置(实例就是这个主题的作者哦)
links_title: Links
links:
Molunerfinn: https://molunerfinn.com

为了主题的平滑升级, theme-melody 使用了data files特性。 推荐把主题默认的配置文件_config.yml复制到 hexo 工作目录下的source/_data/melody.yml,如果source/_data的目录不存在那就创建一个。

2.5 标签页配置

在站点目录下

hexo new page tags

前往source/tags/index.md这个文件并修改

---
title: tags
date: 2018-01-05 00:00:00
type: "tags"
---

2.6 分类配置

在站点目录下

hexo new page categories

你会找到source/categories/index.md这个文件并修改

---
title: categories
date: 2018-01-05 00:00:00
type: "categories"
---

顺便一提hexo新建一篇博客的方式是

hexo new yourblogtitle

在source下_post下编辑你的博文md文件

---
title: 10分钟搭建hexo博客平台
date: 2018-04-06 17:37:48
tags: [hexo, 个人博客, node, pm2]
categories:
- hexo
---

2.7启动服务

hexo server

默认端口是4000,也可以自定义端口

hexo server -p 8000

到此安装过程已经结束,并且已经可以通过浏览器访问,默认会有hello-world一篇博客

3. pm2进程守护

通过pm2后台运行hexo,完成服务器部署 在站点目录下(这里是myblog)新建app.js

var spawn = require('child_process').spawn;

free = spawn('hexo', ['server', '-p 4000']);/* 其实就是等于执行hexo server -p 4000*/

free.stdout.on('data', function (data) {

console.log('standard output:\n' + data);

});

free.stderr.on('data', function (data) {

console.log('standard error output:\n' + data);

});

free.on('exit', function (code, signal) {

console.log('child process eixt ,exit:' + code);

});

作者:不系流年系乾坤
链接:https://www.jianshu.com/p/a256ca175c64
來源:简书

运行pm2

pm2 start app.js -x --name myblog
// 建议-x 以fork模式运行

4. 参考资料