# Linux下搭建Hexo博客

1.安装Node.js-4.4.4

这里4.4.4是目前的稳定版,你可以去官网下载最新的稳定版下载

下载

1
wget https://nodejs.org/dist/v4.4.4/node-v4.4.4-linux-x64.tar.xz

解压

1
xz -d node-v4.4.4-linux-x64.tar.xz tar -xvf node-v4.4.4-linux-x64.tar

安装

1
2
3
4
//新建文件夹并移动
mv node-v4.4.4-linux-x64 node-v4.4.4
//将node.js解压的文件移动到系统目录
mv node-v4.4.4 /usr/local/node

配置环境变量

1
2
3
4
5
6
7
8
9
# 编辑 /etc/profile (使用vim)
vim /etc/profile
# 在底部添加 PATH 变量
export PATH=$PATH:/usr/local/node/bin
# 保存退出,先按exit键,再按shift+:
wq
# 最后保存,使其生效即可
source /etc/profile
验证,命令行输入npm -version,安装成功会输出版本号

2.安装Hexo

安装这个可以不使用root用户,但是全局安装时需要管理员权限,现在目录依旧是/home/Zing下,用户是Zing

1
2
3
4
5
6
7
8
9
10
11
安装Git(已安装可跳过)
apt-get install git
安装hexo
# 创建目录
mkdir hexo
# 切换目录
cd hexo
# 全局安装 Hexo,需要最高权限,记得输入root密码
sudo npm install -g hexo-cli
# 初始化 Hexo
hexo init

安装插件

1
2
3
4
5
6
7
8
9
10
11
12
13
npm install hexo-generator-index --save
npm install hexo-generator-archive --save
npm install hexo-generator-category --save
npm install hexo-generator-tag --save
npm install hexo-server --save
npm install hexo-deployer-git --save
npm install hexo-deployer-heroku --save
npm install hexo-deployer-rsync --save
npm install hexo-deployer-openshift --save
npm install hexo-renderer-marked --save
npm install hexo-renderer-stylus --save
npm install hexo-generator-feed --save
npm install hexo-generator-sitemap --save

测试安装成功

1
hexo server

成成功会有下图提示:

1
2
3
(node:6520) [DEP0061] DeprecationWarning: fs.SyncWriteStream is deprecated.
INFO Start processing
INFO Hexo is running at http://localhost:4000/. Press Ctrl+C to stop.

成功!

没有提示,请重新安装
浏览器输入 http://0.0.0.0:4000 可以访问到首页

修改Hexo配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/
# Site 站点信息设置
title: ZXWalk
subtitle: 不忘初心 方得始终
description: 我是一只小小小小鸟......
author: 祥子
language: zh-CN
timezone:
# URL 链接设置
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: https://markdownimages.github.io
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:
# Directory 文件目录
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:
# Writing 文章
new_post_name: :title.md # File name of new posts
default_layout: post
titlecase: false # Transform title into titlecase
external_link: true # Open external links in new tab
filename_case: 0
render_drafts: false
post_asset_folder: false
relative_link: false
future: true
highlight:
enable: true
line_number: true
auto_detect: false
tab_replace:
# Category & Tag
default_category: uncategorized
category_map:
tag_map:
# Date / Time format 日期
## Hexo uses Moment.js to parse and display date
## You can customize the date format as defined in
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-MM-DD
time_format: HH:mm:ss
# Pagination 分页
## Set per_page to 0 to disable pagination
per_page: 8
pagination_dir: page
# Extensions 扩展
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: indigo
# Deployment 这里设置了Git获
#这里一定要注意不要写错了,否则部署到Github上会出问题
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo:
github: git@github.com:MarkdownImages/MarkdownImages.github.io.git
coding: https://git.coding.net/ZXWalk/ZXWalk.git
branch: master
# repo: https://git.oschina.net/XZwalk/zxwalk.git
message: '站点更新:{{now("YYYY-MM-DD HH/mm/ss")}}'

3.部署到github

1
2
3
4
5
初始化Git设置(设置用户名和邮箱)
生成SSH秘钥将,SSH秘钥配置到github上
在github上创建 用户名.github.io的仓库。并且编辑Hexo的_config.yml文件,配置好git提交仓库地址
编译Hexo的MarkDown 文章,部署到github上
用户名一定是你github上的用户名!切记!

编译

1
2
//可以简化为hexo g
hexo generate

发布

1
2
//可以简化为hexo d
hexo deploy

4.选择主题

安装主题NexT

1
git clone https://github.com/iissnan/hexo-theme-next themes/next

并在目录hexo下的_config.yml中

1
2
# 找到 theme: 修改后面的参数,默认是 landscape
theme: next

找主题

https://hexo.io/themes/

遇到问题

1. 安装插件的时候提示更新npm

如果node不是最新的,node有一个模块叫n,是专门用来管理node.js的版本的。使用npm(NPM是随同nodejs一起安装的包管理工具)安装n模块

1
sudo npm install -g n

然后,升级node.js到最新稳定版

1
sudo n stable

执行上面的命令的时候报错

1
2
sudo: npm:找不到命令
执行命令sudo apt-get install npm 即可

2.hexo deploy时重复输入用户名密码的问题

最主要的原因可能是你木有采用git@github.com而是用https//github.com
修改后重新提交部署即可。

常用命令

1
2
3
4
5
6
7
8
9
10
11
//新建一篇文章
hexo new "My New Post"
//测试
hexo server
//编译
hexo g
//发布
hexo d

参考

在Ubuntu下安装Node.JS的不同方式
Ubuntu下搭建Hexo + github 博客
CentOS下搭建Hexo + github 博客