前言:

以前我一直用csdn博客,没有搭建自己的网站,感觉如果自己的服务器重装系统后,东西都没有了,而且csdn用着感觉还行,就不想麻烦了。前段时间我的写了一篇博客,怎么也提交不上去,而且经常出现无法提交现象,所以就觉得是时候搭建自己的博客网站了。好了不废话了,开始吧。。。

1、安装安装Node.js

a.下载

找到最新的下载地址,用wget命令下载到本地

wget https://nodejs.org/dist/v9.3.0/node-v9.3.0-linux-x64.tar.xz

注意:安装最新版本,网址 https://nodejs.org/dist

b.解压命令
xz -d node-v9.3.0-linux-x64.tar.xz  //解压为tar文件
  tar -xvf node-v9.3.0-linux-x64.tar

或者

tar -xvJf node-v9.3.0-linux-x64.tar.xz

文件就被解压到当前文件夹下了,然后移动

mv  node-v9.3.0-linux-x64 node-v9.3.0
 mv  node-v9.3.0 /usr/local/node
c.配置环境变量
# 编辑 /etc/profile (使用vim)
vim /etc/profile
# 在底部添加 PATH 变量
export PATH=$PATH:/usr/local/node/bin
# 保存退出,先按exit键,再按shift+:
wq
# 最后保存并使其生效即可
source /etc/profile

2、安装Hexo

a.安装hexo

我直接安装在root根目录了,你可以自己选择安装目录

# 创建目录
mkdir hexo
# 切换目录
cd hexo
# 安装Git(已安装可跳过)
yum install git-core
# 安装 Hexo
npm install -g hexo-cli
# 初始化 Hexo
hexo init
b.安装插件

我现在看别人博客才发现我没有安装这些插件,我用yilia主题没有安装这些插件也可以正常使用。建议安装吧。

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
c.修改Hexo配置文件

注意:key对应没有值的时候,冒号后面一定要有空格!否则会报错
例如: timezone:会报错,timezone: 则不会。

这是我的配置文件,在hexo/_config.yml

# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/

# Site 站点信息设置
title: 枫之木落 #站名
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: http://www.lookk.cn
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: true
  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: 5
pagination_dir: page

# Extensions 扩展
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: yilia

# Deployment 这里设置了Git获
#这里一定要注意不要写错了,否则部署到Github上会出问题
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: git
  repo: git@github.com:jiaoxiangyu/jiaoxiangyu.github.io.git    
  branch: master
  message: '站点更新:{{now("YYYY-MM-DD HH/mm/ss")}}'

#yilia主题加的配置  
jsonContent:
    meta: false
    pages: false
    posts:
      title: true
      date: true
      path: true
      text: false
      raw: false
      content: false
      slug: false
      updated: false
      comments: false
      link: false
      permalink: false
      excerpt: false
      categories: false
      tags: true

3、部署到GitHub

前面已经安装过git了,我们要注册一个github账号

可以在hexo目录下执行这些命令:

a.登录github
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
b.生成SSH秘钥

先确定你的VPS 有没有生成过ssh的key,
验证

less  ~/.ssh/id_rsa.pub

如果报错
执行下面代码

# -C后面跟住你在github的用户名邮箱,这样公钥才会被github认可
 ssh-keygen -t rsa -C example@126.com
# 查看 公钥内容 稍后加入Github 账户的 sshkey中
 less ~/.ssh/id_rsa.pub

你会看到一堆代码

如果没有报错

# -C后面跟住你在github的用户名邮箱,这样公钥才会被github认可
 ssh-keygen -t rsa -C example@126.com
# 回车后,输入一个文件夹名字,存储新的SSH 秘钥
.ssh/github
# 查看 公钥内容 稍后加入Github 账户的 sshkey中
 less ~/.ssh/id_rsa.pub

当然,也有可能遇到这种情况问你是否覆盖已有的密钥,你可以选择覆盖(确认没有人使用当前的SSH秘钥),或者不覆盖

c.创建博客项目

创建一个新项目,项目名称为 用户名.github.io ,比如我的Github用户名是jiaoxiangyu,则创建的项目名为
jiaoxiangyu.github.io

注意:用户名是你的github用户名哦!千万别弄错了,不然访问不到的!
d.将ssh秘钥添加到github中

生成后,需要将秘钥放到github上去,登录你的github账号
进入秘钥设置面板:https://github.com/jiaoxiangyu/ssh

在执行less ~/.ssh/id_rsa.pub你会看到一堆长密码,把这一段密码全部复制到github上

e.配置Hexo,编译与部署

还记得我们在_config.yml里最后一段的配置吗?

# Deployment 这里设置了Git获
#这里一定要注意不要写错了,否则部署到Github上会出问题
## Docs: https://hexo.io/docs/deployment.html
deploy: 
  type: git
  repo: git@github.com:jiaoxiangyu/jiaoxiangyu.github.io.git
  branch: master
  message: '站点更新:{{now("YYYY-MM-DD HH/mm/ss")}}'

这里,repo就是你创建的博客项目

最后一步,编译,上传静态代码

# 编译
hexo generate //可以简写 hexo g
# 在主机的hexo目录下 执行以下命令将自动更新到Github
hexo d //可以简写 hexo d

如果 hexo d 报错
ERROR Deployer not found:

npm install --save hexo-deployer-git

其它命令

hexo clean //清空你hexo/public里的内容,可以在每次修改配置文件后清除一下
hexo clean && hexo g && hexo d //多条命令可以一块输入

这就配好了,你可以用你的项目名访问你博客网站了,如:http://jaioxiangyu.github.io

进去是默认主题不太好看,你可以换主题
hexo主题网址https://hexo.io/themes/
也可以看我的主题yilia是怎么配的,在我的下一篇博客笔记中