天天看点

Hexo搭建个人博客初始化配置将github pages作为博客服务器Hexo使用主题绑定域名

#

准备工作(Windows 10)

- Node.js

- Git

安装Git

  • Windows:下载并安装 git.
  • Mac:使用 Homebrew, MacPorts :brew install git;或下载 安装程序 安装。
  • Linux (Ubuntu, Debian):sudo apt-get install git-core
  • Linux (Fedora, Red Hat, CentOS):sudo yum install git-core

安装Node

cURL:

Wget:

$ wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh
           

使用nvm安装Node

$ nvm install stable
           
Windows用户就只能去官网下载最新的安装包安装就行了.

安装Hexo:

初始化

$ hexo init <folder>
$ cd <folder>
$ npm install
           

目录如下:

.
├── _config.yml
├── package.json
├── scaffolds
├── source
| ├── _drafts
| └── _posts
└── themes
           

其中_config.yml是网站配置信息,package.json是应用程序信息包括,版本号,依赖库等.

使用scaffolds来管理模板,Hexo的模板是指在新建的markdown文件中默认填充的内容。例如,如果您修改scaffold/post.md中的Front-matter内容,那么每次新建一篇文章时都会包含这个修改。

source:

资源文件夹是存放用户资源的地方。除 posts 文件夹之外,开头命名为 (下划线)的文件 / 文件夹和隐藏的文件将会被忽略。Markdown 和 HTML 文件会被解析并放到 public 文件夹,而其他文件会被拷贝过去。

themes:

主题 文件夹。Hexo 会根据主题来生成静态页面。

配置

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

# 网站
title: 水人技术分享
subtitle: 只有热爱和坚持,让水人变牛人.
description:
author: 谢杨学君
language: zh-Hans
timezone:

# 网址
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: http://yoursite.com
root: /
# 文章的 永久链接 格式
permalink: :year/:month/:day/:title/
# 永久链接中各部分的默认值
permalink_defaults:

# 目录
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: :year-:month-:day-: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: 
render_drafts: false
post_asset_folder: true
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: 
pagination_dir: page

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
# 主题是NexT
theme: next

# Deployment
## Docs: https://hexo.io/docs/deployment.html
# ssh://[email protected]:xieyangxuejun/xieyangxuejun.github.io
# 我是用github pages来作为服务器.下面就将如何申请.
deploy:
type: git
repo: ssh://[email protected].com/xieyangxuejun/xieyangxuejun.github.io
branch: master
           

将github pages作为博客服务器

  • 注册github pages

    不用多讲

  • 登录自己github: https://github.com/
  • 点击Your repositories 旁边的 New repository.新建一个仓库.
  • 输入username创建仓库名称,因为之后的域名就是username.githb.io.
  • 使用ssh来连接仓库,所以要创建key.

    打开终端进入git bash中

$ cd ~/.ssh
$ ls -al
           

查看是否已经存在sshkey.如果存在就删除.

$ rm xxx
           

设置name和email

git config --global user.name "<your name>"
git config --global user.email "<your email>"
           

生成ssh秘钥

这个时候就能查看到里面有了id_rsa开通的秘钥

获取Key

$ cat ~/.ssh/id_rsa.pub
           

会得到

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC1jsXyPR6N6PdQ2/EEGRNarOKwtwcNuJnONHRRn31I68KI+UHVhcvVCvN0tCFZbKLHLyHzRR7eK09V/mYxtSbqSZHViU1Pv6YbBh+zV/zx3UB2tCh4kHqaPNDPVlxp8wlIrf5D0AaEcXzE/WsfSLOKM/gYl [email protected]
           

然后在Github上添加SSH秘钥,打开 https://github.com/settings/keys 中add new ssh key.

添加一个title,将上面的复制过去.

验证

ssh [email protected]
           

出现

PTY allocation request failed on channel 0

Hi xieyangxuejun! You’ve successfully authenticated, but GitHub does not provide shell access.

Connection to github.com closed.

然后将

ssh://[email protected]/xieyangxuejun/xieyangxuejun.github.io

添加到_config.yml中

# Deployment
## Docs: https://hexo.io/docs/deployment.html
# ssh://[email protected]/git-xuhao/git-xuhao.github.io
deploy:
type: git
repo: ssh://[email protected].com/xieyangxuejun/xieyangxuejun.github.io
branch: master
           

执行下面的命令

npm install hexo-deployer-git --save
hexo deploy
           

就部署成功了.

Hexo使用

新建博文

hexo new [layout] <title>
           

生成静态文件 -d立即部署 -w监视文件变动

$ hexo generate
$ hexo g
           

发布模板草稿

$ hexo publish [layout] <filename>
           

运行server

部署

$ hexo deploy -g
           

迁移博客

清楚缓存文件和已经生成的静态文件

hexo clean
           

调试模式

自定义配置文件

布局

Hexo 有三种默认布局:post、page 和 draft,它们分别对应不同的路径,而您自定义的其他布局和 post 相同,都将储存到 source/_posts 文件夹。

文件名称

Hexo 默认以标题做为文件名称,但您可编辑 new_post_name 参数来改变默认的文件名称,举例来说,设为 :year-:month-:day-:title.md 可让您更方便的通过日期来管理文章。

主题

在Hexo项目中有两个配置,站点配置文件和主题配置文件.

其中主题配置文件包括

.
├── _config.yml
├── languages
├── layout
├── scripts
└── source
           

目前我使用的是NexT主题

进入项目中theme文件夹,使用git clone https://github.com/iissnan/hexo-theme-next.git克隆这个项目.打开站点的_config.yml中theme: 改为next.

再次部署一下就行了.

关于Next主题的使用参考官方文档:

http://theme-next.iissnan.com/getting-started.html

绑定域名

首先在阿里云上购买一个域名,进入后台域名解析中

将xieyangxuejun.github.io解析到这个域名上就行了