hexo搭建

Hexo搭建Github静态博客

应用GitHub Pages创建属于自己的个人博客,GitHub将提供免费的空间。GitHub提供的域名(用户名+github+io),在Repository name对应处填写资源名,其需要使用自己的用户名,每个用户名下面只能建立一个,并且资源命名必须符合这样的规则username/username.github.io,之后勾选下面的”Initialize this repository with a README”

1.环境

1.1安装Git

下载地址:https://git-scm.com/download/win
下载安装包后正常安装即可。

1.2安装node.js

下载地址:http://nodejs.org/download/
可以下载 node-v0.10.33-x64.msi
安装时直接保持默认配置即可。

2.配置Github

1.1建立Repository

建立与你用户名对应的仓库,仓库名必须为【your_user_name.github.io】

1.2配置SSH-Key

参考:http://beiyuu.com/github-pages

3.安装Hexo

关于Hexo的安装配置过程,请以官方Hexo给出的步骤为准。

3.1Installation

打开Git命令行,执行如下命令

$ npm install -g hexo

3.2 Quick Start

1.Setup your blog
在电脑中建立一个名字叫「Hexo」的文件夹(比如我建在了D:\Hexo),然后在此文件夹中右键打开Git Bash。执行下面的命令

$ hexo init
[info] Copying data
[info] You are almost done! Don’t forget to run npm install before you start b
logging with Hexo!

Hexo随后会自动在目标文件夹建立网站所需要的文件。然后按照提示,运行 npm install(在 /D/Hexo下)

npm install

会在D:\Hexo目录中安装 node_modules。

2.Start the server
运行下面的命令(在 /D/Hexo下)

$ hexo server
[info] Hexo is running at http://localhost:4000/. Press Ctrl+C to stop.

3. Create a new post
新打开一个git bash命令行窗口,cd到/D/Hexo下,执行下面的命令

$ hexo new “My New Post”
[info] File created at d:\Hexo\source_posts\My-New-Post.md

刷新http://localhost:4000/,可以发现已生成了一篇新文章 “My New Post”。

NOTE:
有一个问题,发现 “My New Post” 被发了2遍,在Hexo server所在的git bash窗口也能看到create了2次。

$ herxo serve
[info] Hexo is running at http://localhost:4000/. Press Ctrl+C to stop.[create] d:\Hexo\source_posts\My-New-Post.md
[create] d:\Hexo\source_posts\My-New-Post.md

经验证,在hexo new “My New Post” 时,如果按Ctrl+C将hexo server停掉,就不会出现发2次的问题了。

所以,在hexo new文章时,需要stop server。

4. Generate static files
执行下面的命令,将markdown文件生成静态网页

$ hexo generate

该命令执行完后,会在 D:\Hexo\public\ 目录下生成一系列html,css等文件。

5. 编辑文章

hexo new “My New Post”会在D:\Hexo\source_posts目录下生成一个markdown文件:My-New-Post.md
可以使用一个支持markdown语法的编辑器(比如 Sublime Text 2)来编辑该文件。

6. 部署到Github

部署到Github前需要配置_config.yml文件,首先找到下面的内容

#Deployment

##Docs: http://hexo.io/docs/deployment.htmldeploy: type:

然后将它们修改为

#Deployment

##Docs: http://hexo.io/docs/deployment.htmldeploy: type: github
repository: git@github.com:zhchnchn/zhchnchn.github.io.git branch: master

NOTE1:
Repository:必须是SSH形式的url(git@github.com:zhchnchn/zhchnchn.github.io.git),而不能是HTTPS形式的url(https://github.com/zhchnchn/zhchnchn.github.io.git),否则会出现错误:

$ hexo deploy
[info] Start deploying: github
[error] https://github.com/zhchnchn/zhchnchn.github.io is not a valid repositor URL!

使用SSH url,如果电脑没有开放SSH 端口,会致部署失败。

fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

NOTE2:
如果你是为一个项目制作网站,那么需要把branch设置为gh-pages。
7. 测试
当部署完成后,在浏览器中打开http://zhchnchn.github.io/(https://zhchnchn.github.io/) ,正常显示网页,表明部署成功。
8. 总结:部署步骤
每次部署的步骤,可按以下三步来进行。

hexo clean
hexo generate
hexo deploy

9. 总结:本地调试

  1. 在执行下面的命令后,

$ hexo g #生成
$ hexo s #启动本地服务,进行文章预览调试

浏览器输入http://localhost:4000,查看搭建效果。此后的每次变更_config.yml 文件或者新建文件都可以先用此命令调试,尤其是当你想调试新添加的主题时。

2. 可以用简化的一条命令

hexo s -g

命令总结

3.3.1常用命令

hexo new “postName” #新建文章
hexo new page “pageName” #新建页面
hexo generate #生成静态页面至public目录
hexo server #开启预览访问端口(默认端口4000,’ctrl + c’关闭server)
hexo deploy #将.deploy目录部署到GitHub
hexo help # 查看帮助
hexo version #查看Hexo的版本

3.3.2

hexo deploy -g #生成加部署
hexo server -g #生成加预览

命令的简写为:

hexo n == hexo new
hexo g == hexo generate
hexo s == hexo server
hexo d == hexo deploy

4 配置Hexo

4.1 配置文件介绍

下面的各个部分的介绍,请直接参考【3】。

1._config.yml配置文件介绍
NOTE:在修改_config.yml配置文件时,按照【3】的介绍进行修改后,重新 hexo clean 或者hexo deploy时,可能会出现如下错误:

$ hexo clean
[error] { name: ‘HexoError’,
reason: ‘can not read a block mapping entry; a multiline key
may not be an imp
licit key’, mark:
{ name: null,
buffer: ‘# Hexo Configuration\n## Docs:
http://hexo.io/docs/configuration.h
tml\n## Source: https://github.com/hexojs/hexo/\n\n# Site\ntitle: Zhchnchn\nsubt
itle: Coding on the way\ndescription: Zhchnchn\’s blog\nauthor:
Zhchnchn\nemail:115063497@qq.com\nlanguage:zh-CN\n\n# URL\n## If your site is put in a subdirect ……
,
position: 249,
line: 12,
column: 0 },
message: ‘Config file load failed’,
domain:
{ domain: null,
_events: { error: [Function] },
_maxListeners: 10,
members: [ [Object] ] },
domainThrown: true,
stack: undefined }

我的_config.yml配置文件是一个空行,所以错误肯定在前面,经过对比发现,我前面修改了一下 # Site的各项设置,在冒号:后面没留空格导致了该问题,请对比一下下面的区别:

错误的设置:

author:Zhchnchn
email:XXX@qq.com
language:zh-CN

正确的设置:

author: Zhchnchn
email: XXX@qq.com
language: zh-CN

(问题在于必须要有空格)

4.2 安装主题

Hexo提供了很多主题,具体可参见Hexo Themes【4】。这里我选择使用Pacman主题。具体设置方法如下【5】

4.2.1安装

  1. 将Git Shell 切到/D/Hexo目录下,然后执行下面的命令,将pacman下载到 themes/pacman 目录下。

$ git clone https://github.com/A-limon/pacman.git themes/pacman

  1. 修改你的博客根目录/D/Hexo下的config.yml配置文件中的theme属性,将其设置为pacman。

  2. 更新pacman主题

cd themes/pacman
git pull

NOTE:先备份_config.yml 文件后再升级

4.4.2配置

如果pacman的默认设置不能满足需要的话,你可以修改 /themes/pacman/下的配置文件_config.yml来定制。

详细主题跟新安装参照【http://blog.csdn.net/qq_23435721/article/details/50938038

5.发布文章

$ hexo new “My New Post”
[info] File created at d:\Hexo\source_posts\My-New-Post.md
hexo g #生成静态文件
hexo g -d #部署到Github

Contents
  1. 1. Hexo搭建Github静态博客
    1. 1.1. 1.环境
      1. 1.1.1. 1.1安装Git
      2. 1.1.2. 1.2安装node.js
    2. 1.2. 2.配置Github
      1. 1.2.1. 1.1建立Repository
      2. 1.2.2. 1.2配置SSH-Key
    3. 1.3. 3.安装Hexo
      1. 1.3.1. 3.1Installation
      2. 1.3.2. 3.2 Quick Start
    4. 1.4. 命令总结
      1. 1.4.1. 3.3.1常用命令
      2. 1.4.2. 3.3.2
    5. 1.5. 4 配置Hexo
      1. 1.5.1. 4.1 配置文件介绍
    6. 1.6. 4.2 安装主题
      1. 1.6.1. 4.2.1安装
      2. 1.6.2. 4.4.2配置
    7. 1.7. 5.发布文章
|